-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement limit without consider image
close: #14
- Loading branch information
Showing
6 changed files
with
120 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { fromMarkdown } from 'mdast-util-from-markdown'; | ||
import { Content, Parent } from 'mdast-util-from-markdown/lib/index'; | ||
import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm'; | ||
import { toMarkdown } from 'mdast-util-to-markdown'; | ||
import { gfm } from 'micromark-extension-gfm'; | ||
|
||
export function parseMarkdown(text: string) { | ||
const tree = fromMarkdown(trimLeadingWS(text), { | ||
extensions: [gfm()], | ||
mdastExtensions: [gfmFromMarkdown()], | ||
}); | ||
return tree; | ||
} | ||
|
||
function trimLeadingWS(str: string) { | ||
/* | ||
Get the initial indentation | ||
But ignore new line characters | ||
*/ | ||
const matcher = /^[\r\n]?(\s+)/; | ||
if (matcher.test(str)) { | ||
/* | ||
Replace the initial whitespace | ||
globally and over multiple lines | ||
*/ | ||
return str.replace(new RegExp('^' + str.match(matcher)![1], 'gm'), ''); | ||
} else { | ||
// Regex doesn't match so return the original string | ||
return str; | ||
} | ||
} | ||
|
||
export function walk(root: Parent, cb: (token: Content) => boolean | void) { | ||
root.children.forEach((node) => { | ||
if (node) { | ||
const skip = cb(node); | ||
if (!skip) { | ||
if ((node as Parent).children) { | ||
walk(node as Parent, cb); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export function makeMarkdown(tree: Parent) { | ||
return toMarkdown(tree, { | ||
extensions: [gfmToMarkdown()], | ||
listItemIndent: 'one', | ||
rule: '-', | ||
fence: '`', | ||
fences: true, | ||
}); | ||
} |
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
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