feat: add spoilers to markdown renderer

This commit is contained in:
Xyphyn committed 2023-07-28 13:25:13 -07:00
1 parent 74fd7714ce
commit bec538d386
5 files changed
+69 -27

No files matched your search

+2
View File
@@ -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",
+4 -25
View File
@@ -1,32 +1,7 @@
<script lang="ts">
import { md } from '$lib/components/markdown/markdown'
import { page } from '$app/stores'
export let source: string = ''
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 = `${$page.url.origin}/c/${match.url.slice(1)}`
},
})
</script>
<div class="break-words flex flex-col markdown gap-2 leading-[22px]">
@@ -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;
}
+49
View File
@@ -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 `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`
} else {
// closing tag
return '</details>\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)}`
},
})
+1 -1
View File
@@ -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)
+13 -1
View File
@@ -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"