diff options
Diffstat (limited to 'source/library/graphql')
| -rw-r--r-- | source/library/graphql/markdown.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/source/library/graphql/markdown.ts b/source/library/graphql/markdown.ts new file mode 100644 index 0000000..a2ca6f7 --- /dev/null +++ b/source/library/graphql/markdown.ts @@ -0,0 +1,45 @@ + + + +/*** EXPORT ------------------------------------------- ***/ + +export function markdown(input: string): string { + const inlineCode: string[] = []; + const inlineCodePattern = /`([^`]+)`/g; + const refPattern = /\s*\[(\w+)\]\s+<([^>]+)>$/gm; + const refs = new Map<string, string>(); + + let processed = input.replace(inlineCodePattern, (_match, content) => { + const index = inlineCode.length; + inlineCode.push(content); + return `<!--INLINE:CODE:${index}-->`; + }); + + for (const match of processed.matchAll(refPattern)) { + const [, ref, url] = match; + refs.set(ref, url); + } + + processed = processed + .replace(/\[([^\]]+)\]\(([^)]+)\)/g, `<a href="$2" target="_blank">$1</a>`) + .replace(/(\*\*|__)(.*?)\1/g, `<strong style="white-space: nowrap;">$2</strong>`) + .replace(/'/g, "’") + .replace(/(\.\.\.)/g, "…") + .replace(/---/g, "<hr/>"); + + for (const block in inlineCode) { + processed = processed.replace(`<!--INLINE:CODE:${block}-->`, `<code>${escapeHtml(inlineCode[block])}</code>`); + } + + return processed.trimEnd(); +} + +/*** HELPER ------------------------------------------- ***/ + +function escapeHtml(str: string): string { + return str + .replace(/&/g, "&") + .replace(/</g, "<") + .replace(/>/g, ">") + .replace(/"/g, """); +} |