From bec538d38694344207910acf6cc535403613c2e8 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Fri, 28 Jul 2023 13:25:13 -0700 Subject: [PATCH] feat: add spoilers to markdown renderer --- package.json | 2 + src/lib/components/markdown/Markdown.svelte | 29 ++---------- src/lib/components/markdown/markdown.ts | 49 +++++++++++++++++++++ src/lib/lemmy.ts | 2 +- yarn.lock | 14 +++++- 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index a18de3de..d8415937 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,11 @@ "@sveltejs/adapter-node": "^1.3.1", "@sveltejs/kit": "^1.5.0", "@types/markdown-it": "^12.2.3", + "@types/markdown-it-container": "^2.0.6", "@types/nprogress": "^0.2.0", "autoprefixer": "^10.4.14", "jsdom": "^22.1.0", + "markdown-it-container": "^3.0.0", "nprogress": "^0.2.0", "postcss": "^8.4.24", "svelte": "^3.54.0", diff --git a/src/lib/components/markdown/Markdown.svelte b/src/lib/components/markdown/Markdown.svelte index 5ad83319..b0d49680 100644 --- a/src/lib/components/markdown/Markdown.svelte +++ b/src/lib/components/markdown/Markdown.svelte @@ -1,32 +1,7 @@
@@ -44,6 +19,10 @@ @apply text-xl font-bold; } + .markdown :global(details) { + @apply cursor-pointer; + } + .markdown :global(hr) { @apply w-[90%] mx-auto my-4 opacity-50; } diff --git a/src/lib/components/markdown/markdown.ts b/src/lib/components/markdown/markdown.ts index 1550c5c1..c93393a5 100644 --- a/src/lib/components/markdown/markdown.ts +++ b/src/lib/components/markdown/markdown.ts @@ -1,6 +1,55 @@ import MarkdownIt from 'markdown-it' +import markdown_it_container from 'markdown-it-container' 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) => { + console.log('test') + 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' + } + }, +}) + +export const mdLimited: MarkdownIt = new MarkdownIt('zero').enable([ + 'emphasis', + 'backticks', + 'strikethrough', + 'link', +]) + +md.linkify.add('!', { + validate: function (text, pos, self) { + var tail = text.slice(pos) + + if (!self.re.community) { + self.re.community = new RegExp( + /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,6})/ + ) + } + 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) { + match.url = `/c/${match.url.slice(1)}` + }, }) diff --git a/src/lib/lemmy.ts b/src/lib/lemmy.ts index bfc95ab1..25fa652a 100644 --- a/src/lib/lemmy.ts +++ b/src/lib/lemmy.ts @@ -6,7 +6,7 @@ import { env } from '$env/dynamic/public' import { isModOfAny } from '$lib/components/lemmy/moderation/moderation.js' export const LINKED_INSTANCE_URL = env.PUBLIC_INSTANCE_URL -export const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemm.ee' +export const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemme' export let instance = writable(DEFAULT_INSTANCE_URL) export let corsSupported = writable(true) diff --git a/yarn.lock b/yarn.lock index bf69a189..7394a8c8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -355,7 +355,14 @@ resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-3.0.2.tgz#fd2cd2edbaa7eaac7e7f3c1748b52a19143846c9" integrity sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA== -"@types/markdown-it@^12.2.3": +"@types/markdown-it-container@^2.0.6": + version "2.0.6" + resolved "https://registry.yarnpkg.com/@types/markdown-it-container/-/markdown-it-container-2.0.6.tgz#acf0a02dff2add8df9d68e7b033b7165ebe37ca3" + integrity sha512-euUVSCx2+tGV8BlI7yA7AXmNsLzYCoUbg/O2jJ0wzV/0oxKbmd5RJZI2map8cuEKgmjNy13ndFWRZZO+6rl0GA== + dependencies: + "@types/markdown-it" "*" + +"@types/markdown-it@*", "@types/markdown-it@^12.2.3": version "12.2.3" resolved "https://registry.yarnpkg.com/@types/markdown-it/-/markdown-it-12.2.3.tgz#0d6f6e5e413f8daaa26522904597be3d6cd93b51" integrity sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ== @@ -1121,6 +1128,11 @@ magic-string@^0.30.0, magic-string@^0.30.1: dependencies: "@jridgewell/sourcemap-codec" "^1.4.15" +markdown-it-container@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/markdown-it-container/-/markdown-it-container-3.0.0.tgz#1d19b06040a020f9a827577bb7dbf67aa5de9a5b" + integrity sha512-y6oKTq4BB9OQuY/KLfk/O3ysFhB3IMYoIWhGJEidXt1NQFocFK2sA2t0NYZAMyMShAGL6x5OPIbrmXPIqaN9rw== + markdown-it@^13.0.1: version "13.0.1" resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-13.0.1.tgz#c6ecc431cacf1a5da531423fc6a42807814af430"