Skip to content

Instantly share code, notes, and snippets.

@tol-is
Last active January 6, 2022 20:35
Show Gist options
  • Select an option

  • Save tol-is/dcf6656a4d241ca44dab9367d33b8b11 to your computer and use it in GitHub Desktop.

Select an option

Save tol-is/dcf6656a4d241ca44dab9367d33b8b11 to your computer and use it in GitHub Desktop.

Revisions

  1. tol-is revised this gist Jan 6, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    const markdown = `# Heading One\n## Heading Two\n ##Should not Match\n ## # Only the first parth should match \n ### Heading Three`;


    const toc = getMarkdownHeadings(markdown);
    const toc = getToc(markdown);

    /*
    Expecting
  2. tol-is revised this gist Jan 6, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion getToc.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    const getMarkdownHeadings = (source: string) =>
    const getToc = (source: string) =>
    source
    .split("\n")
    .map((line) => {
  3. tol-is revised this gist Jan 6, 2022. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    const markdown = `# Heading One\n## Heading Two\n ##Should not Match\n ## # Only the first parth should match \n ### Heading Three`;


    const toc = getMarkdownHeadings(markdown);

    /*
    Expecting
    [
    {
    level: 1,
    heading: "Heading One"
    },
    {
    level: 2,
    heading: "Heading Two"
    },
    {
    level: 2,
    heading: "# Only the first parth should match"
    },
    {
    level: 3,
    heading: "Heading Three"
    }
    ];
    */
  4. tol-is created this gist Jan 6, 2022.
    14 changes: 14 additions & 0 deletions getToc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    const getMarkdownHeadings = (source: string) =>
    source
    .split("\n")
    .map((line) => {
    const matches = line.match(/^#+[\s]+/);

    return matches
    ? {
    level: matches[0].trim().length,
    heading: line.replace(matches[0], "").trim(),
    }
    : false;
    })
    .filter(Boolean);