From 3c06c95f396b6e911076bc3291d5855ed01b5caa Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Sun, 26 Apr 2026 20:18:30 -0700 Subject: cleanup and ready for launch --- source/library/graphql/markdown.ts | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 source/library/graphql/markdown.ts (limited to 'source/library/graphql/markdown.ts') 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(); + + let processed = input.replace(inlineCodePattern, (_match, content) => { + const index = inlineCode.length; + inlineCode.push(content); + return ``; + }); + + for (const match of processed.matchAll(refPattern)) { + const [, ref, url] = match; + refs.set(ref, url); + } + + processed = processed + .replace(/\[([^\]]+)\]\(([^)]+)\)/g, `$1`) + .replace(/(\*\*|__)(.*?)\1/g, `$2`) + .replace(/'/g, "’") + .replace(/(\.\.\.)/g, "…") + .replace(/---/g, "
"); + + for (const block in inlineCode) { + processed = processed.replace(``, `${escapeHtml(inlineCode[block])}`); + } + + return processed.trimEnd(); +} + +/*** HELPER ------------------------------------------- ***/ + +function escapeHtml(str: string): string { + return str + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """); +} -- cgit v1.2.3