fix: spoiler support
This commit is contained in:
1 parent
687bcde97b
commit
14789bd5ba
7 files changed
+85
-60
No files matched your search
@@ -1,6 +1,55 @@
|
|||||||
|
<script lang="ts" context="module">
|
||||||
|
import { marked } from 'marked'
|
||||||
|
marked.setOptions({
|
||||||
|
pedantic: false,
|
||||||
|
mangle: false,
|
||||||
|
headerIds: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
marked.use(linkify, {
|
||||||
|
extensions: [
|
||||||
|
containerExtension((params: any) => {
|
||||||
|
if (params.type == 'spoiler') {
|
||||||
|
return {
|
||||||
|
type: 'spoiler',
|
||||||
|
raw: params.raw,
|
||||||
|
title: params.options,
|
||||||
|
tokens: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
export const renderers = {
|
||||||
|
heading: MdHeading,
|
||||||
|
image: MdImage,
|
||||||
|
link: MdLink,
|
||||||
|
blockquote: MdQuote,
|
||||||
|
hr: MdHr,
|
||||||
|
html: MdHtml,
|
||||||
|
text: MdText,
|
||||||
|
code: MdCode,
|
||||||
|
list: MdList,
|
||||||
|
del: MdText,
|
||||||
|
// @ts-ignore
|
||||||
|
spoiler: MdSpoiler,
|
||||||
|
table: MdTable,
|
||||||
|
tablebody: MdTableBody,
|
||||||
|
tablecell: MdTableCell,
|
||||||
|
tablehead: MdTableHead,
|
||||||
|
tablerow: MdTableRow,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const options = {
|
||||||
|
mangle: false,
|
||||||
|
headerIds: false,
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import SvelteMarkdown from 'svelte-markdown'
|
import SvelteMarkdown from 'svelte-markdown'
|
||||||
import { marked } from 'marked'
|
|
||||||
import { linkify } from './renderers/plugins'
|
import { linkify } from './renderers/plugins'
|
||||||
import {
|
import {
|
||||||
MdCode,
|
MdCode,
|
||||||
@@ -26,60 +75,19 @@
|
|||||||
export let inline: boolean = false
|
export let inline: boolean = false
|
||||||
export let noStyle: boolean = false
|
export let noStyle: boolean = false
|
||||||
|
|
||||||
let div: HTMLElement
|
|
||||||
|
|
||||||
marked.use(linkify, {
|
|
||||||
extensions: [
|
|
||||||
containerExtension((params: any) => {
|
|
||||||
if (params.type == 'spoiler') {
|
|
||||||
return {
|
|
||||||
type: 'spoiler',
|
|
||||||
raw: params.raw,
|
|
||||||
title: params.options,
|
|
||||||
tokens: [],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
})
|
|
||||||
|
|
||||||
$: tokens = marked.lexer(source)
|
$: tokens = marked.lexer(source)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
bind:this={div}
|
|
||||||
class="{noStyle
|
class="{noStyle
|
||||||
? ''
|
? ''
|
||||||
: 'break-words flex flex-col markdown gap-2 leading-[1.5]'} {$$props.class}"
|
: 'break-words flex flex-col gap-2 leading-[1.5]'} {$$props.class}"
|
||||||
style={$$props.style}
|
style={$$props.style}
|
||||||
>
|
>
|
||||||
<!-- {@html rendered} -->
|
|
||||||
<SvelteMarkdown
|
<SvelteMarkdown
|
||||||
bind:source={tokens}
|
bind:source={tokens}
|
||||||
renderers={{
|
{renderers}
|
||||||
heading: MdHeading,
|
{options}
|
||||||
image: MdImage,
|
isInline={inline || undefined}
|
||||||
link: MdLink,
|
|
||||||
blockquote: MdQuote,
|
|
||||||
hr: MdHr,
|
|
||||||
code: MdCode,
|
|
||||||
html: MdHtml,
|
|
||||||
text: MdText,
|
|
||||||
list: MdList,
|
|
||||||
del: MdText,
|
|
||||||
// @ts-ignore
|
|
||||||
spoiler: MdSpoiler,
|
|
||||||
table: MdTable,
|
|
||||||
tablebody: MdTableBody,
|
|
||||||
tablecell: MdTableCell,
|
|
||||||
tablehead: MdTableHead,
|
|
||||||
tablerow: MdTableRow,
|
|
||||||
paragraph: MdParagraph,
|
|
||||||
}}
|
|
||||||
options={{
|
|
||||||
pedantic: true,
|
|
||||||
}}
|
|
||||||
isInline={inline}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
code: string
|
code: string
|
||||||
lang?: string
|
lang?: string
|
||||||
} {
|
} {
|
||||||
console.log(raw)
|
|
||||||
const match = /^```(.*)/gi.exec(src)
|
const match = /^```(.*)/gi.exec(src)
|
||||||
|
|
||||||
if (match?.[1]) {
|
if (match?.[1]) {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
class="max-w-full h-auto rounded-2xl p-2 border border-slate-200 dark:border-zinc-800
|
class="max-w-xl h-auto rounded-2xl p-2 border border-slate-200 dark:border-zinc-800
|
||||||
inline-block"
|
inline-block"
|
||||||
>
|
>
|
||||||
{#if type == 'video' || type == 'embed' || type == 'iframe'}
|
{#if type == 'video' || type == 'embed' || type == 'iframe'}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
<p>
|
<p class="leading-[1.5]">
|
||||||
<slot />
|
<slot />
|
||||||
</p>
|
</p>
|
||||||
@@ -1,17 +1,37 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { marked } from 'marked'
|
import { marked } from 'marked'
|
||||||
|
import SvelteMarkdown from 'svelte-markdown'
|
||||||
|
import { options, renderers } from '../Markdown.svelte'
|
||||||
|
import { Disclosure } from 'mono-svelte'
|
||||||
|
import { Icon } from 'svelte-hero-icons'
|
||||||
|
import Expandable from '$lib/components/ui/Expandable.svelte'
|
||||||
|
|
||||||
export let raw: string
|
export let raw: string
|
||||||
|
|
||||||
function extractTitle(text: string) {
|
function extractTitle(text: string) {
|
||||||
const spoilerRegex = /^::: spoiler (.+)\n([\s\S]+?)\n:::/gm
|
const spoilerRegex = /^::: ?spoiler (.+)\n([\s\S]+?)\n:::/gm
|
||||||
|
const result = spoilerRegex.exec(text)
|
||||||
|
|
||||||
return text.replace(spoilerRegex, function (match, title, content) {
|
if (!result?.[1] || !result?.[2])
|
||||||
return `<details><summary class="cursor-pointer">${marked.parseInline(title)}</summary>${marked.parse(content)}</details>`
|
return {
|
||||||
})
|
title: 'parsing error',
|
||||||
|
content: 'parsing error',
|
||||||
}
|
}
|
||||||
|
|
||||||
$: html = extractTitle(raw)
|
return {
|
||||||
|
title: result[1],
|
||||||
|
content: result[2],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$: data = extractTitle(raw)
|
||||||
|
|
||||||
|
$: console.log(data.content)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{@html html}
|
<Expandable class="border-y border-slate-200 dark:border-zinc-800 py-2">
|
||||||
|
<div slot="title" class="text-left">
|
||||||
|
<SvelteMarkdown source={data.title} {renderers} {options} isInline />
|
||||||
|
</div>
|
||||||
|
<SvelteMarkdown source={data.content} {renderers} {options} />
|
||||||
|
</Expandable>
|
||||||
+2
-2
@@ -2,7 +2,7 @@ export const isImage = (url: string | undefined) => {
|
|||||||
try {
|
try {
|
||||||
if (!url) return false
|
if (!url) return false
|
||||||
|
|
||||||
return /\.(jpeg|jpg|gif|png|svg|bmp|webp)$/i.test(new URL(url).pathname)
|
return /\.(jpeg|jpg|gif|png|svg|bmp|webp)$/i.test(url)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ export const isVideo = (url: string | undefined) => {
|
|||||||
try {
|
try {
|
||||||
if (!url) return false
|
if (!url) return false
|
||||||
|
|
||||||
return /\.(mp4|mov|webm|mkv)$/i.test(new URL(url).pathname)
|
return /\.(mp4|mov|webm|mkv)$/i.test(url)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -234,9 +234,7 @@
|
|||||||
opened
|
opened
|
||||||
/>
|
/>
|
||||||
{#if post.post_view.post.body}
|
{#if post.post_view.post.body}
|
||||||
<div
|
<div class="text-base text-slate-800 dark:text-zinc-300 leading-[1.5]">
|
||||||
class="text-sm text-slate-800 dark:text-zinc-300 rounded-md leading-[22px]"
|
|
||||||
>
|
|
||||||
<Markdown source={post.post_view.post.body} />
|
<Markdown source={post.post_view.post.body} />
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
Reference in new issue
Block a user