-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mdast-util-select-section package
Co-authored-by: vanyauhalin <[email protected]>
- Loading branch information
1 parent
cbec10b
commit 16bab88
Showing
9 changed files
with
94 additions
and
51 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
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,60 @@ | ||
import {fromMarkdown} from "mdast-util-from-markdown" | ||
import {toMarkdown} from "mdast-util-to-markdown" | ||
import {test} from "uvu" | ||
import {is} from "uvu/assert" | ||
import {extractSection} from "./main.ts" | ||
|
||
const sc: [string, string, string][] = [ | ||
["", "", ""], | ||
["", "h", ""], | ||
["h", "h", ""], | ||
["## h", "h", ""], | ||
[j("## h", "a"), "h", "a"], | ||
[j("## H", "a"), "h", "a"], | ||
[j("## h", "a"), " h ", "a"], | ||
[j("## h", "a", "b"), "h", j("a", "b")], | ||
[j("## h", "a", "## h", "b"), "h", "a"], | ||
[j("# h", "a", "## h", "b"), "h", "b"], | ||
] | ||
|
||
for (const [a, h, e] of sc) { | ||
test(`extractSection(): selects section '${h}' from '${a}'`, () => { | ||
const x = fromMarkdown(a) | ||
const y = extractSection(h, x) | ||
|
||
let z = toMarkdown(y) | ||
if (z.endsWith("\n")) { | ||
z = z.slice(0, -1) | ||
} | ||
|
||
is(z, e) | ||
}) | ||
} | ||
|
||
const rs: [string, string, string][] = [ | ||
["## h", "h", ""], | ||
[j("## h", "a", "## D", "d"), "h", j("## D", "d")], | ||
[j("## D", "d", "## h", "a"), "h", j("## D", "d")], | ||
[j("## D", "d", "## h", "a", "## E", "e"), "h", j("## D", "d", "## E", "e")], | ||
] | ||
|
||
for (const [a, h, e] of rs) { | ||
test(`extractSection(): removes section '${h}' from '${a}'`, () => { | ||
const x = fromMarkdown(a) | ||
|
||
extractSection(h, x) | ||
|
||
let z = toMarkdown(x) | ||
if (z.endsWith("\n")) { | ||
z = z.slice(0, -1) | ||
} | ||
|
||
is(z, e) | ||
}) | ||
} | ||
|
||
test.run() | ||
|
||
function j(...a: string[]): string { | ||
return a.join("\n\n") | ||
} |
24 changes: 16 additions & 8 deletions
24
...ges/mdast-util-select-section/lib/main.ts → ...es/mdast-util-extract-section/lib/main.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,35 +1,43 @@ | ||
import {isHeadingNode, isParentNode} from "@onlyoffice/mdast-util-is-node" | ||
import {type Node, type Root} from "mdast" | ||
import {type Node, type Root, type RootContent} from "mdast" | ||
import {toString} from "mdast-util-to-string" | ||
|
||
export function selectSection(h: string, n: Node): Root { | ||
export function extractSection(h: string, n: Node): Root { | ||
const r: Root = {type: "root", children: []} | ||
|
||
if (!h || !isParentNode(n)) { | ||
return r | ||
} | ||
|
||
h = h.trim().toLocaleLowerCase() | ||
|
||
let f = false | ||
const a: RootContent[] = [] | ||
|
||
let f: -1 | 0 | 1 = -1 | ||
|
||
for (const c of n.children) { | ||
if (isHeadingNode(c) && c.depth === 2 && f) { | ||
break | ||
if (isHeadingNode(c) && c.depth === 2 && f === 0) { | ||
f = 1 | ||
} | ||
|
||
if (isHeadingNode(c) && c.depth === 2) { | ||
if (isHeadingNode(c) && c.depth === 2 && f === -1) { | ||
const s = toString(c).toLocaleLowerCase() | ||
|
||
if (s === h) { | ||
f = true | ||
f = 0 | ||
continue | ||
} | ||
} | ||
|
||
if (f) { | ||
if (f === 0) { | ||
r.children.push(c) | ||
continue | ||
} | ||
|
||
a.push(c) | ||
} | ||
|
||
n.children = a | ||
|
||
return r | ||
} |
2 changes: 1 addition & 1 deletion
2
...es/mdast-util-select-section/package.json → ...s/mdast-util-extract-section/package.json
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.