diff options
Diffstat (limited to 'src/micromark-encode.ts')
-rw-r--r-- | src/micromark-encode.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/micromark-encode.ts b/src/micromark-encode.ts new file mode 100644 index 000000000..d205d13e3 --- /dev/null +++ b/src/micromark-encode.ts @@ -0,0 +1,35 @@ +import type { HtmlExtension, Token, Tokenize } from 'micromark/dist/shared-types'; + +const characterReferences = { + '"': 'quot', + '&': 'amp', + '<': 'lt', + '>': 'gt', + '{': 'lbrace', + '}': 'rbrace', +}; + +type EncodedChars = '"' | '&' | '<' | '>' | '{' | '}'; + +function encode(value: string): string { + return value.replace(/["&<>{}]/g, (raw: string) => { + return '&' + characterReferences[raw as EncodedChars] + ';'; + }); +} + +function encodeToken(this: Record<string, () => void>) { + const token: Token = arguments[0]; + const serialize = (this.sliceSerialize as unknown) as (t: Token) => string; + const raw = (this.raw as unknown) as (s: string) => void; + const value = serialize(token); + raw(encode(value)); +} + +const plugin: HtmlExtension = { + exit: { + codeTextData: encodeToken, + codeFlowValue: encodeToken, + }, +}; + +export { plugin as encodeMarkdown };
\ No newline at end of file |