feat: add spoilers to markdown renderer
This commit is contained in:
1 parent
74fd7714ce
commit
bec538d386
5 files changed
+69
-27
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)}`
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user