diff --git a/bun.lockb b/bun.lockb index d6aabcda..8e4836c3 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 3015a15d..5cb9be83 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,12 @@ "autoprefixer": "^10.4.16", "jsdom": "^22.1.0", "lemmy-js-client": "latest", + "linkify-it": "^5.0.0", "markdown-it": "^14.1.0", "markdown-it-container": "^3.0.0", "markdown-it-sub": "^1.0.0", "markdown-it-sup": "^1.0.0", + "marked-linkify-it": "^3.1.10", "mono-svelte": "^1.5.14", "nprogress": "^0.2.0", "postcss": "^8.4.30", @@ -30,6 +32,7 @@ "svelte-check": "^3.0.1", "svelte-hero-icons": "^5.0.0", "svelte-infinite-scroll": "^2.0.1", + "svelte-markdown": "^0.4.1", "sveltekit-i18n": "^2.4.2", "tailwindcss": "^3.3.3", "tslib": "^2.4.1", diff --git a/src/lib/components/markdown/Markdown.svelte b/src/lib/components/markdown/Markdown.svelte index fa814072..bbf5cccf 100644 --- a/src/lib/components/markdown/Markdown.svelte +++ b/src/lib/components/markdown/Markdown.svelte @@ -1,55 +1,19 @@
- {@html rendered} + +
- - diff --git a/src/lib/components/markdown/markdown.ts b/src/lib/components/markdown/markdown.ts index bd598da4..9400ae5d 100644 --- a/src/lib/components/markdown/markdown.ts +++ b/src/lib/components/markdown/markdown.ts @@ -1,3 +1,4 @@ +import LinkifyIt from 'linkify-it' import MarkdownIt from 'markdown-it' import markdown_it_container from 'markdown-it-container' // @ts-ignore @@ -5,125 +6,51 @@ import markdown_it_sub from 'markdown-it-sub' // @ts-ignore import markdown_it_sup from 'markdown-it-sup' -export const md = new MarkdownIt({ - html: false, - linkify: true, -}) - .use(markdown_it_container, 'spoiler', { - validate: (params: string) => { - return params.trim().match(/^spoiler+(.*)/) - }, +// export const md = new MarkdownIt({ +// html: false, +// linkify: true, +// }) +// .use(markdown_it_container, 'spoiler', { +// validate: (params: string) => { +// return params.trim().match(/^spoiler+(.*)/) +// }, - render: (tokens: any, idx: any) => { - var m = tokens[idx].info.trim().match(/^spoiler+(.*)/) +// render: (tokens: any, idx: any) => { +// var m = tokens[idx].info.trim().match(/^spoiler+(.*)/) - if (tokens[idx].nesting === 1) { - // opening tag - return `
${md.utils.escapeHtml(m[1])} \n` - } else { - // closing tag - return '
\n' - } - }, - }) - .use(markdown_it_sub) - .use(markdown_it_sup) +// if (tokens[idx].nesting === 1) { +// // opening tag +// return `
${md.utils.escapeHtml(m[1])} \n` +// } else { +// // closing tag +// return '
\n' +// } +// }, +// }) +// .use(markdown_it_sub) +// .use(markdown_it_sup) -export const mdInline: MarkdownIt = new MarkdownIt('zero') - .enable(['emphasis', 'backticks', 'strikethrough', 'link', 'list', 'heading']) - .use(markdown_it_container, 'spoiler', { - validate: (params: string) => { - return params.trim().match(/^spoiler+(.*)/) - }, +// export const mdInline: MarkdownIt = new MarkdownIt('zero') +// .enable(['emphasis', 'backticks', 'strikethrough', 'link', 'list', 'heading']) +// .use(markdown_it_container, 'spoiler', { +// validate: (params: string) => { +// return params.trim().match(/^spoiler+(.*)/) +// }, - render: (tokens: any, idx: any) => { - var m = tokens[idx].info.trim().match(/^spoiler+(.*)/) +// render: (tokens: any, idx: any) => { +// var m = tokens[idx].info.trim().match(/^spoiler+(.*)/) - if (tokens[idx].nesting === 1) { - // opening tag - return `
${md.utils.escapeHtml(m[1])} \n` - } else { - // closing tag - return '
\n' - } - }, - }) - .use(markdown_it_sub) - .use(markdown_it_sup) - -const communityLinks = { - validate: function (text: any, pos: any, self: any) { - var tail = text.slice(pos) - - if (!self.re.community) { - self.re.community = new RegExp( - /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i - ) - } - if (self.re.community.test(tail)) { - // Linkifier allows punctuation chars before prefix, - // but we additionally disable `@` ("@@mention" is invalid) - if (pos >= 2 && tail[pos - 2] === '!') { - return false - } - return tail.match(self.re.community)![0].length - } - return 0 - }, - normalize: function (match: any) { - let prefix = match.url - prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1) - - match.url = `/c/${prefix}` - }, -} - -const userLinks = { - validate: function (text: any, pos: any, self: any) { - var tail = text.slice(pos) - - if (!self.re.user) { - self.re.user = new RegExp( - /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i - ) - } - if (self.re.user.test(tail)) { - // Linkifier allows punctuation chars before prefix, - // but we additionally disable `@` ("@@mention" is invalid) - if (pos >= 2 && tail[pos - 2] === '!') { - return false - } - return tail.match(self.re.user)![0].length - } - return 0 - }, - normalize: function (match: any) { - let prefix = match.url - prefix = prefix.startsWith('u/') ? prefix.slice(2) : prefix.slice(1) - - match.url = `/u/${prefix}` - }, -} - -md.linkify.add('!', { - validate: communityLinks.validate, - normalize: communityLinks.normalize, -}) - -md.linkify.add('c/', { - validate: communityLinks.validate, - normalize: communityLinks.normalize, -}) - -md.linkify.add('@', { - validate: userLinks.validate, - normalize: userLinks.normalize, -}) - -md.linkify.add('u/', { - validate: userLinks.validate, - normalize: userLinks.normalize, -}) +// if (tokens[idx].nesting === 1) { +// // opening tag +// return `
${md.utils.escapeHtml(m[1])} \n` +// } else { +// // closing tag +// return '
\n' +// } +// }, +// }) +// .use(markdown_it_sub) +// .use(markdown_it_sup) const regexes = { post: /^https:\/\/([a-zA-Z0-9.-]+)\/post\/(\d+)$/i, diff --git a/src/lib/components/markdown/renderers/Heading.svelte b/src/lib/components/markdown/renderers/Heading.svelte new file mode 100644 index 00000000..32559e29 --- /dev/null +++ b/src/lib/components/markdown/renderers/Heading.svelte @@ -0,0 +1,23 @@ + + +test +{#if depth === 1} +

+{:else if depth === 2} +

+{:else if depth === 3} +

+{:else if depth === 4} +

+{:else if depth === 5} +
+{:else if depth === 6} +
+{:else} + {raw} +{/if} diff --git a/src/lib/components/markdown/renderers/MdImage.svelte b/src/lib/components/markdown/renderers/MdImage.svelte new file mode 100644 index 00000000..909d08f9 --- /dev/null +++ b/src/lib/components/markdown/renderers/MdImage.svelte @@ -0,0 +1,8 @@ + + +hi +{text} diff --git a/src/lib/components/markdown/renderers/plugins.ts b/src/lib/components/markdown/renderers/plugins.ts new file mode 100644 index 00000000..fcfdef68 --- /dev/null +++ b/src/lib/components/markdown/renderers/plugins.ts @@ -0,0 +1,88 @@ +import { marked } from 'marked' +import markedLinkifyIt from 'marked-linkify-it' + +export const spoilerPlugin = { + name: 'spoiler', + level: 'block', + start(src: string) { + return src.match(/^:::/)?.index + }, + tokenizer(src: string, tokens: marked.TokensList) { + const rule = /^::: spoiler (.+)\n([\s\S]*?)\n:::/ + const match = rule.exec(src) + if (match) { + return { + type: 'spoiler', + raw: match[0], + title: match[1].trim(), + text: match[2].trim(), + } + } + }, + renderer(token: any) { + return ` +
+ ${marked.parseInline(token.title)} + ${marked.parse(token.text)} +
+ ` + }, +} + +export const linkify = markedLinkifyIt( + { + '!': { + validate: function (text: any, pos: any, self: any) { + var tail = text.slice(pos) + + if (!self.re.community) { + self.re.community = new RegExp( + /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i + ) + } + if (self.re.community.test(tail)) { + // Linkifier allows punctuation chars before prefix, + // but we additionally disable `@` ("@@mention" is invalid) + if (pos >= 2 && tail[pos - 2] === '!') { + return false + } + return tail.match(self.re.community)![0].length + } + return 0 + }, + normalize: function (match: any) { + let prefix = match.url + prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1) + + match.url = `/c/${prefix}` + }, + }, + '@': { + validate: function (text: any, pos: any, self: any) { + var tail = text.slice(pos) + + if (!self.re.user) { + self.re.user = new RegExp( + /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i + ) + } + if (self.re.user.test(tail)) { + // Linkifier allows punctuation chars before prefix, + // but we additionally disable `@` ("@@mention" is invalid) + if (pos >= 2 && tail[pos - 2] === '!') { + return false + } + return tail.match(self.re.user)![0].length + } + return 0 + }, + normalize: function (match: any) { + let prefix = match.url + prefix = prefix.startsWith('u/') ? prefix.slice(2) : prefix.slice(1) + + match.url = `/u/${prefix}` + }, + }, + }, + {} +)