Skip to content

Commit

Permalink
feat: improve TOC generator
Browse files Browse the repository at this point in the history
  • Loading branch information
shah committed Jan 23, 2024
1 parent 8878797 commit 211d289
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
28 changes: 14 additions & 14 deletions lib/universal/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ws from "./whitespace.ts";

export function typicalMarkdownTags(options: {
readonly toc?: {
heading: (text: string, level: number) => void;
heading: (text: string, level: number) => string;
};
} = {}) {
const wrap = (wrapper: string, str: string) => `${wrapper}${str}${wrapper}`;
Expand Down Expand Up @@ -66,9 +66,9 @@ export function typicalMarkdownTags(options: {
const bold = (str: string) => wrap("**", str);

const heading = (level: number, str: string) => {
const markdown = spaces(join(times(always("#"), level)), str);
options?.toc?.heading(str, level);
return markdown;
// in case table of contents rewriting is required, do it now
const rewrittenText = options?.toc?.heading(str, level);
return spaces(join(times(always("#"), level)), rewrittenText ?? str);
};

const h1 = (str: string) => heading(1, str);
Expand Down Expand Up @@ -149,16 +149,16 @@ export class MarkdownDocument<
readonly tags: Tags = typicalMarkdownTags({
toc: {
heading: (text, level) => {
if (this.tableOfContents) {
const anchorName = this.tags.anchorName(text);
this.tableOfContents?.headings.push({
heading: text,
level,
anchorName,
anchorText: `<a name="${anchorName}">${text}</a>`,
tocItemText: `[${text}](#${anchorName})`,
});
}
const anchorName = this.tags.anchorName(text);
const state = {
heading: text,
level,
anchorName,
anchorText: text,
tocItemText: `[${text}](#${anchorName})`,
};
this.tableOfContents.headings.push(state);
return state.anchorText;
},
},
}) as Tags,
Expand Down
14 changes: 9 additions & 5 deletions lib/universal/markdown_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { testingAsserts as ta } from "../../deps-test.ts";
import * as mod from "./markdown.ts";

const golden1 = `# Markdown test
const golden1 = `- [Markdown test](#markdown-test)
- [Second level heading](#second-level-heading)
- [Third level heading](#third-level-heading)
# Markdown test
This is a test document which should prepare **some bold text** and ***some italic text***.
Expand Down Expand Up @@ -43,28 +47,28 @@ Deno.test("Generated Markdown", () => {
${t.table(["h-column1", "h-column2"], ["r1-column1", "r1-column2"])}`;

ta.assertEquals(md.markdownText(), golden1);
ta.assertEquals(md.markdownText(() => md.tocMarkdownLists() + "\n"), golden1);
ta.assertEquals(md.tableOfContents, {
headings: [
{
level: 1,
heading: "Markdown test",
anchorName: "markdown-test",
anchorText: `<a name="markdown-test">Markdown test</a>`,
anchorText: `Markdown test`,
tocItemText: `[Markdown test](#markdown-test)`,
},
{
level: 2,
heading: "Second level heading",
anchorName: "second-level-heading",
anchorText: `<a name="second-level-heading">Second level heading</a>`,
anchorText: `Second level heading`,
tocItemText: `[Second level heading](#second-level-heading)`,
},
{
level: 3,
heading: "Third level heading",
anchorName: "third-level-heading",
anchorText: `<a name="third-level-heading">Third level heading</a>`,
anchorText: `Third level heading`,
tocItemText: `[Third level heading](#third-level-heading)`,
},
],
Expand Down

0 comments on commit 211d289

Please sign in to comment.