|
| 1 | +import { suite } from "uvu"; |
| 2 | +import * as assert from "uvu/assert"; |
| 3 | + |
| 4 | +import unified from "unified"; |
| 5 | +import markdown from "remark-parse"; |
| 6 | +import extract_frontmatter from "remark-frontmatter"; |
| 7 | +import rehype from "remark-rehype"; |
| 8 | +import stringify from "rehype-stringify"; |
| 9 | +import vFile from "vfile"; |
| 10 | + |
| 11 | +import { headings, custom_vfile } from "./headings"; |
| 12 | + |
| 13 | +const { process } = unified() |
| 14 | + .use(markdown) |
| 15 | + .use(extract_frontmatter) |
| 16 | + .use(headings) |
| 17 | + .use(rehype) |
| 18 | + // .use(() => (tree) => console.log(JSON.stringify(tree, null, 2))) |
| 19 | + .use(stringify); |
| 20 | + |
| 21 | +const _headings = suite("headings"); |
| 22 | + |
| 23 | +_headings("transforms and formats headings", async () => { |
| 24 | + const sections: unknown[] = []; |
| 25 | + const src = vFile({ |
| 26 | + contents: `### hello`, |
| 27 | + data: { |
| 28 | + dir: "blog", |
| 29 | + sections, |
| 30 | + section_stack: [sections], |
| 31 | + section_title: "section", |
| 32 | + prev_level: 3, |
| 33 | + slugs: [], |
| 34 | + seen_slugs: new Map(), |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + const output = await process(src); |
| 39 | + |
| 40 | + assert.equal( |
| 41 | + output.contents, |
| 42 | + `<h3><span id="section-hello" class="offset-anchor"></span><a href="blog#section-hello" class="anchor" aria-hidden></a>hello</h3>` |
| 43 | + ); |
| 44 | +}); |
| 45 | + |
| 46 | +_headings("transforms and formats multi-level headings", async () => { |
| 47 | + const sections: unknown[] = []; |
| 48 | + const src = vFile({ |
| 49 | + contents: `### subsection |
| 50 | +
|
| 51 | +#### subsubsection |
| 52 | +`, |
| 53 | + data: { |
| 54 | + dir: "blog", |
| 55 | + sections, |
| 56 | + section_stack: [sections], |
| 57 | + section_title: "section", |
| 58 | + prev_level: 3, |
| 59 | + slugs: [], |
| 60 | + seen_slugs: new Map(), |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + const output = await process(src); |
| 65 | + |
| 66 | + assert.equal( |
| 67 | + output.contents, |
| 68 | + `<h3><span id="section-subsection" class="offset-anchor"></span><a href="blog#section-subsection" class="anchor" aria-hidden></a>subsection</h3> |
| 69 | +<h4><span id="section-subsection-subsubsection" class="offset-anchor"></span><a href="blog#section-subsection-subsubsection" class="anchor" aria-hidden></a>subsubsection</h4>` |
| 70 | + ); |
| 71 | +}); |
| 72 | + |
| 73 | +_headings("transforms and formats multi-level headings", async () => { |
| 74 | + const sections: unknown[] = []; |
| 75 | + const src = vFile({ |
| 76 | + contents: `### subsection |
| 77 | +
|
| 78 | +#### subsubsection |
| 79 | +
|
| 80 | +##### subsubsubsection |
| 81 | +
|
| 82 | +`, |
| 83 | + data: { |
| 84 | + dir: "blog", |
| 85 | + sections, |
| 86 | + section_stack: [sections], |
| 87 | + section_title: "section", |
| 88 | + prev_level: 3, |
| 89 | + slugs: [], |
| 90 | + seen_slugs: new Map(), |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + const output = await process(src); |
| 95 | + |
| 96 | + assert.equal( |
| 97 | + output.contents, |
| 98 | + `<h3><span id="section-subsection-1" class="offset-anchor"></span><a href="blog#section-subsection-1" class="anchor" aria-hidden></a>subsection</h3> |
| 99 | +<h4><span id="section-subsection-1-subsubsection" class="offset-anchor"></span><a href="blog#section-subsection-1-subsubsection" class="anchor" aria-hidden></a>subsubsection</h4> |
| 100 | +<h5><span id="section-subsection-1-subsubsection-subsubsubsection" class="offset-anchor" data-scrollignore></span><a href="blog#section-subsection-1-subsubsection-subsubsubsection" class="anchor" aria-hidden></a>subsubsubsection</h5>` |
| 101 | + ); |
| 102 | +}); |
| 103 | + |
| 104 | +_headings.only("transforms and formats multi-level headings", async () => { |
| 105 | + const sections: unknown[] = []; |
| 106 | + const src = vFile({ |
| 107 | + contents: `### subsection |
| 108 | +
|
| 109 | +#### subsubsection |
| 110 | +
|
| 111 | +##### subsubsubsection |
| 112 | +
|
| 113 | +### one |
| 114 | +
|
| 115 | +#### two |
| 116 | +
|
| 117 | +##### three |
| 118 | +
|
| 119 | +#### four |
| 120 | +
|
| 121 | +`, |
| 122 | + data: { |
| 123 | + dir: "blog", |
| 124 | + sections, |
| 125 | + section_stack: [sections], |
| 126 | + section_title: "section", |
| 127 | + prev_level: 3, |
| 128 | + slugs: [], |
| 129 | + seen_slugs: new Map(), |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + const output = await process(src); |
| 134 | + |
| 135 | + assert.equal( |
| 136 | + output.contents, |
| 137 | + `<h3><span id="section-subsection" class="offset-anchor"></span><a href="blog#section-subsection" class="anchor" aria-hidden></a>subsection</h3> |
| 138 | +<h4><span id="section-subsection-subsubsection" class="offset-anchor"></span><a href="blog#section-subsection-subsubsection" class="anchor" aria-hidden></a>subsubsection</h4> |
| 139 | +<h5><span id="section-subsection-subsubsection-subsubsubsection" class="offset-anchor" data-scrollignore></span><a href="blog#section-subsection-subsubsection-subsubsubsection" class="anchor" aria-hidden></a>subsubsubsection</h5> |
| 140 | +<h3><span id="section-one" class="offset-anchor"></span><a href="blog#section-one" class="anchor" aria-hidden></a>one</h3> |
| 141 | +<h4><span id="section-one-two" class="offset-anchor"></span><a href="blog#section-one-two" class="anchor" aria-hidden></a>two</h4> |
| 142 | +<h5><span id="section-one-two-three" class="offset-anchor" data-scrollignore></span><a href="blog#section-one-two-three" class="anchor" aria-hidden></a>three</h5> |
| 143 | +<h4><span id="section-one-four" class="offset-anchor"></span><a href="blog#section-one-four" class="anchor" aria-hidden></a>four</h4>` |
| 144 | + ); |
| 145 | +}); |
| 146 | + |
| 147 | +_headings.run(); |
0 commit comments