Skip to content

Commit

Permalink
Merge pull request #721 from wrayzheng/fix-heading-pattern
Browse files Browse the repository at this point in the history
fix: heading pattern issue with leading tag
  • Loading branch information
chhoumann authored Nov 3, 2024
2 parents 846cb1f + 99bc7ef commit 3f27a4b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
77 changes: 76 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 @@ -184,6 +184,26 @@ test("getEndOfSection - target is heading, should not consider subsections", ()
expect(result).toBe(5);
});

test("getEndOfSection - capture to end of section with a leading tag, should not consider subsections", () => {
const lines = [
"# Notes",
"",
"## Topic A", // target (2)
"content a1",
"#TagForA1",
"content a2", // result (5)
"## Topic B",
"content b1",
"",
"",
];

const targetLine = 2;

const result = getEndOfSection(lines, targetLine, false);
expect(result).toBe(5);
});

test("getEndOfSection - target is heading, should consider subsections", () => {
const lines = [
"# Notes", // target (0)
Expand Down Expand Up @@ -290,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 },
]);
});
4 changes: 2 additions & 2 deletions src/formatters/helpers/getEndOfSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ 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) => {
const match = line.match(/^(#+)[\s]?(.*)$/);
const match = line.match(/^(#+)[\s]+(.*)$/);

if (!match) return;

Expand Down

0 comments on commit 3f27a4b

Please sign in to comment.