mirror of
https://github.com/mat-1/matdoesdev.git
synced 2025-08-02 06:36:04 +00:00
start switching to markdown-it
This commit is contained in:
parent
bfe39ce7b8
commit
8559f00904
3 changed files with 59 additions and 115 deletions
|
@ -36,10 +36,11 @@
|
|||
"@sveltejs/adapter-node": "^1.0.0-next.0",
|
||||
"@sveltejs/adapter-static": "^1.0.0-next.21",
|
||||
"@types/js-yaml": "^4.0.4",
|
||||
"@types/markdown-it": "^12.2.3",
|
||||
"cookie": "^0.4.1",
|
||||
"html-minifier": "^4.0.0",
|
||||
"js-yaml": "^4.1.0",
|
||||
"marked": "^4.0.3"
|
||||
"markdown-it": "^12.3.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
|
|
117
src/lib/utils.ts
117
src/lib/utils.ts
|
@ -1,115 +1,12 @@
|
|||
import { marked } from 'marked'
|
||||
import MarkdownIt from 'markdown-it'
|
||||
|
||||
export function markdownToHtml(md: string, baseUrl?: string): string {
|
||||
const renderer: Partial<marked.Renderer> = {
|
||||
image(href: string, title: string, text: string) {
|
||||
// href = cleanUrl(this.options.sanitize, this.options.baseUrl, href)
|
||||
href = baseUrl ? resolveUrl(baseUrl, href) : href
|
||||
if (href === null) return text
|
||||
let out = `<img src="${href}" alt="${text}"`
|
||||
if (title) out += ` title="${title}"`
|
||||
out += '/>'
|
||||
return out
|
||||
},
|
||||
}
|
||||
const md = new MarkdownIt({
|
||||
html: true,
|
||||
breaks: true,
|
||||
})
|
||||
|
||||
const centered: marked.TokenizerExtension = {
|
||||
name: 'centered',
|
||||
level: 'block',
|
||||
start(src: string) {
|
||||
// the marked typings want a `number` and we're returning a `number | undefined` so we have to do this :(
|
||||
return src.match(/\|\|/)?.index as number
|
||||
},
|
||||
tokenizer(
|
||||
this: marked.TokenizerThis,
|
||||
src: string,
|
||||
tokens: marked.Token[]
|
||||
): marked.Tokens.Generic | void {
|
||||
const rule = /^\|\|(.+?)\|\|/
|
||||
const match = rule.exec(src)
|
||||
if (match) {
|
||||
const token = {
|
||||
type: 'centered',
|
||||
raw: match[0],
|
||||
text: match[1].trim(),
|
||||
tokens: [],
|
||||
}
|
||||
this.lexer.inline(token.text, token.tokens)
|
||||
return token
|
||||
}
|
||||
},
|
||||
// @ts-expect-error Marked doesn't include `renderer` in the typings.
|
||||
renderer(this: marked.TokenizerThis, token: marked.Tokens): string | false {
|
||||
// @ts-expect-error Property 'parser' does not exist on type 'TokenizerThis'.
|
||||
return `<div class="center">${this.parser.parse(token.tokens)}\n</div>`
|
||||
},
|
||||
}
|
||||
const left: marked.TokenizerExtension = {
|
||||
name: 'left',
|
||||
level: 'block',
|
||||
start(src: string) {
|
||||
// the marked typings want a `number` and we're returning a `number | undefined` so we have to do this :(
|
||||
return src.match(/<-\W/)?.index as number
|
||||
},
|
||||
tokenizer(
|
||||
this: marked.TokenizerThis,
|
||||
src: string,
|
||||
tokens: marked.Token[]
|
||||
): marked.Tokens.Generic | void {
|
||||
const rule = /^<-\W([\w\W]+?)\W<-/
|
||||
const match = rule.exec(src)
|
||||
if (match) {
|
||||
const token = {
|
||||
type: 'left',
|
||||
raw: match[0],
|
||||
text: match[1].trim(),
|
||||
tokens: [],
|
||||
}
|
||||
this.lexer.blockTokens(token.text, token.tokens)
|
||||
return token
|
||||
}
|
||||
},
|
||||
// @ts-expect-error Marked doesn't include `renderer` in the typings.
|
||||
renderer(this: marked.TokenizerThis, token: marked.Tokens): string | false {
|
||||
// @ts-expect-error Property 'parser' does not exist on type 'TokenizerThis'.
|
||||
return `<div class="markdown-float-left">${this.parser.parse(token.tokens)}\n</div>`
|
||||
},
|
||||
}
|
||||
const right: marked.TokenizerExtension = {
|
||||
name: 'right',
|
||||
level: 'block',
|
||||
start(src: string) {
|
||||
// the marked typings want a `number` and we're returning a `number | undefined` so we have to do this :(
|
||||
return src.match(/->\W/)?.index as number
|
||||
},
|
||||
tokenizer(
|
||||
this: marked.TokenizerThis,
|
||||
src: string,
|
||||
tokens: marked.Token[]
|
||||
): marked.Tokens.Generic | void {
|
||||
const rule = /^->\W([\w\W]+?)\W->/
|
||||
const match = rule.exec(src)
|
||||
if (match) {
|
||||
const token = {
|
||||
type: 'right',
|
||||
raw: match[0],
|
||||
text: match[1].trim(),
|
||||
tokens: [],
|
||||
}
|
||||
this.lexer.blockTokens(token.text, token.tokens)
|
||||
return token
|
||||
}
|
||||
},
|
||||
// @ts-expect-error Marked doesn't include `renderer` in the typings.
|
||||
renderer(this: marked.TokenizerThis, token: marked.Tokens): string | false {
|
||||
// @ts-expect-error Property 'parser' does not exist on type 'TokenizerThis'.
|
||||
return `<div class="markdown-float-right">${this.parser.parse(token.tokens)}\n</div>`
|
||||
},
|
||||
}
|
||||
|
||||
marked.use({ renderer, extensions: [centered, left, right] })
|
||||
|
||||
return marked.parse(md, { baseUrl, breaks: true })
|
||||
export function markdownToHtml(original: string, baseUrl?: string): string {
|
||||
return md.render(original)
|
||||
}
|
||||
|
||||
// https://nodejs.org/api/url.html#urlresolvefrom-to
|
||||
|
|
54
yarn.lock
54
yarn.lock
|
@ -166,11 +166,29 @@
|
|||
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
|
||||
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
|
||||
|
||||
"@types/linkify-it@*":
|
||||
version "3.0.2"
|
||||
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":
|
||||
version "12.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@types/markdown-it/-/markdown-it-12.2.3.tgz#0d6f6e5e413f8daaa26522904597be3d6cd93b51"
|
||||
integrity sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==
|
||||
dependencies:
|
||||
"@types/linkify-it" "*"
|
||||
"@types/mdurl" "*"
|
||||
|
||||
"@types/marked@^4.0.0":
|
||||
version "4.0.3"
|
||||
resolved "https://registry.yarnpkg.com/@types/marked/-/marked-4.0.3.tgz#2098f4a77adaba9ce881c9e0b6baf29116e5acc4"
|
||||
integrity sha512-HnMWQkLJEf/PnxZIfbm0yGJRRZYYMhb++O9M36UCTA9z53uPvVoSlAwJr3XOpDEryb7Hwl1qAx/MV6YIW1RXxg==
|
||||
|
||||
"@types/mdurl@*":
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/mdurl/-/mdurl-1.0.2.tgz#e2ce9d83a613bacf284c7be7d491945e39e1f8e9"
|
||||
integrity sha512-eC4U9MlIcu2q0KQmXszyn5Akca/0jrQmwDRgpAMJai7qBWq4amIQhZyNau4VYGtCeALvW1/NtjzJJ567aZxfKA==
|
||||
|
||||
"@types/node@*":
|
||||
version "17.0.23"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.23.tgz#3b41a6e643589ac6442bdbd7a4a3ded62f33f7da"
|
||||
|
@ -527,6 +545,11 @@ enquirer@^2.3.5:
|
|||
dependencies:
|
||||
ansi-colors "^4.1.1"
|
||||
|
||||
entities@~2.1.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/entities/-/entities-2.1.0.tgz#992d3129cf7df6870b96c57858c249a120f8b8b5"
|
||||
integrity sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==
|
||||
|
||||
es6-promise@^3.1.2:
|
||||
version "3.3.1"
|
||||
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.3.1.tgz#a08cdde84ccdbf34d027a1451bc91d4bcd28a613"
|
||||
|
@ -1097,6 +1120,13 @@ levn@^0.4.1:
|
|||
prelude-ls "^1.2.1"
|
||||
type-check "~0.4.0"
|
||||
|
||||
linkify-it@^3.0.1:
|
||||
version "3.0.3"
|
||||
resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-3.0.3.tgz#a98baf44ce45a550efb4d49c769d07524cc2fa2e"
|
||||
integrity sha512-ynTsyrFSdE5oZ/O9GEf00kPngmOfVwazR5GKDq6EYfhlpFug3J2zybX56a2PRRpc9P+FuSoGNAwjlbDs9jJBPQ==
|
||||
dependencies:
|
||||
uc.micro "^1.0.1"
|
||||
|
||||
lodash.merge@^4.6.2:
|
||||
version "4.6.2"
|
||||
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
|
||||
|
@ -1131,10 +1161,21 @@ magic-string@^0.26.1:
|
|||
dependencies:
|
||||
sourcemap-codec "^1.4.8"
|
||||
|
||||
marked@^4.0.3:
|
||||
version "4.0.13"
|
||||
resolved "https://registry.yarnpkg.com/marked/-/marked-4.0.13.tgz#4fd46ca93da46448f3d83f054d938c4f905a258d"
|
||||
integrity sha512-lS/ZCa4X0gsRcfWs1eoh6dLnHr9kVH3K1t2X4M/tTtNouhZ7anS1Csb6464VGLQHv8b2Tw1cLeZQs58Jav8Rzw==
|
||||
markdown-it@^12.3.2:
|
||||
version "12.3.2"
|
||||
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-12.3.2.tgz#bf92ac92283fe983fe4de8ff8abfb5ad72cd0c90"
|
||||
integrity sha512-TchMembfxfNVpHkbtriWltGWc+m3xszaRD0CZup7GFFhzIgQqxIfn3eGj1yZpfuflzPvfkt611B2Q/Bsk1YnGg==
|
||||
dependencies:
|
||||
argparse "^2.0.1"
|
||||
entities "~2.1.0"
|
||||
linkify-it "^3.0.1"
|
||||
mdurl "^1.0.1"
|
||||
uc.micro "^1.0.5"
|
||||
|
||||
mdurl@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e"
|
||||
integrity sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=
|
||||
|
||||
merge2@^1.3.0, merge2@^1.4.1:
|
||||
version "1.4.1"
|
||||
|
@ -1614,6 +1655,11 @@ typescript@*, typescript@^4.4.3:
|
|||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.3.tgz#eefeafa6afdd31d725584c67a0eaba80f6fc6c6c"
|
||||
integrity sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==
|
||||
|
||||
uc.micro@^1.0.1, uc.micro@^1.0.5:
|
||||
version "1.0.6"
|
||||
resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac"
|
||||
integrity sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==
|
||||
|
||||
uglify-js@^3.5.1:
|
||||
version "3.15.3"
|
||||
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.15.3.tgz#9aa82ca22419ba4c0137642ba0df800cb06e0471"
|
||||
|
|
Loading…
Add table
Reference in a new issue