Skip to content

Commit

Permalink
test: add test casts for getting markdown headings
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Nov 3, 2024
1 parent 159887f commit 7c55eb8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
57 changes: 56 additions & 1 deletion src/formatters/helpers/getEndOfSection.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "vitest";
import getEndOfSection from "./getEndOfSection";
import getEndOfSection, { getMarkdownHeadings } from "./getEndOfSection";

test("getEndOfSection - find the end of a section", () => {
const lines = [
Expand Down Expand Up @@ -310,3 +310,58 @@ test("getEndOfSection - capture to last line, shouldConsiderSubsections OFF", ()
const result = getEndOfSection(lines, targetLine, false);
expect(result).toBe(2);
});


test("getMarkdownHeadings - correctly identifies headings", () => {
const lines = [
"# Heading 1",
"## Heading 2",
"### Heading 3",
"#### Heading 4",
"##### Heading 5",
"###### Heading 6",
"Normal text",
"#Not a heading",
"# Heading with #hash in text",
"##Invalid heading",
"",
" # Heading with leading spaces",
];

const result = getMarkdownHeadings(lines);

expect(result).toEqual([
{ level: 1, text: "Heading 1", line: 0 },
{ level: 2, text: "Heading 2", line: 1 },
{ level: 3, text: "Heading 3", line: 2 },
{ level: 4, text: "Heading 4", line: 3 },
{ level: 5, text: "Heading 5", line: 4 },
{ level: 6, text: "Heading 6", line: 5 },
{ level: 1, text: "Heading with #hash in text", line: 8 },
]);
});

test("getMarkdownHeadings - handles empty input", () => {
const lines: string[] = [];

const result = getMarkdownHeadings(lines);

expect(result).toEqual([]);
});

test("getMarkdownHeadings - correctly ignores Obsidian tags", () => {
const lines = [
"# Real Heading",
"#tag",
"#anothertag",
"Text with #inline_tag",
"## Heading with #tag in it",
];

const result = getMarkdownHeadings(lines);

expect(result).toEqual([
{ level: 1, text: "Real Heading", line: 0 },
{ level: 2, text: "Heading with #tag in it", line: 4 },
]);
});
2 changes: 1 addition & 1 deletion src/formatters/helpers/getEndOfSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function isSameHeading(heading1: Heading, heading2: Heading): boolean {
return heading1.line === heading2.line;
}

function getMarkdownHeadings(bodyLines: string[]): Heading[] {
export function getMarkdownHeadings(bodyLines: string[]): Heading[] {
const headers: Heading[] = [];

bodyLines.forEach((line, index) => {
Expand Down

0 comments on commit 7c55eb8

Please sign in to comment.