diff options
| author | netop://ウィビ <paul@webb.page> | 2026-04-26 20:18:30 -0700 |
|---|---|---|
| committer | netop://ウィビ <paul@webb.page> | 2026-04-26 20:18:30 -0700 |
| commit | 3c06c95f396b6e911076bc3291d5855ed01b5caa (patch) | |
| tree | 17cd218339c52fbeee93d931303b04a3ff294f8b /source/library/graphql/markdown.ts | |
| parent | f059d97ab7f6d74d61139ac698cb871be7cb632e (diff) | |
| download | graphiql-3c06c95f396b6e911076bc3291d5855ed01b5caa.tar.gz graphiql-3c06c95f396b6e911076bc3291d5855ed01b5caa.zip | |
cleanup and ready for launch
Diffstat (limited to 'source/library/graphql/markdown.ts')
| -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, """); +} |