summaryrefslogtreecommitdiff
path: root/src/markdown-encode.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/markdown-encode.ts')
-rw-r--r--src/markdown-encode.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/markdown-encode.ts b/src/markdown-encode.ts
new file mode 100644
index 000000000..0719aaa32
--- /dev/null
+++ b/src/markdown-encode.ts
@@ -0,0 +1,34 @@
+import type { HtmlExtension, Token } 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] + ';'
+ });
+}
+
+const plugin: HtmlExtension = {
+ exit: {
+ codeFlowValue() {
+ 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));
+ }
+ }
+};
+
+export {
+ plugin as default
+}; \ No newline at end of file