Skip to main content
Module

x/markdown/src/helpers.ts

Deno Markdown module forked from https://github.com/ts-stack/markdown/tree/bb47aa8e625e89e6aa84f49a98536a3089dee831
Latest
File
/** * @license * * Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed) * https://github.com/chjj/marked * * Copyright (c) 2018, Костя Третяк. (MIT Licensed) * https://github.com/ts-stack/markdown */
import { Replacements } from "./interfaces.ts";
const escapeTest = /[&<>"']/;const escapeReplace = /[&<>"']/g;const replacements: Replacements = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", // tslint:disable-next-line:quotemark "'": "&#39;",};
const escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;const escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
export function escape(html: string, encode?: boolean) { if (encode) { if (escapeTest.test(html)) { return html.replace(escapeReplace, (ch: string) => replacements[ch]); } } else { if (escapeTestNoEncode.test(html)) { return html.replace( escapeReplaceNoEncode, (ch: string) => replacements[ch], ); } }
return html;}
export function unescape(html: string) { // Explicitly match decimal, hex, and named HTML entities return html.replace( /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi, function (_, n) { n = n.toLowerCase();
if (n === "colon") { return ":"; }
if (n.charAt(0) === "#") { return n.charAt(1) === "x" ? String.fromCharCode(parseInt(n.substring(2), 16)) : String.fromCharCode(+n.substring(1)); }
return ""; }, );}