feat: switch to svelte-markdown
This commit is contained in:
1 parent
2b642b0926
commit
98a899c596
27 files changed
+510
-139
No files matched your search
@@ -79,9 +79,9 @@ export const mediaType = (url?: string, view: View = 'cozy'): MediaType => {
|
|||||||
|
|
||||||
return 'none'
|
return 'none'
|
||||||
}
|
}
|
||||||
export const iframeType = (post: Post): IframeType => {
|
export const iframeType = (url: string): IframeType => {
|
||||||
if (isVideo(post.url)) return 'video'
|
if (isVideo(url)) return 'video'
|
||||||
if (isYoutubeLink(post.url)) return 'youtube'
|
if (isYoutubeLink(url)) return 'youtube'
|
||||||
return 'none'
|
return 'none'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,7 @@
|
|||||||
export let thumbnail: string | undefined = undefined
|
export let thumbnail: string | undefined = undefined
|
||||||
export let url: string
|
export let url: string
|
||||||
export let opened = !$userSettings.embeds.clickToView
|
export let opened = !$userSettings.embeds.clickToView
|
||||||
|
export let autoplay: boolean = $userSettings.embeds.clickToView
|
||||||
|
|
||||||
const urlToEmbed = (inputUrl: string) => {
|
const urlToEmbed = (inputUrl: string) => {
|
||||||
if (type == 'video') {
|
if (type == 'video') {
|
||||||
@@ -52,11 +53,10 @@
|
|||||||
|
|
||||||
const videoID = youtubeVideoID(inputUrl)
|
const videoID = youtubeVideoID(inputUrl)
|
||||||
|
|
||||||
if ($userSettings.embeds.clickToView)
|
if (autoplay) url.searchParams.set('autoplay', '1')
|
||||||
url.searchParams.set('autoplay', '1')
|
|
||||||
|
|
||||||
return `https://${youtubeDomain(
|
return `https://${youtubeDomain(
|
||||||
$userSettings.embeds.youtube,
|
$userSettings.embeds.youtube
|
||||||
)}/embed/${videoID}?${url.searchParams.toString()}`
|
)}/embed/${videoID}?${url.searchParams.toString()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const typeData = (
|
const typeData = (
|
||||||
type: IframeType,
|
type: IframeType
|
||||||
): {
|
): {
|
||||||
icon: IconSource
|
icon: IconSource
|
||||||
text: string
|
text: string
|
||||||
@@ -102,11 +102,7 @@
|
|||||||
{#if opened}
|
{#if opened}
|
||||||
{#if type == 'video'}
|
{#if type == 'video'}
|
||||||
<!-- svelte-ignore a11y-media-has-caption -->
|
<!-- svelte-ignore a11y-media-has-caption -->
|
||||||
<video
|
<video {autoplay} controls class="rounded-xl aspect-video">
|
||||||
autoplay={$userSettings.embeds.clickToView}
|
|
||||||
controls
|
|
||||||
class="rounded-xl aspect-video"
|
|
||||||
>
|
|
||||||
<source src={url} />
|
<source src={url} />
|
||||||
</video>
|
</video>
|
||||||
{:else}
|
{:else}
|
||||||
@@ -116,7 +112,7 @@
|
|||||||
frameborder="0"
|
frameborder="0"
|
||||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||||
allowfullscreen
|
allowfullscreen
|
||||||
class="aspect-video rounded-xl dark:bg-zinc-900"
|
class="aspect-video rounded-xl dark:bg-zinc-900 {$$props.class ?? ''}"
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
{:else}
|
{:else}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@
|
|||||||
{:else if (type == 'iframe' || type == 'video') && (view == 'cozy' || view == 'card') && post.url}
|
{:else if (type == 'iframe' || type == 'video') && (view == 'cozy' || view == 'card') && post.url}
|
||||||
<PostIframe
|
<PostIframe
|
||||||
thumbnail={post.thumbnail_url}
|
thumbnail={post.thumbnail_url}
|
||||||
type={iframeType(post)}
|
type={iframeType(post.url)}
|
||||||
url={post.url}
|
url={post.url}
|
||||||
{opened}
|
{opened}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,9 +1,26 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import SvelteMarkdown from 'svelte-markdown'
|
import SvelteMarkdown from 'svelte-markdown'
|
||||||
import { marked } from 'marked'
|
import { marked } from 'marked'
|
||||||
import { linkify, spoilerPlugin } from './renderers/plugins'
|
import { linkify } from './renderers/plugins'
|
||||||
import Heading from './renderers/Heading.svelte'
|
import {
|
||||||
import MdImage from './renderers/MdImage.svelte'
|
MdCode,
|
||||||
|
MdHeading,
|
||||||
|
MdHr,
|
||||||
|
MdHtml,
|
||||||
|
MdImage,
|
||||||
|
MdLink,
|
||||||
|
MdQuote,
|
||||||
|
MdText,
|
||||||
|
} from './renderers'
|
||||||
|
import MdList from './renderers/MdList.svelte'
|
||||||
|
import containerExtension from './renderers/spoiler/spoiler'
|
||||||
|
import MdSpoiler from './renderers/MdSpoiler.svelte'
|
||||||
|
import MdTable from './renderers/table/MdTable.svelte'
|
||||||
|
import MdTableBody from './renderers/table/MdTableBody.svelte'
|
||||||
|
import MdTableCell from './renderers/table/MdTableCell.svelte'
|
||||||
|
import MdTableHead from './renderers/table/MdTableHead.svelte'
|
||||||
|
import MdTableRow from './renderers/table/MdTableRow.svelte'
|
||||||
|
import MdParagraph from './renderers/MdParagraph.svelte'
|
||||||
|
|
||||||
export let source: string = ''
|
export let source: string = ''
|
||||||
export let inline: boolean = false
|
export let inline: boolean = false
|
||||||
@@ -11,9 +28,23 @@
|
|||||||
|
|
||||||
let div: HTMLElement
|
let div: HTMLElement
|
||||||
|
|
||||||
marked.use(linkify)
|
marked.use(linkify, {
|
||||||
|
extensions: [
|
||||||
|
containerExtension((params: any) => {
|
||||||
|
if (params.type == 'spoiler') {
|
||||||
|
return {
|
||||||
|
type: 'spoiler',
|
||||||
|
raw: params.raw,
|
||||||
|
title: params.options,
|
||||||
|
tokens: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
$: output = marked.parse(source)
|
$: tokens = marked.lexer(source)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
@@ -25,7 +56,29 @@
|
|||||||
>
|
>
|
||||||
<!-- {@html rendered} -->
|
<!-- {@html rendered} -->
|
||||||
<SvelteMarkdown
|
<SvelteMarkdown
|
||||||
source={output}
|
bind:source={tokens}
|
||||||
renderers={{ heading: Heading, image: MdImage }}
|
renderers={{
|
||||||
|
heading: MdHeading,
|
||||||
|
image: MdImage,
|
||||||
|
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,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
import LinkifyIt from 'linkify-it'
|
|
||||||
import MarkdownIt from 'markdown-it'
|
|
||||||
import markdown_it_container from 'markdown-it-container'
|
|
||||||
// @ts-ignore
|
|
||||||
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+(.*)/)
|
|
||||||
// },
|
|
||||||
|
|
||||||
// render: (tokens: any, idx: any) => {
|
|
||||||
// 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'
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
// .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+(.*)/)
|
|
||||||
// },
|
|
||||||
|
|
||||||
// render: (tokens: any, idx: any) => {
|
|
||||||
// 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'
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
// .use(markdown_it_sub)
|
|
||||||
// .use(markdown_it_sup)
|
|
||||||
|
|
||||||
const regexes = {
|
|
||||||
post: /^https:\/\/([a-zA-Z0-9.-]+)\/post\/(\d+)$/i,
|
|
||||||
comment: /^https:\/\/([a-zA-Z0-9.-]+)\/comment\/(\d+)$/i,
|
|
||||||
user: /^https:\/\/([a-zA-Z0-9.-]+)(\/u\/)([a-zA-Z0-9.-_]+)$/i,
|
|
||||||
community: /^https:\/\/([a-zA-Z0-9.-]+)(\/c\/)([a-zA-Z0-9.-_]+)$/i,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert links to photon links
|
|
||||||
*/
|
|
||||||
export const photonify = (link: string) => {
|
|
||||||
if (regexes.post.test(link)) {
|
|
||||||
const match = link.match(regexes.post)
|
|
||||||
if (!match) return
|
|
||||||
return `/post/${match?.[1]}/${match?.[2]}`
|
|
||||||
}
|
|
||||||
if (regexes.comment.test(link)) {
|
|
||||||
const match = link.match(regexes.comment)
|
|
||||||
if (!match) return
|
|
||||||
return `/comment/${match?.[1]}/${match?.[2]}`
|
|
||||||
}
|
|
||||||
if (regexes.user.test(link)) {
|
|
||||||
const match = link.match(regexes.user)
|
|
||||||
if (!match) return
|
|
||||||
return `/u/${match?.[3]}@${match?.[1]}`
|
|
||||||
}
|
|
||||||
if (regexes.community.test(link)) {
|
|
||||||
const match = link.match(regexes.community)
|
|
||||||
if (!match) return
|
|
||||||
return `/c/${match?.[3]}@${match?.[1]}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
<script>
|
|
||||||
import { getContext } from 'svelte'
|
|
||||||
|
|
||||||
export let depth
|
|
||||||
export let raw
|
|
||||||
</script>
|
|
||||||
|
|
||||||
test
|
|
||||||
{#if depth === 1}
|
|
||||||
<h1 class="font-bold text-3xl"><slot></slot></h1>
|
|
||||||
{:else if depth === 2}
|
|
||||||
<h2><slot></slot></h2>
|
|
||||||
{:else if depth === 3}
|
|
||||||
<h3><slot></slot></h3>
|
|
||||||
{:else if depth === 4}
|
|
||||||
<h4><slot></slot></h4>
|
|
||||||
{:else if depth === 5}
|
|
||||||
<h5><slot></slot></h5>
|
|
||||||
{:else if depth === 6}
|
|
||||||
<h6><slot></slot></h6>
|
|
||||||
{:else}
|
|
||||||
{raw}
|
|
||||||
{/if}
|
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { t } from '$lib/translations'
|
||||||
|
import { Button, Material, toast } from 'mono-svelte'
|
||||||
|
import { Icon, ClipboardDocument } from 'svelte-hero-icons'
|
||||||
|
|
||||||
|
export let raw: string
|
||||||
|
|
||||||
|
function parseCodeblock(src: string): {
|
||||||
|
code: string
|
||||||
|
lang?: string
|
||||||
|
} {
|
||||||
|
console.log(raw)
|
||||||
|
const match = /^```(.*)/gi.exec(src)
|
||||||
|
|
||||||
|
if (match?.[1]) {
|
||||||
|
return {
|
||||||
|
code: src.replace(/^```(.*)/gi, '').replaceAll('```', ''),
|
||||||
|
lang: match?.[1],
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return { code: src.replaceAll('```', '') }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$: codeblock = parseCodeblock(raw)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Material
|
||||||
|
padding="none"
|
||||||
|
class="flex flex-col rounded-xl
|
||||||
|
divide-y divide-slate-200 dark:divide-zinc-800 overflow-hidden"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="w-full bg-slate-25 dark:bg-zinc-925 h-9 flex items-center justify-between p-2"
|
||||||
|
>
|
||||||
|
<pre class="code-baseline text-xs"> {codeblock.lang}</pre>
|
||||||
|
<Button
|
||||||
|
size="square-sm"
|
||||||
|
color="tertiary"
|
||||||
|
on:click={() => {
|
||||||
|
navigator?.clipboard?.writeText(codeblock.code)
|
||||||
|
toast({ content: $t('toast.copied') })
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Icon
|
||||||
|
src={ClipboardDocument}
|
||||||
|
size="16"
|
||||||
|
micro
|
||||||
|
class="text-slate-600 dark:text-zinc-400"
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<pre
|
||||||
|
class="code-baseline w-full overflow-x-auto text-xs bg-white dark:bg-zinc-950 px-4">
|
||||||
|
{codeblock.code}
|
||||||
|
</pre>
|
||||||
|
</Material>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export let depth: number = 0
|
||||||
|
export let raw: string | undefined = undefined
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if depth === 1}
|
||||||
|
<h1 class="font-bold text-4xl"><slot></slot></h1>
|
||||||
|
{:else if depth === 2}
|
||||||
|
<h2 class="font-bold text-3xl"><slot></slot></h2>
|
||||||
|
{:else if depth === 3}
|
||||||
|
<h3 class="font-bold text-2xl"><slot></slot></h3>
|
||||||
|
{:else if depth === 4}
|
||||||
|
<h4 class="font-semibold text-xl"><slot></slot></h4>
|
||||||
|
{:else if depth === 5}
|
||||||
|
<h5 class="font-semibold text-lg"><slot></slot></h5>
|
||||||
|
{:else if depth === 6}
|
||||||
|
<h6 class="font-semibold text-base"><slot></slot></h6>
|
||||||
|
{:else}
|
||||||
|
{raw}
|
||||||
|
{/if}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<hr class="border-slate-200 dark:border-zinc-800" />
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export let text: string
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
{text}
|
||||||
|
</p>
|
||||||
@@ -1,8 +1,39 @@
|
|||||||
<script>
|
<script lang="ts">
|
||||||
export let href = ''
|
import { showImage } from '$lib/components/ui/ExpandableImage.svelte'
|
||||||
export let title = undefined
|
import {
|
||||||
export let text = ''
|
iframeType,
|
||||||
|
mediaType,
|
||||||
|
optimizeImageURL,
|
||||||
|
} from '$lib/components/lemmy/post/helpers'
|
||||||
|
import PostIframe from '$lib/components/lemmy/post/media/PostIframe.svelte'
|
||||||
|
|
||||||
|
export let href: string
|
||||||
|
export let title: string | undefined = undefined
|
||||||
|
export let text: string = ''
|
||||||
|
|
||||||
|
$: type = mediaType(href, 'cozy')
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
hi
|
<div
|
||||||
<img src={href} {title} alt={text} />
|
class="max-w-full h-auto rounded-2xl p-2 border border-slate-200 dark:border-zinc-800
|
||||||
|
inline-block"
|
||||||
|
>
|
||||||
|
{#if type == 'video' || type == 'embed' || type == 'iframe'}
|
||||||
|
<PostIframe
|
||||||
|
type={iframeType(href)}
|
||||||
|
url={href}
|
||||||
|
opened={true}
|
||||||
|
autoplay={false}
|
||||||
|
class="w-full"
|
||||||
|
/>
|
||||||
|
{:else}
|
||||||
|
<button on:click={() => showImage(href, text)}>
|
||||||
|
<img
|
||||||
|
src={optimizeImageURL(href, 1024)}
|
||||||
|
{title}
|
||||||
|
alt={text}
|
||||||
|
class="rounded-lg"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { photonify } from './plugins'
|
||||||
|
|
||||||
|
export let href = ''
|
||||||
|
export let title = undefined
|
||||||
|
|
||||||
|
export const parseURL = (href: string) => {
|
||||||
|
try {
|
||||||
|
return new URL(href)
|
||||||
|
} catch (e) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$: photonified = photonify(href)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<a
|
||||||
|
href={photonified ?? href}
|
||||||
|
{title}
|
||||||
|
class="hover:underline text-sky-600 dark:text-sky-500"
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</a>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export let ordered: boolean
|
||||||
|
export let start: number
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if ordered}
|
||||||
|
<ol
|
||||||
|
{start}
|
||||||
|
class="pl-5 list-decimal *:marker:font-medium *:marker:text-primary-900 *:marker:dark:text-primary-100"
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</ol>
|
||||||
|
{:else}
|
||||||
|
<ul
|
||||||
|
class="list-disc pl-4 *:marker:text-primary-900 *:marker:dark:text-primary-100"
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</ul>
|
||||||
|
{/if}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<p>
|
||||||
|
<slot />
|
||||||
|
</p>
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<blockquote class="border-l-2 dark:border-zinc-700 border-slate-300 p-1 px-3">
|
||||||
|
<slot />
|
||||||
|
</blockquote>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Disclosure } from 'mono-svelte'
|
||||||
|
|
||||||
|
export let raw: string
|
||||||
|
|
||||||
|
function extractTitle(text: string) {
|
||||||
|
const spoilerRegex = /^::: spoiler (.+)\n([\s\S]+?)\n:::/gm
|
||||||
|
|
||||||
|
return text.replace(spoilerRegex, function (match, title, content) {
|
||||||
|
return `<details><summary class="cursor-pointer">${title}</summary>${content}</details>`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
$: html = extractTitle(raw)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{@html html}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export let text: string
|
||||||
|
export let raw: string
|
||||||
|
|
||||||
|
console.log(raw)
|
||||||
|
|
||||||
|
function preprocess(src: string): string {
|
||||||
|
// superscript, subscript support
|
||||||
|
src = src
|
||||||
|
.replace(/~([^~]+)~/g, '<sub>$1</sub>')
|
||||||
|
.replace(/\^([^^]+)\^/g, '<sup>$1</sup>')
|
||||||
|
|
||||||
|
return src
|
||||||
|
}
|
||||||
|
|
||||||
|
$: processed = preprocess(raw)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{@html processed}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import MdHeading from './MdHeading.svelte'
|
||||||
|
import MdImage from './MdImage.svelte'
|
||||||
|
import MdLink from './MdLink.svelte'
|
||||||
|
import MdQuote from './MdQuote.svelte'
|
||||||
|
import MdHr from './MdHr.svelte'
|
||||||
|
import MdCode from './MdCode.svelte'
|
||||||
|
import MdHtml from './MdHtml.svelte'
|
||||||
|
import MdText from './MdText.svelte'
|
||||||
|
import MdList from './MdList.svelte'
|
||||||
|
import MdSpoiler from './MdSpoiler.svelte'
|
||||||
|
import MdTable from './table/MdTable.svelte'
|
||||||
|
import MdTableBody from './table/MdTableBody.svelte'
|
||||||
|
import MdTableCell from './table/MdTableCell.svelte'
|
||||||
|
import MdTableHead from './table/MdTableHead.svelte'
|
||||||
|
import MdTableRow from './table/MdTableRow.svelte'
|
||||||
|
import MdParagraph from './MdParagraph.svelte'
|
||||||
|
|
||||||
|
export {
|
||||||
|
MdCode,
|
||||||
|
MdHeading,
|
||||||
|
MdHr,
|
||||||
|
MdHtml,
|
||||||
|
MdImage,
|
||||||
|
MdLink,
|
||||||
|
MdQuote,
|
||||||
|
MdText,
|
||||||
|
MdList,
|
||||||
|
MdSpoiler,
|
||||||
|
MdTable,
|
||||||
|
MdTableBody,
|
||||||
|
MdTableCell,
|
||||||
|
MdTableHead,
|
||||||
|
MdTableRow,
|
||||||
|
MdParagraph,
|
||||||
|
}
|
||||||
@@ -86,3 +86,36 @@ export const linkify = markedLinkifyIt(
|
|||||||
},
|
},
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const regexes = {
|
||||||
|
post: /^https:\/\/([a-zA-Z0-9.-]+)\/post\/(\d+)$/i,
|
||||||
|
comment: /^https:\/\/([a-zA-Z0-9.-]+)\/comment\/(\d+)$/i,
|
||||||
|
user: /^https:\/\/([a-zA-Z0-9.-]+)(\/u\/)([a-zA-Z0-9.-_]+)$/i,
|
||||||
|
community: /^https:\/\/([a-zA-Z0-9.-]+)(\/c\/)([a-zA-Z0-9.-_]+)$/i,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert links to photon links
|
||||||
|
*/
|
||||||
|
export const photonify = (link: string) => {
|
||||||
|
if (regexes.post.test(link)) {
|
||||||
|
const match = link.match(regexes.post)
|
||||||
|
if (!match) return
|
||||||
|
return `/post/${match?.[1]}/${match?.[2]}`
|
||||||
|
}
|
||||||
|
if (regexes.comment.test(link)) {
|
||||||
|
const match = link.match(regexes.comment)
|
||||||
|
if (!match) return
|
||||||
|
return `/comment/${match?.[1]}/${match?.[2]}`
|
||||||
|
}
|
||||||
|
if (regexes.user.test(link)) {
|
||||||
|
const match = link.match(regexes.user)
|
||||||
|
if (!match) return
|
||||||
|
return `/u/${match?.[3]}@${match?.[1]}`
|
||||||
|
}
|
||||||
|
if (regexes.community.test(link)) {
|
||||||
|
const match = link.match(regexes.community)
|
||||||
|
if (!match) return
|
||||||
|
return `/c/${match?.[3]}@${match?.[1]}`
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
import { Lexer } from 'marked'
|
||||||
|
|
||||||
|
export type ContainerOptions = Record<string, boolean | string>
|
||||||
|
|
||||||
|
export type TokenExtractionParameters = {
|
||||||
|
/**
|
||||||
|
* The type of container
|
||||||
|
*/
|
||||||
|
type: string
|
||||||
|
/**
|
||||||
|
* The content inside the container
|
||||||
|
*/
|
||||||
|
content: string
|
||||||
|
/**
|
||||||
|
* The raw full container block
|
||||||
|
*/
|
||||||
|
raw: string
|
||||||
|
/**
|
||||||
|
* The options of the container
|
||||||
|
*/
|
||||||
|
options: ContainerOptions
|
||||||
|
/**
|
||||||
|
* The lexer that can be used to render nested tokens if needed
|
||||||
|
*/
|
||||||
|
lexer: Lexer
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TokenExtractor = (params: any) => any
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Container extension is a marked extension that parses a block of text surrounded by :::
|
||||||
|
*
|
||||||
|
* @param tokensExtractor Converts the container input into a custom token that svelte components will receive.
|
||||||
|
* If this function returns a value that contains an array of `tokens`,
|
||||||
|
* this array will be populated with the tokens that are found inside the container,
|
||||||
|
* allowing to nest markdown inside the container.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* Given the following markdown:
|
||||||
|
*
|
||||||
|
* :::container option="option-1" option-2="option-2" boolean-option
|
||||||
|
* Something inside the container
|
||||||
|
* :::
|
||||||
|
*
|
||||||
|
* The token extractor would receive the following parameters:
|
||||||
|
* **type**: `container`
|
||||||
|
* **content**: `Something inside the container`
|
||||||
|
* **options**: { 'option': 'option-1', 'option-2': 'option-2', 'boolean-option': true }
|
||||||
|
*/
|
||||||
|
export default function (tokensExtractor: any): any {
|
||||||
|
return {
|
||||||
|
name: 'container',
|
||||||
|
level: 'block',
|
||||||
|
start(src: any) {
|
||||||
|
return src.match(/^:::[^:\n]/)?.index
|
||||||
|
},
|
||||||
|
tokenizer(src: any): any {
|
||||||
|
const rule =
|
||||||
|
/^:::[\s]?(?<type>[a-z0-9-]+)(?<options>.*)?\n(?<content>(?:.|\n)*)\n:::(?:\n|$)/i
|
||||||
|
|
||||||
|
const match = rule.exec(findRawContainer(src))
|
||||||
|
if (!match || !match.groups) return null
|
||||||
|
|
||||||
|
const type = match.groups.type.toLocaleLowerCase()
|
||||||
|
const options = parseOptions(match.groups.options || '')
|
||||||
|
const content = match.groups.content || ''
|
||||||
|
|
||||||
|
const result = tokensExtractor({
|
||||||
|
type: type,
|
||||||
|
content: content,
|
||||||
|
raw: match[0],
|
||||||
|
options: options,
|
||||||
|
lexer: this.lexer,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result?.tokens) {
|
||||||
|
this.lexer.blockTokens(content, result.tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function findRawContainer(src: any): any {
|
||||||
|
if (!src.startsWith(':::')) return undefined
|
||||||
|
const lines = src.split('\n')
|
||||||
|
let open = 1
|
||||||
|
let lineNumber = 1
|
||||||
|
|
||||||
|
for (lineNumber = 1; lineNumber < lines.length; lineNumber++) {
|
||||||
|
const line = lines[lineNumber]
|
||||||
|
if (line.startsWith(':::')) {
|
||||||
|
if (/:::[^:\n\s]/.test(line)) {
|
||||||
|
open++
|
||||||
|
} else if (/^:::(\n|$)/.test(line)) {
|
||||||
|
open -= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (open === 0) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines.slice(0, lineNumber + 1).join('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseOptions(options: string): ContainerOptions {
|
||||||
|
const output: any = {}
|
||||||
|
let remaining = options
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const regex = /(?<name>[a-z0-9]+)(?:="(?<value>(?:.*?))")?/i
|
||||||
|
const match = regex.exec(remaining)
|
||||||
|
if (!match) break
|
||||||
|
|
||||||
|
const name = match?.groups?.name
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
output[name] = match?.groups?.value ?? true
|
||||||
|
}
|
||||||
|
|
||||||
|
remaining = remaining.slice(match.index + match[0].length)
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<table class="overflow-auto w-full table-fixed rounded-xl">
|
||||||
|
<slot />
|
||||||
|
</table>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<tbody><slot /></tbody>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export let header: boolean
|
||||||
|
export let align:
|
||||||
|
| 'left'
|
||||||
|
| 'center'
|
||||||
|
| 'right'
|
||||||
|
| 'justify'
|
||||||
|
| 'char'
|
||||||
|
| null
|
||||||
|
| undefined = undefined
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if header}
|
||||||
|
<th
|
||||||
|
class="bg-slate-50 dark:bg-zinc-900 border border-slate-200 dark:border-zinc-800 py-2 px-3"
|
||||||
|
{align}
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</th>
|
||||||
|
{:else}
|
||||||
|
<td
|
||||||
|
class="bg-slate-25 dark:bg-zinc-950 border border-slate-200 dark:border-zinc-800 py-2 px-3"
|
||||||
|
{align}
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</td>
|
||||||
|
{/if}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<thead><slot /></thead>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<tr><slot /></tr>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { photonify } from '$lib/components/markdown/markdown.js'
|
import { photonify } from '$lib/components/markdown/renderers/plugins'
|
||||||
import { TextInput } from 'mono-svelte'
|
import { TextInput } from 'mono-svelte'
|
||||||
|
|
||||||
let link = ''
|
let link = ''
|
||||||
|
|||||||
+2
-2
@@ -54,12 +54,12 @@ body {
|
|||||||
z-index: 999 !important;
|
z-index: 999 !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
pre {
|
pre:not(.code-baseline) {
|
||||||
@apply bg-slate-100 p-2 rounded-xl my-2 max-w-full whitespace-pre-wrap
|
@apply bg-slate-100 p-2 rounded-xl my-2 max-w-full whitespace-pre-wrap
|
||||||
border-slate-200 border dark:bg-zinc-950 dark:border-zinc-900 text-xs;
|
border-slate-200 border dark:bg-zinc-950 dark:border-zinc-900 text-xs;
|
||||||
}
|
}
|
||||||
|
|
||||||
:not(pre) > code {
|
:not(pre) > code:not(.code-baseline) {
|
||||||
@apply bg-slate-100 dark:bg-zinc-950;
|
@apply bg-slate-100 dark:bg-zinc-950;
|
||||||
padding-left: 0.25rem;
|
padding-left: 0.25rem;
|
||||||
padding-right: 0.25rem;
|
padding-right: 0.25rem;
|
||||||
|
|||||||
Reference in new issue
Block a user