Skip to content

Commit 3f27a4b

Browse files
authored
Merge pull request #721 from wrayzheng/fix-heading-pattern
fix: heading pattern issue with leading tag
2 parents 846cb1f + 99bc7ef commit 3f27a4b

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/formatters/helpers/getEndOfSection.test.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect } from "vitest";
2-
import getEndOfSection from "./getEndOfSection";
2+
import getEndOfSection, { getMarkdownHeadings } from "./getEndOfSection";
33

44
test("getEndOfSection - find the end of a section", () => {
55
const lines = [
@@ -184,6 +184,26 @@ test("getEndOfSection - target is heading, should not consider subsections", ()
184184
expect(result).toBe(5);
185185
});
186186

187+
test("getEndOfSection - capture to end of section with a leading tag, should not consider subsections", () => {
188+
const lines = [
189+
"# Notes",
190+
"",
191+
"## Topic A", // target (2)
192+
"content a1",
193+
"#TagForA1",
194+
"content a2", // result (5)
195+
"## Topic B",
196+
"content b1",
197+
"",
198+
"",
199+
];
200+
201+
const targetLine = 2;
202+
203+
const result = getEndOfSection(lines, targetLine, false);
204+
expect(result).toBe(5);
205+
});
206+
187207
test("getEndOfSection - target is heading, should consider subsections", () => {
188208
const lines = [
189209
"# Notes", // target (0)
@@ -290,3 +310,58 @@ test("getEndOfSection - capture to last line, shouldConsiderSubsections OFF", ()
290310
const result = getEndOfSection(lines, targetLine, false);
291311
expect(result).toBe(2);
292312
});
313+
314+
315+
test("getMarkdownHeadings - correctly identifies headings", () => {
316+
const lines = [
317+
"# Heading 1",
318+
"## Heading 2",
319+
"### Heading 3",
320+
"#### Heading 4",
321+
"##### Heading 5",
322+
"###### Heading 6",
323+
"Normal text",
324+
"#Not a heading",
325+
"# Heading with #hash in text",
326+
"##Invalid heading",
327+
"",
328+
" # Heading with leading spaces",
329+
];
330+
331+
const result = getMarkdownHeadings(lines);
332+
333+
expect(result).toEqual([
334+
{ level: 1, text: "Heading 1", line: 0 },
335+
{ level: 2, text: "Heading 2", line: 1 },
336+
{ level: 3, text: "Heading 3", line: 2 },
337+
{ level: 4, text: "Heading 4", line: 3 },
338+
{ level: 5, text: "Heading 5", line: 4 },
339+
{ level: 6, text: "Heading 6", line: 5 },
340+
{ level: 1, text: "Heading with #hash in text", line: 8 },
341+
]);
342+
});
343+
344+
test("getMarkdownHeadings - handles empty input", () => {
345+
const lines: string[] = [];
346+
347+
const result = getMarkdownHeadings(lines);
348+
349+
expect(result).toEqual([]);
350+
});
351+
352+
test("getMarkdownHeadings - correctly ignores Obsidian tags", () => {
353+
const lines = [
354+
"# Real Heading",
355+
"#tag",
356+
"#anothertag",
357+
"Text with #inline_tag",
358+
"## Heading with #tag in it",
359+
];
360+
361+
const result = getMarkdownHeadings(lines);
362+
363+
expect(result).toEqual([
364+
{ level: 1, text: "Real Heading", line: 0 },
365+
{ level: 2, text: "Heading with #tag in it", line: 4 },
366+
]);
367+
});

src/formatters/helpers/getEndOfSection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ function isSameHeading(heading1: Heading, heading2: Heading): boolean {
88
return heading1.line === heading2.line;
99
}
1010

11-
function getMarkdownHeadings(bodyLines: string[]): Heading[] {
11+
export function getMarkdownHeadings(bodyLines: string[]): Heading[] {
1212
const headers: Heading[] = [];
1313

1414
bodyLines.forEach((line, index) => {
15-
const match = line.match(/^(#+)[\s]?(.*)$/);
15+
const match = line.match(/^(#+)[\s]+(.*)$/);
1616

1717
if (!match) return;
1818

0 commit comments

Comments
 (0)