@kaciras-blog/markdown
Advanced tools
Comparing version 3.2.1 to 3.2.2
@@ -1,2 +0,2 @@ | ||
import { a as f } from "./fence-Kc-htwcQ.js"; | ||
import { a as f } from "./fence-6ZKU-3dl.js"; | ||
import { silencePromise as u, identity as d } from "@kaciras/utilities/browser"; | ||
@@ -3,0 +3,0 @@ function g(n, o) { |
import MarkdownIt from "markdown-it"; | ||
export interface GenericDirective { | ||
type: string; | ||
label: string; | ||
href: string; | ||
attrs: Record<string, any>; | ||
} | ||
/** | ||
* 检查链接,处理 URL 转义和 XSS 攻击,如果有风险则返回空字符串。 | ||
* 解析指令语法,从中提取出各个部分并处理转义,详解见: | ||
* https://blog.kaciras.com/article/18/add-video-support-to-markdown | ||
* | ||
* @param md MarkdownIt 的实例 | ||
* @param link 需要检查的链接 | ||
* @return 安全的链接,可以直接写进HTML | ||
* @param line 待解析的文本 | ||
* @return 包含各个部分的对象 | ||
* @throws 如果给定的文本不符合指令语法 | ||
*/ | ||
export declare function checkLink(md: MarkdownIt, link: string): string; | ||
export declare function parseGenericDirective(line: string): GenericDirective; | ||
/** | ||
@@ -11,0 +18,0 @@ * 自定义渲染函数,以 type 作为键,值为渲染函数。 |
112
lib/index.js
import { unescapeMd as p, escapeHtml as v } from "markdown-it/lib/common/utils.mjs"; | ||
import y from "markdown-it-toc-done-right"; | ||
import { default as De } from "markdown-it-toc-done-right"; | ||
import { default as qe } from "markdown-it-toc-done-right"; | ||
import m from "markdown-it-anchor"; | ||
import L from "markdown-it-footnote"; | ||
import { f as w } from "./fence-Kc-htwcQ.js"; | ||
import { f as w } from "./fence-6ZKU-3dl.js"; | ||
import a from "highlight.js/lib/core"; | ||
@@ -17,14 +17,14 @@ import $ from "highlight.js/lib/languages/c"; | ||
import A from "highlight.js/lib/languages/ini"; | ||
import z from "highlight.js/lib/languages/java"; | ||
import T from "highlight.js/lib/languages/javascript"; | ||
import T from "highlight.js/lib/languages/java"; | ||
import z from "highlight.js/lib/languages/javascript"; | ||
import E from "highlight.js/lib/languages/json"; | ||
import P from "highlight.js/lib/languages/kotlin"; | ||
import F from "highlight.js/lib/languages/less"; | ||
import H from "highlight.js/lib/languages/lua"; | ||
import I from "highlight.js/lib/languages/protobuf"; | ||
import G from "highlight.js/lib/languages/python"; | ||
import N from "highlight.js/lib/languages/rust"; | ||
import R from "highlight.js/lib/languages/scss"; | ||
import q from "highlight.js/lib/languages/shell"; | ||
import D from "highlight.js/lib/languages/sql"; | ||
import G from "highlight.js/lib/languages/lua"; | ||
import H from "highlight.js/lib/languages/protobuf"; | ||
import I from "highlight.js/lib/languages/python"; | ||
import D from "highlight.js/lib/languages/rust"; | ||
import N from "highlight.js/lib/languages/scss"; | ||
import R from "highlight.js/lib/languages/shell"; | ||
import q from "highlight.js/lib/languages/sql"; | ||
import O from "highlight.js/lib/languages/yaml"; | ||
@@ -42,4 +42,4 @@ import U from "highlight.js/lib/languages/typescript"; | ||
if (!t) { | ||
const { type: l, label: c, href: u, attrs: b } = o, f = e.push("directive", l, 0); | ||
f.attrs = [["href", u]], f.meta = b, f.content = c, f.map = [r, e.line]; | ||
const { type: c, label: l, href: u, attrs: b } = o, f = e.push("directive", c, 0); | ||
f.attrs = [["href", u]], f.meta = b, f.content = l, f.map = [r, e.line]; | ||
} | ||
@@ -52,9 +52,9 @@ return e.line = r + 1, !0; | ||
throw new Error("Not a directive syntax."); | ||
const [s, t] = r, n = d(e, s.length, 91, 93), i = d(e, n + 1, 40, 41), o = p(e.slice(s.length + 1, n)), l = p(e.slice(n + 2, i)), c = i + 1; | ||
const [{ length: s }, t] = r, n = d(e, s, 91, 93), i = d(e, n + 1, 40, 41), o = p(e.slice(s + 1, n)), c = p(e.slice(n + 2, i)), l = i + 1; | ||
let u = {}; | ||
if (e.charCodeAt(c) === 123) | ||
u = JSON.parse(e.slice(c)); | ||
else if (c !== e.length) | ||
if (e.charCodeAt(l) === 123) | ||
u = JSON.parse(e.slice(l)); | ||
else if (l !== e.length) | ||
throw new Error("Extra strings after the directive."); | ||
return { type: t, label: o, href: l, attrs: u }; | ||
return { type: t, label: o, href: c, attrs: u }; | ||
} | ||
@@ -102,8 +102,8 @@ function d(e, r, s, t) { | ||
e.renderer.rules.directive = (s, t) => { | ||
const n = s[t], { tag: i, content: o } = n, l = n.attrGet("href"), c = r[i]; | ||
return c ? c(V(e, l), o, e) : `[Unknown directive: ${i}]`; | ||
const n = s[t], { tag: i, content: o } = n, c = n.attrGet("href"), l = r[i]; | ||
return l ? l(V(e, c), o, e) : `[Unknown directive: ${i}]`; | ||
}, e.block.ruler.before("html_block", "directive", W); | ||
} | ||
var Q = /* @__PURE__ */ ((e) => (e[e.None = 0] = "None", e[e.Italic = 1] = "Italic", e[e.Bold = 2] = "Bold", e[e.Code = 4] = "Code", e[e.StrikeThrough = 8] = "StrikeThrough", e))(Q || {}); | ||
function Ie(e) { | ||
function He(e) { | ||
let r = 0, s = 0, t = e.length - 1, n = 0, i = e.charCodeAt(0); | ||
@@ -116,14 +116,14 @@ if (t < 1) | ||
for (; s < t; s++, t--, n++) { | ||
const c = e.charCodeAt(s); | ||
if (c !== e.charCodeAt(t)) | ||
const l = e.charCodeAt(s); | ||
if (l !== e.charCodeAt(t)) | ||
break; | ||
if (c !== i) { | ||
if (l !== i) { | ||
const u = o(); | ||
if (u === 0) | ||
break; | ||
n = 0, i = c, r |= u; | ||
n = 0, i = l, r |= u; | ||
} | ||
} | ||
const l = o(); | ||
return [r | l, l === 0 ? s - n : s]; | ||
const c = o(); | ||
return [r | c, c === 0 ? s - n : s]; | ||
} | ||
@@ -191,14 +191,14 @@ function X(e) { | ||
a.registerLanguage("ini", A); | ||
a.registerLanguage("java", z); | ||
a.registerLanguage("javascript", T); | ||
a.registerLanguage("java", T); | ||
a.registerLanguage("javascript", z); | ||
a.registerLanguage("json", E); | ||
a.registerLanguage("kotlin", P); | ||
a.registerLanguage("less", F); | ||
a.registerLanguage("lua", H); | ||
a.registerLanguage("protobuf", I); | ||
a.registerLanguage("python", G); | ||
a.registerLanguage("rust", N); | ||
a.registerLanguage("scss", R); | ||
a.registerLanguage("shell", q); | ||
a.registerLanguage("sql", D); | ||
a.registerLanguage("lua", G); | ||
a.registerLanguage("protobuf", H); | ||
a.registerLanguage("python", I); | ||
a.registerLanguage("rust", D); | ||
a.registerLanguage("scss", N); | ||
a.registerLanguage("shell", R); | ||
a.registerLanguage("sql", q); | ||
a.registerLanguage("yaml", O); | ||
@@ -252,3 +252,3 @@ a.registerLanguage("typescript", U); | ||
const s = e[r], t = s.attrGet("src") ?? "", n = this.utils.escapeHtml(s.content); | ||
return `<span class="center-wrapper"><a ${k(t)} href="${t}" target="_blank" rel="noopener,nofollow"><img data-src="${t}" alt="${n}" class="md-img" crossorigin></a>${n ? `<span class="md-alt">${n}</span>` : ""}</span>`; | ||
return `<span class="md-center" ${k(t)}><a class="md-inspect" href="${t}" target="_blank" rel="noopener,nofollow"><img data-src="${t}" alt="${n}" class="md-img" crossorigin></a>${n ? `<span class="md-alt">${n}</span>` : ""}</span>`; | ||
} | ||
@@ -258,9 +258,9 @@ const ee = { | ||
gif(e, r, s) { | ||
return r = s.utils.escapeHtml(r), `<p class="center-wrapper" ${k(e)}><video class="gif" crossorigin loop muted data-src="${e}"></video>${r ? `<span class="md-alt">${r}</span>` : ""}</p>`; | ||
return r = s.utils.escapeHtml(r), `<p class="md-center" ${k(e)}><video class="gif" crossorigin loop muted data-src="${e}"></video>${r ? `<span class="md-alt">${r}</span>` : ""}</p>`; | ||
}, | ||
video(e, r, s) { | ||
return r = s.normalizeLink(r), s.validateLink(r) || (r = ""), `<p class="center-wrapper"><video class="md-video" controls crossorigin poster="${r}" data-src="${e}"></video></p>`; | ||
return r = s.normalizeLink(r), s.validateLink(r) || (r = ""), `<p class="md-center"><video class="md-video" controls crossorigin poster="${r}" data-src="${e}"></video></p>`; | ||
}, | ||
audio(e, r, s) { | ||
return r = s.utils.escapeHtml(r), `<p class="center-wrapper"><audio controls data-src="${e}" crossorigin></audio>${r ? `<span class="md-alt">${r}</span>` : ""}</p>`; | ||
return r = s.utils.escapeHtml(r), `<p class="md-center"><audio controls data-src="${e}" crossorigin></audio>${r ? `<span class="md-alt">${r}</span>` : ""}</p>`; | ||
} | ||
@@ -272,4 +272,4 @@ }; | ||
function g(e, r, s) { | ||
const { src: t, bMarks: n, tShift: i, eMarks: o } = e, l = n[r] + i[r], c = o[r]; | ||
return l + s.length === c && t.startsWith(s, l); | ||
const { src: t, bMarks: n, tShift: i, eMarks: o } = e, c = n[r] + i[r], l = o[r]; | ||
return c + s.length === l && t.startsWith(s, c); | ||
} | ||
@@ -286,4 +286,4 @@ function te(e, r, s) { | ||
e.parentType = "collapsible", e.lineMax = t - 1; | ||
let l = e.push("collapsible_open", "details", 1); | ||
return l.block = !0, l.markup = "<details>", l.map = [r, r + 1], e.md.block.tokenize(e, r + 1, t - 1), l = e.push("collapsible_close", "details", -1), l.block = !0, l.markup = "</details>", e.parentType = i, e.lineMax = o, e.line = t, !0; | ||
let c = e.push("collapsible_open", "details", 1); | ||
return c.block = !0, c.markup = "<details>", c.map = [r, r + 1], e.md.block.tokenize(e, r + 1, t - 1), c = e.push("collapsible_close", "details", -1), c.block = !0, c.markup = "</details>", e.parentType = i, e.lineMax = o, e.line = t, !0; | ||
} | ||
@@ -298,4 +298,4 @@ function ne(e, r, s) { | ||
e.push("summary_open", "summary", 1); | ||
const l = e.push("inline", "", 0); | ||
return l.children = [], l.content = t.slice(n[r], i[o - 1]), e.push("summary_close", "summary", -1), e.line = o + 1, !0; | ||
const c = e.push("inline", "", 0); | ||
return c.children = [], c.content = t.slice(n[r], i[o - 1]), e.push("summary_close", "summary", -1), e.line = o + 1, !0; | ||
} | ||
@@ -326,3 +326,3 @@ function se(e) { | ||
} | ||
function le(e) { | ||
function ce(e) { | ||
const { rules: r } = e.renderer, s = r.code_inline; | ||
@@ -333,6 +333,6 @@ r.code_inline = function(t, n, ...i) { | ||
} | ||
function Ge(e, r = {}) { | ||
return r.plain ? e.use(h) : (r.guest || e.use(ie), e.options.highlight ??= Y, e.use(w), e.use(re), e.use(le)), r.guest ? e.use(oe) : e.use(y), e.use(ae).use(se); | ||
function Ie(e, r = {}) { | ||
return r.plain ? e.use(h) : (r.guest || e.use(ie), e.options.highlight ??= Y, e.use(w), e.use(re), e.use(ce)), r.guest ? e.use(oe) : e.use(y), e.use(ae).use(se); | ||
} | ||
function Ne(e, r) { | ||
function De(e, r) { | ||
function s(t) { | ||
@@ -359,6 +359,5 @@ for (const n of t) | ||
ie as anchor, | ||
V as checkLink, | ||
le as classify, | ||
ce as classify, | ||
se as collapsible, | ||
Ne as collect, | ||
De as collect, | ||
K as defaultDirectiveMap, | ||
@@ -368,8 +367,9 @@ h as directive, | ||
ae as footnote, | ||
Ie as getEmphasis, | ||
He as getEmphasis, | ||
Y as highlight, | ||
Ge as kfmPreset, | ||
Ie as kfmPreset, | ||
re as media, | ||
De as toc, | ||
J as parseGenericDirective, | ||
qe as toc, | ||
oe as ugc | ||
}; |
{ | ||
"name": "@kaciras-blog/markdown", | ||
"version": "3.2.1", | ||
"version": "3.2.2", | ||
"license": "MIT", | ||
@@ -21,4 +21,4 @@ "description": "Kaciras Flavored Markdown", | ||
"dependencies": { | ||
"@kaciras/utilities": "^0.10.4", | ||
"bootstrap-icons": "^1.11.2", | ||
"@kaciras/utilities": "^0.12.1", | ||
"bootstrap-icons": "^1.11.3", | ||
"highlight.js": "^11.9.0", | ||
@@ -33,9 +33,9 @@ "markdown-it": "^14.0.0", | ||
"@types/markdown-it": "^13.0.7", | ||
"@vitest/coverage-v8": "^1.1.1", | ||
"@vitest/coverage-v8": "^1.2.2", | ||
"html-minifier-terser": "^7.2.0", | ||
"magic-string": "^0.30.5", | ||
"magic-string": "^0.30.6", | ||
"postcss-nested": "^6.0.1", | ||
"postcss-simple-vars": "^7.0.1", | ||
"vite": "^5.0.10", | ||
"vitest": "^1.1.1" | ||
"vite": "^5.0.12", | ||
"vitest": "^1.2.2" | ||
}, | ||
@@ -42,0 +42,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
28539
628
+ Added@kaciras/utilities@0.12.4(transitive)
- Removed@kaciras/utilities@0.10.4(transitive)
Updated@kaciras/utilities@^0.12.1
Updatedbootstrap-icons@^1.11.3