Skip to content

Commit

Permalink
test(remark-table-of-content): re-organization
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jan 26, 2025
1 parent dd1163c commit cf8fc7e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 94 deletions.
89 changes: 1 addition & 88 deletions remark/table-of-content/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { unified } from "unified";
import parseMarkdown from "remark-parse";
import remark2rehype from "remark-rehype";
import html from "rehype-stringify";
import extractFrontmatter from "remark-frontmatter";
import pluginReadFrontmatter from "remark-read-frontmatter";
import pluginToc, { DataToc } from "../src/index.js";
import { Data } from "mdast";
import pluginToc from "../src/index.js";

describe("Extract table of content", function () {
it("default", async function () {
Expand All @@ -28,45 +25,6 @@ describe("Extract table of content", function () {
]);
});

it("`depth_min` option", async function () {
const { data } = await unified()
.use(parseMarkdown)
.use(pluginToc, { depth_min: 2 })
.use(remark2rehype)
.use(html).process(dedent`
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
`);
data.toc?.should.eql([
{ title: "Heading 2", depth: 2, anchor: "heading-2" },
{ title: "Heading 3", depth: 3, anchor: "heading-3" },
]);
});

it("`depth_max` option", async function () {
const { data } = await unified()
.use(parseMarkdown)
.use(pluginToc, { depth_max: 5 })
.use(remark2rehype)
.use(html).process(dedent`
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6
`);
data.toc?.should.eql([
{ title: "Heading 1", depth: 1, anchor: "heading-1" },
{ title: "Heading 2", depth: 2, anchor: "heading-2" },
{ title: "Heading 3", depth: 3, anchor: "heading-3" },
{ title: "Heading 4", depth: 4, anchor: "heading-4" },
{ title: "Heading 5", depth: 5, anchor: "heading-5" },
]);
});

it("heading with styling", async function () {
const { data } = await unified()
.use(parseMarkdown)
Expand Down Expand Up @@ -108,49 +66,4 @@ describe("Extract table of content", function () {
{ title: "Heading 3 code();", depth: 3, anchor: "heading-3-code" },
]);
});

describe("option `property`", function () {
it("multi-level property", async function () {
type DataWithToc = Data & {
parent: {
toc: DataToc;
};
};
const { data } = (await unified()
.use(parseMarkdown)
.use(extractFrontmatter, ["yaml"])
.use(pluginReadFrontmatter)
.use(pluginToc, { property: ["parent", "toc"] })
.use(remark2rehype)
.use(html).process(dedent`
# Heading 1
## Heading 2
`)) as unknown as { data: DataWithToc };
data.parent.should.eql({
toc: [
{ title: "Heading 1", depth: 1, anchor: "heading-1" },
{ title: "Heading 2", depth: 2, anchor: "heading-2" },
],
});
});

it("preserve value if fontmatter contains `toc: false`", async function () {
const { data } = await unified()
.use(parseMarkdown)
.use(extractFrontmatter, ["yaml"])
.use(pluginReadFrontmatter)
.use(pluginToc)
.use(remark2rehype)
.use(html).process(dedent`
---
toc: false
---
# Heading 1
## Heading 2
`);
data.should.eql({
toc: false,
});
});
});
});
48 changes: 48 additions & 0 deletions remark/table-of-content/test/option-depth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import "should";
import dedent from "dedent";
import { unified } from "unified";
import parseMarkdown from "remark-parse";
import remark2rehype from "remark-rehype";
import html from "rehype-stringify";
import pluginToc from "../src/index.js";

describe("Options `depth_min` and `depth_max`", function () {
it("`depth_min` option", async function () {
const { data } = await unified()
.use(parseMarkdown)
.use(pluginToc, { depth_min: 2 })
.use(remark2rehype)
.use(html).process(dedent`
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
`);
data.toc?.should.eql([
{ title: "Heading 2", depth: 2, anchor: "heading-2" },
{ title: "Heading 3", depth: 3, anchor: "heading-3" },
]);
});

it("`depth_max` option", async function () {
const { data } = await unified()
.use(parseMarkdown)
.use(pluginToc, { depth_max: 5 })
.use(remark2rehype)
.use(html).process(dedent`
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6
`);
data.toc?.should.eql([
{ title: "Heading 1", depth: 1, anchor: "heading-1" },
{ title: "Heading 2", depth: 2, anchor: "heading-2" },
{ title: "Heading 3", depth: 3, anchor: "heading-3" },
{ title: "Heading 4", depth: 4, anchor: "heading-4" },
{ title: "Heading 5", depth: 5, anchor: "heading-5" },
]);
});
});
55 changes: 55 additions & 0 deletions remark/table-of-content/test/option-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import "should";
import dedent from "dedent";
import { unified } from "unified";
import parseMarkdown from "remark-parse";
import remark2rehype from "remark-rehype";
import html from "rehype-stringify";
import extractFrontmatter from "remark-frontmatter";
import pluginReadFrontmatter from "remark-read-frontmatter";
import pluginToc, { DataToc } from "../src/index.js";
import { Data } from "mdast";

describe("option `property`", function () {
it("multi-level property", async function () {
type DataWithToc = Data & {
parent: {
toc: DataToc;
};
};
const { data } = (await unified()
.use(parseMarkdown)
.use(extractFrontmatter, ["yaml"])
.use(pluginReadFrontmatter)
.use(pluginToc, { property: ["parent", "toc"] })
.use(remark2rehype)
.use(html).process(dedent`
# Heading 1
## Heading 2
`)) as unknown as { data: DataWithToc };
data.parent.should.eql({
toc: [
{ title: "Heading 1", depth: 1, anchor: "heading-1" },
{ title: "Heading 2", depth: 2, anchor: "heading-2" },
],
});
});

it("preserve value if fontmatter contains `toc: false`", async function () {
const { data } = await unified()
.use(parseMarkdown)
.use(extractFrontmatter, ["yaml"])
.use(pluginReadFrontmatter)
.use(pluginToc)
.use(remark2rehype)
.use(html).process(dedent`
---
toc: false
---
# Heading 1
## Heading 2
`);
data.should.eql({
toc: false,
});
});
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import "should";
import each from "each";
import fs from "fs";
import path from "path";
import * as fs from "node:fs";
import * as path from "node:path";
import { exec } from "child_process";

const __dirname = new URL(".", import.meta.url).pathname;
const dir = path.resolve(__dirname, "../samples");
const samples = fs.readdirSync(dir);

describe("Samples", function () {
each(samples, true, (sample) => {
if (!/\.js$/.test(sample)) return;
for (const sample of samples) {
// if (!/\.js$/.test(sample)) return;
it(`Sample ${sample}`, function (callback) {
exec(`node ${path.resolve(dir, sample)}`, (err) => callback(err));
});
});
}
});

0 comments on commit cf8fc7e

Please sign in to comment.