-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(remark-table-of-content): re-organization
- Loading branch information
Showing
4 changed files
with
109 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); |
11 changes: 5 additions & 6 deletions
11
remark/table-of-content/test/samples.js → remark/table-of-content/test/samples.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); | ||
} | ||
}); |