blob: 635ab3b540e00eb8bb4b0f7bcfb4b081fc65cb1d (
plain) (
blame)
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
|
import type { Token } from 'micromark/dist/shared-types';
import type { MicromarkExtension, MicromarkExtensionContext } from '../../@types/micromark';
const characterReferences = {
'"': 'quot',
'&': 'amp',
'<': 'lt',
'>': 'gt',
'{': 'lbrace',
'}': 'rbrace',
};
type EncodedChars = '"' | '&' | '<' | '>' | '{' | '}';
/** Encode HTML entity */
function encode(value: string): string {
return value.replace(/["&<>{}]/g, (raw: string) => {
return '&' + characterReferences[raw as EncodedChars] + ';';
});
}
/** Encode Markdown node */
function encodeToken(this: MicromarkExtensionContext) {
const token: Token = arguments[0];
const value = this.sliceSerialize(token);
this.raw(encode(value));
}
const plugin: MicromarkExtension = {
exit: {
codeTextData: encodeToken,
codeFlowValue: encodeToken,
},
};
export { plugin as encodeMarkdown };
|