-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreplaceMdSection.mjs
51 lines (44 loc) · 1.6 KB
/
replaceMdSection.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import { unified } from "unified";
import REMARK_STRINGIFY_OPTIONS from "./REMARK_STRINGIFY_OPTIONS.mjs";
import remarkPluginReplaceSection from "./remarkPluginReplaceSection.mjs";
/**
* Replaces a markdown section.
* @kind function
* @name replaceMdSection
* @param {string} markdown Markdown.
* @param {string} targetHeading Heading text of the section to replace.
* @param {object} replacementMdAst Replacement markdown AST (with a [`root`](https://github.com/syntax-tree/mdast#root) top level type), defaulting to empty.
* @returns {string} Updated markdown.
* @ignore
*/
export default function replaceMdSection(
markdown,
targetHeading,
replacementMdAst
) {
if (typeof markdown !== "string")
throw new TypeError("Argument 1 `markdown` must be a string.");
if (markdown === "")
throw new TypeError("Argument 1 `markdown` must be a populated string.");
if (typeof targetHeading !== "string")
throw new TypeError("Argument 2 `targetHeading` must be a string.");
if (targetHeading === "")
throw new TypeError(
"Argument 2 `targetHeading` must be a populated string."
);
if (typeof replacementMdAst !== "object")
throw new TypeError("Argument 3 `replacementMdAst` must be an object.");
return unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkStringify, REMARK_STRINGIFY_OPTIONS)
.use(remarkPluginReplaceSection, {
targetHeading,
replacementAst: replacementMdAst,
})
.processSync(markdown)
.toString();
}