|
1 | 1 | import { test, expect } from "vitest";
|
2 |
| -import getEndOfSection from "./getEndOfSection"; |
| 2 | +import getEndOfSection, { getMarkdownHeadings } from "./getEndOfSection"; |
3 | 3 |
|
4 | 4 | test("getEndOfSection - find the end of a section", () => {
|
5 | 5 | const lines = [
|
@@ -184,6 +184,26 @@ test("getEndOfSection - target is heading, should not consider subsections", ()
|
184 | 184 | expect(result).toBe(5);
|
185 | 185 | });
|
186 | 186 |
|
| 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 | + |
187 | 207 | test("getEndOfSection - target is heading, should consider subsections", () => {
|
188 | 208 | const lines = [
|
189 | 209 | "# Notes", // target (0)
|
@@ -290,3 +310,58 @@ test("getEndOfSection - capture to last line, shouldConsiderSubsections OFF", ()
|
290 | 310 | const result = getEndOfSection(lines, targetLine, false);
|
291 | 311 | expect(result).toBe(2);
|
292 | 312 | });
|
| 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 | +}); |
0 commit comments