temp commit
This commit is contained in:
1 parent
25a3b10937
commit
2b642b0926
7 files changed
+175
-251
No files matched your search
@@ -18,10 +18,12 @@
|
||||
"autoprefixer": "^10.4.16",
|
||||
"jsdom": "^22.1.0",
|
||||
"lemmy-js-client": "latest",
|
||||
"linkify-it": "^5.0.0",
|
||||
"markdown-it": "^14.1.0",
|
||||
"markdown-it-container": "^3.0.0",
|
||||
"markdown-it-sub": "^1.0.0",
|
||||
"markdown-it-sup": "^1.0.0",
|
||||
"marked-linkify-it": "^3.1.10",
|
||||
"mono-svelte": "^1.5.14",
|
||||
"nprogress": "^0.2.0",
|
||||
"postcss": "^8.4.30",
|
||||
@@ -30,6 +32,7 @@
|
||||
"svelte-check": "^3.0.1",
|
||||
"svelte-hero-icons": "^5.0.0",
|
||||
"svelte-infinite-scroll": "^2.0.1",
|
||||
"svelte-markdown": "^0.4.1",
|
||||
"sveltekit-i18n": "^2.4.2",
|
||||
"tailwindcss": "^3.3.3",
|
||||
"tslib": "^2.4.1",
|
||||
|
||||
@@ -1,55 +1,19 @@
|
||||
<script lang="ts">
|
||||
import { page } from '$app/stores'
|
||||
import { env } from '$env/dynamic/public'
|
||||
import { md, mdInline, photonify } from '$lib/components/markdown/markdown'
|
||||
import { onMount } from 'svelte'
|
||||
import SvelteMarkdown from 'svelte-markdown'
|
||||
import { marked } from 'marked'
|
||||
import { linkify, spoilerPlugin } from './renderers/plugins'
|
||||
import Heading from './renderers/Heading.svelte'
|
||||
import MdImage from './renderers/MdImage.svelte'
|
||||
|
||||
export let source: string = ''
|
||||
export let inline: boolean = false
|
||||
export let noStyle: boolean = false
|
||||
|
||||
let rendered = ''
|
||||
|
||||
function replaceURLs(node: HTMLElement, source: string) {
|
||||
if (!div) return
|
||||
const links = node.querySelectorAll('a')
|
||||
|
||||
links.forEach((l) => {
|
||||
const photonified = photonify(l.href)
|
||||
|
||||
if (photonified) l.href = photonified
|
||||
else if (!l.href.startsWith($page.url.origin)) {
|
||||
l.target = '_blank'
|
||||
l.rel = 'noopener noreferrer'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let div: HTMLElement
|
||||
|
||||
function render(source: string): string {
|
||||
if (!source || source == '') return ''
|
||||
marked.use(linkify)
|
||||
|
||||
try {
|
||||
return inline ? mdInline.render(source) : md.render(source)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return ''
|
||||
}
|
||||
}
|
||||
$: rendered = render(source)
|
||||
$: replaceURLs(div, rendered)
|
||||
/*
|
||||
Horrendous hack to get @html reactivity working
|
||||
From server to client, the rendered html won't change for some reason
|
||||
I have no clue why this fixes it, but it does.
|
||||
*/
|
||||
if (env?.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true') {
|
||||
onMount(() => {
|
||||
rendered = `${rendered} `
|
||||
replaceURLs(div, rendered)
|
||||
})
|
||||
}
|
||||
$: output = marked.parse(source)
|
||||
</script>
|
||||
|
||||
<div
|
||||
@@ -59,98 +23,9 @@
|
||||
: 'break-words flex flex-col markdown gap-2 leading-[1.5]'} {$$props.class}"
|
||||
style={$$props.style}
|
||||
>
|
||||
{@html rendered}
|
||||
<!-- {@html rendered} -->
|
||||
<SvelteMarkdown
|
||||
source={output}
|
||||
renderers={{ heading: Heading, image: MdImage }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<style lang="postcss">
|
||||
.markdown :global(h1) {
|
||||
@apply text-3xl font-bold;
|
||||
}
|
||||
.markdown :global(h2) {
|
||||
@apply text-2xl font-bold;
|
||||
}
|
||||
.markdown :global(h3) {
|
||||
@apply text-xl font-bold;
|
||||
}
|
||||
|
||||
.markdown :global(details) {
|
||||
@apply cursor-pointer;
|
||||
}
|
||||
|
||||
.markdown :global(hr) {
|
||||
@apply w-full mx-auto my-2 border-slate-300 dark:border-zinc-800;
|
||||
}
|
||||
|
||||
:global(.dark .markdown hr) {
|
||||
@apply w-full mx-auto my-2 border-zinc-800;
|
||||
}
|
||||
|
||||
.markdown :global(img) {
|
||||
@apply max-h-[40vh] border rounded-lg border-slate-200 dark:border-zinc-800;
|
||||
}
|
||||
|
||||
:global(.dark .markdown img) {
|
||||
@apply border-zinc-800;
|
||||
}
|
||||
|
||||
.markdown :global(a) {
|
||||
@apply text-sky-500 hover:underline;
|
||||
}
|
||||
|
||||
.markdown :global(ul) {
|
||||
@apply list-disc pl-4 leading-3;
|
||||
}
|
||||
|
||||
.markdown :global(ol) {
|
||||
@apply list-decimal pl-4 leading-3;
|
||||
}
|
||||
|
||||
.markdown :global(ul > *) {
|
||||
@apply leading-[20px];
|
||||
}
|
||||
|
||||
.markdown :global(ol > *) {
|
||||
@apply leading-[20px];
|
||||
}
|
||||
|
||||
.markdown :global(li) {
|
||||
@apply m-0 leading-[1.5] !important;
|
||||
}
|
||||
|
||||
.markdown :global(li > p) {
|
||||
@apply m-0 leading-[1.5] !important;
|
||||
}
|
||||
|
||||
.markdown :global(li > p) {
|
||||
@apply contents !important;
|
||||
}
|
||||
|
||||
.markdown :global(blockquote) {
|
||||
@apply leading-[1px] border-l-2 border-slate-400 dark:border-zinc-600 pl-2 my-1 h-max;
|
||||
}
|
||||
|
||||
.markdown :global(p) {
|
||||
@apply leading-[1.5] max-w-3xl;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.markdown :global(table) {
|
||||
@apply rounded-xl w-full overflow-auto min-w-0 table-fixed relative;
|
||||
}
|
||||
|
||||
.markdown :global(table thead tr th) {
|
||||
@apply border border-slate-200 px-4 py-2;
|
||||
}
|
||||
|
||||
.markdown :global(table tr td) {
|
||||
@apply border border-slate-200 px-4 py-2 overflow-auto;
|
||||
}
|
||||
|
||||
:global(.dark) .markdown :global(table tr td) {
|
||||
@apply border border-zinc-800 px-4 py-2 overflow-auto;
|
||||
}
|
||||
|
||||
:global(.dark) .markdown :global(table thead tr th) {
|
||||
@apply border border-zinc-800 bg-zinc-900 px-4 py-2;
|
||||
}
|
||||
</style>
|
||||
@@ -1,3 +1,4 @@
|
||||
import LinkifyIt from 'linkify-it'
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import markdown_it_container from 'markdown-it-container'
|
||||
// @ts-ignore
|
||||
@@ -5,125 +6,51 @@ 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+(.*)/)
|
||||
},
|
||||
// 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+(.*)/)
|
||||
// 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)
|
||||
// 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+(.*)/)
|
||||
},
|
||||
// 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+(.*)/)
|
||||
// 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 communityLinks = {
|
||||
validate: function (text: any, pos: any, self: any) {
|
||||
var tail = text.slice(pos)
|
||||
|
||||
if (!self.re.community) {
|
||||
self.re.community = new RegExp(
|
||||
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i
|
||||
)
|
||||
}
|
||||
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: any) {
|
||||
let prefix = match.url
|
||||
prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1)
|
||||
|
||||
match.url = `/c/${prefix}`
|
||||
},
|
||||
}
|
||||
|
||||
const userLinks = {
|
||||
validate: function (text: any, pos: any, self: any) {
|
||||
var tail = text.slice(pos)
|
||||
|
||||
if (!self.re.user) {
|
||||
self.re.user = new RegExp(
|
||||
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i
|
||||
)
|
||||
}
|
||||
if (self.re.user.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.user)![0].length
|
||||
}
|
||||
return 0
|
||||
},
|
||||
normalize: function (match: any) {
|
||||
let prefix = match.url
|
||||
prefix = prefix.startsWith('u/') ? prefix.slice(2) : prefix.slice(1)
|
||||
|
||||
match.url = `/u/${prefix}`
|
||||
},
|
||||
}
|
||||
|
||||
md.linkify.add('!', {
|
||||
validate: communityLinks.validate,
|
||||
normalize: communityLinks.normalize,
|
||||
})
|
||||
|
||||
md.linkify.add('c/', {
|
||||
validate: communityLinks.validate,
|
||||
normalize: communityLinks.normalize,
|
||||
})
|
||||
|
||||
md.linkify.add('@', {
|
||||
validate: userLinks.validate,
|
||||
normalize: userLinks.normalize,
|
||||
})
|
||||
|
||||
md.linkify.add('u/', {
|
||||
validate: userLinks.validate,
|
||||
normalize: userLinks.normalize,
|
||||
})
|
||||
// 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,
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<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,8 @@
|
||||
<script>
|
||||
export let href = ''
|
||||
export let title = undefined
|
||||
export let text = ''
|
||||
</script>
|
||||
|
||||
hi
|
||||
<img src={href} {title} alt={text} />
|
||||
@@ -0,0 +1,88 @@
|
||||
import { marked } from 'marked'
|
||||
import markedLinkifyIt from 'marked-linkify-it'
|
||||
|
||||
export const spoilerPlugin = {
|
||||
name: 'spoiler',
|
||||
level: 'block',
|
||||
start(src: string) {
|
||||
return src.match(/^:::/)?.index
|
||||
},
|
||||
tokenizer(src: string, tokens: marked.TokensList) {
|
||||
const rule = /^::: spoiler (.+)\n([\s\S]*?)\n:::/
|
||||
const match = rule.exec(src)
|
||||
if (match) {
|
||||
return {
|
||||
type: 'spoiler',
|
||||
raw: match[0],
|
||||
title: match[1].trim(),
|
||||
text: match[2].trim(),
|
||||
}
|
||||
}
|
||||
},
|
||||
renderer(token: any) {
|
||||
return `
|
||||
<details>
|
||||
<summary>${marked.parseInline(token.title)}</summary>
|
||||
${marked.parse(token.text)}
|
||||
</details>
|
||||
`
|
||||
},
|
||||
}
|
||||
|
||||
export const linkify = markedLinkifyIt(
|
||||
{
|
||||
'!': {
|
||||
validate: function (text: any, pos: any, self: any) {
|
||||
var tail = text.slice(pos)
|
||||
|
||||
if (!self.re.community) {
|
||||
self.re.community = new RegExp(
|
||||
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i
|
||||
)
|
||||
}
|
||||
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: any) {
|
||||
let prefix = match.url
|
||||
prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1)
|
||||
|
||||
match.url = `/c/${prefix}`
|
||||
},
|
||||
},
|
||||
'@': {
|
||||
validate: function (text: any, pos: any, self: any) {
|
||||
var tail = text.slice(pos)
|
||||
|
||||
if (!self.re.user) {
|
||||
self.re.user = new RegExp(
|
||||
/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z]{2,63})/i
|
||||
)
|
||||
}
|
||||
if (self.re.user.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.user)![0].length
|
||||
}
|
||||
return 0
|
||||
},
|
||||
normalize: function (match: any) {
|
||||
let prefix = match.url
|
||||
prefix = prefix.startsWith('u/') ? prefix.slice(2) : prefix.slice(1)
|
||||
|
||||
match.url = `/u/${prefix}`
|
||||
},
|
||||
},
|
||||
},
|
||||
{}
|
||||
)
|
||||
Reference in new issue
Block a user