From ebfdc0ffe39eb1538b626c582fdbd763669ab9c2 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 27 May 2023 17:41:22 +0200 Subject: [PATCH] add link without preview --- packages/client/src/scripts/markdown.ts | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/client/src/scripts/markdown.ts b/packages/client/src/scripts/markdown.ts index 0a746b66d..4a54baddd 100644 --- a/packages/client/src/scripts/markdown.ts +++ b/packages/client/src/scripts/markdown.ts @@ -314,6 +314,40 @@ marked.use({ return `${this.parser.parseInline(token.tokens)}`; }, }, + { + name: 'silent-link', + level: 'inline', + start(src) { return src.startsWith('?['), + tokenizer(src) { + // a markdown [title](link) with prepended question mark to signify + // that no link preview should be shown for this link + + // regular expression abridged from marked.js internals + const match = src.match(/^\?\[((?:\\.|[^\[\]\\])*)\]\(\s*(?:<((?:\\.|[^\n<>\\])+)>|([^\s\x00-\x1f]*))\s*\)/); + if (!match) return; + + try { + // match group 2 when enclosed in <> and match group 3 otherwise + const href = new URL(match[2] ?? match[3]).href; + } catch { + // invalid URL + return; + } + + const token = { + type: 'silent-link', + raw: match[0], + tokens: [], + href, + }; + this.lexer.inline(match[1], token.tokens); + return token; + }, + renderer(token) { + const secureUrl = encodeURI(token.href).replaceAll("%25", "%"); + return `${this.parser.parseInline(token.tokens)}`; + }, + }, ], });