@humanonlyweb/writer 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/dist/components/WriterContent.d.ts +57 -0
- package/dist/components/WriterContent.js +114 -0
- package/dist/components/WriterEditor.js +232 -0
- package/dist/components/WriterEditor.vue.d.ts +70 -0
- package/dist/components/WriterToolbar.js +135 -0
- package/dist/components/WriterToolbar.vue.d.ts +27 -0
- package/dist/config.d.ts +39 -0
- package/dist/config.js +41 -0
- package/dist/docs/README.md +24 -0
- package/dist/docs/blocks.md +64 -0
- package/dist/docs/changelog.md +17 -0
- package/dist/docs/components/content.md +51 -0
- package/dist/docs/components/editor.md +236 -0
- package/dist/docs/components/toolbar.md +146 -0
- package/dist/docs/images.md +80 -0
- package/dist/docs/index.md +86 -0
- package/dist/docs/installation.md +74 -0
- package/dist/docs/limitations.md +11 -0
- package/dist/docs/localisation.md +35 -0
- package/dist/docs/parsing.md +47 -0
- package/dist/docs/security.md +16 -0
- package/dist/docs/storing.md +105 -0
- package/dist/docs/styling.md +47 -0
- package/dist/docs/word-count.md +16 -0
- package/dist/editor/commands.d.ts +1 -0
- package/dist/editor/commands.js +359 -0
- package/dist/editor/images.d.ts +1 -0
- package/dist/editor/images.js +400 -0
- package/dist/editor/live.d.ts +2 -0
- package/dist/editor/live.js +411 -0
- package/dist/editor/setup.d.ts +9 -0
- package/dist/editor/setup.js +485 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +8 -0
- package/dist/load-parser.js +12 -0
- package/dist/nuxt/module.d.ts +35 -0
- package/dist/nuxt/module.js +114 -0
- package/dist/nuxt/runtime/plugin.d.ts +2 -0
- package/dist/nuxt/runtime/plugin.js +25 -0
- package/dist/nuxt/runtime/server/parse.d.ts +12 -0
- package/dist/nuxt/runtime/server/parse.js +15 -0
- package/dist/package.js +3 -0
- package/dist/parse.d.ts +16 -0
- package/dist/parse.js +62 -0
- package/dist/parser-options.d.ts +19 -0
- package/dist/placeholder.js +5 -0
- package/dist/render.d.ts +10 -0
- package/dist/render.js +97 -0
- package/dist/sanitize.d.ts +11 -0
- package/dist/sanitize.js +139 -0
- package/dist/stats.d.ts +8 -0
- package/dist/stats.js +17 -0
- package/dist/styles/core.css +275 -0
- package/dist/translations.d.ts +26 -0
- package/dist/translations.js +47 -0
- package/dist/types.d.ts +9 -0
- package/package.json +101 -4
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createWriter } from "../../config.js";
|
|
2
|
+
import { computed } from "vue";
|
|
3
|
+
import { defineNuxtPlugin, useAppConfig } from "nuxt/app";
|
|
4
|
+
import { blocks, components, parser } from "#build/humanonlyweb-writer.mjs";
|
|
5
|
+
// src/nuxt/runtime/plugin.ts
|
|
6
|
+
var plugin_default = defineNuxtPlugin({
|
|
7
|
+
name: "humanonlyweb-writer",
|
|
8
|
+
setup(nuxtApp) {
|
|
9
|
+
const appConfig = useAppConfig();
|
|
10
|
+
const meta = appConfig.writer?.blocks ?? {};
|
|
11
|
+
const withMeta = {};
|
|
12
|
+
const found = blocks;
|
|
13
|
+
for (const [name, component] of Object.entries(found)) withMeta[name] = {
|
|
14
|
+
component,
|
|
15
|
+
...meta[name]
|
|
16
|
+
};
|
|
17
|
+
nuxtApp.vueApp.use(createWriter({
|
|
18
|
+
blocks: withMeta,
|
|
19
|
+
components,
|
|
20
|
+
parser,
|
|
21
|
+
translations: computed(() => appConfig.writer?.translations ?? {})
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
export { plugin_default as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Parse } from "../../../parse.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses Markdown on the server with the module's `parser` options: the same ones the
|
|
4
|
+
* app renders with. Store the result beside the Markdown and render it with
|
|
5
|
+
* `<WriterContent>`, so pages load neither the parser nor the editor.
|
|
6
|
+
*/
|
|
7
|
+
export declare const parseWriterDocument: Parse;
|
|
8
|
+
/**
|
|
9
|
+
* This package's version and a hash of the `parser` options. Store it with each parsed
|
|
10
|
+
* document; one stored with another value is due for parsing again.
|
|
11
|
+
*/
|
|
12
|
+
export declare const writerParserVersion: string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createParser } from "../../../parse.js";
|
|
2
|
+
import options, { version } from "#humanonlyweb-writer/parser";
|
|
3
|
+
// src/nuxt/runtime/server/parse.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parses Markdown on the server with the module's `parser` options: the same ones the
|
|
6
|
+
* app renders with. Store the result beside the Markdown and render it with
|
|
7
|
+
* `<WriterContent>`, so pages load neither the parser nor the editor.
|
|
8
|
+
*/
|
|
9
|
+
const parseWriterDocument = createParser(options);
|
|
10
|
+
/**
|
|
11
|
+
* This package's version and a hash of the `parser` options. Store it with each parsed
|
|
12
|
+
* document; one stored with another value is due for parsing again.
|
|
13
|
+
*/
|
|
14
|
+
const writerParserVersion = version;
|
|
15
|
+
export { parseWriterDocument, writerParserVersion };
|
package/dist/package.js
ADDED
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ParserOptions } from "./parser-options.js";
|
|
2
|
+
import { NATIVE_TAGS, isSafeUrl, sanitize } from "./sanitize.js";
|
|
3
|
+
import { countWords, readingMinutes } from "./stats.js";
|
|
4
|
+
import { MarkdownDocument, MarkdownDocument as MarkdownDocument$1 } from "comark";
|
|
5
|
+
import { Toc, TocLink } from "comark/plugins/toc";
|
|
6
|
+
import { RangiOptions } from "comark/plugins/rangi";
|
|
7
|
+
export type Parse = (markdown: string) => Promise<MarkdownDocument$1>;
|
|
8
|
+
/**
|
|
9
|
+
* A parser for stored Markdown. Raw HTML is text, never markup. Run it on the server
|
|
10
|
+
* when saving to store the document alongside its source, with the same options the
|
|
11
|
+
* app renders with.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createParser(options?: ParserOptions): Parse;
|
|
14
|
+
/** `createParser()` with the defaults. */
|
|
15
|
+
export declare const parse: Parse;
|
|
16
|
+
export { type MarkdownDocument, NATIVE_TAGS, type ParserOptions, type RangiOptions, type Toc, type TocLink, countWords, isSafeUrl, readingMinutes, sanitize };
|
package/dist/parse.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { NATIVE_TAGS, isSafeUrl, sanitize } from "./sanitize.js";
|
|
2
|
+
import { countWords, readingMinutes } from "./stats.js";
|
|
3
|
+
import { createMarkdownParser } from "comark";
|
|
4
|
+
import alert from "comark/plugins/alert";
|
|
5
|
+
import attributes from "comark/plugins/attributes";
|
|
6
|
+
import components from "comark/plugins/components";
|
|
7
|
+
import footnotes from "comark/plugins/footnotes";
|
|
8
|
+
import frontmatter from "comark/plugins/frontmatter";
|
|
9
|
+
import taskList from "comark/plugins/task-list";
|
|
10
|
+
import toc from "comark/plugins/toc";
|
|
11
|
+
// src/parse.ts
|
|
12
|
+
/** Whether `node` is or holds a `tag` element, without serializing it. Children start at index 2. */
|
|
13
|
+
function contains(node, tag) {
|
|
14
|
+
if (!Array.isArray(node)) return false;
|
|
15
|
+
return node[0] === tag || node.some((child, i) => i >= 2 && contains(child, tag));
|
|
16
|
+
}
|
|
17
|
+
function highlighter(options) {
|
|
18
|
+
return {
|
|
19
|
+
name: "writer-highlight",
|
|
20
|
+
async post(state) {
|
|
21
|
+
if (!state.tree.nodes.some((node) => contains(node, "pre"))) return;
|
|
22
|
+
const { rangiCodeBlocks } = await import("comark/plugins/rangi");
|
|
23
|
+
await rangiCodeBlocks(state.tree, options);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const sanitizer = {
|
|
28
|
+
name: "writer-sanitize",
|
|
29
|
+
post(state) {
|
|
30
|
+
state.tree.nodes = sanitize(state.tree.nodes);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* A parser for stored Markdown. Raw HTML is text, never markup. Run it on the server
|
|
35
|
+
* when saving to store the document alongside its source, with the same options the
|
|
36
|
+
* app renders with.
|
|
37
|
+
*/
|
|
38
|
+
function createParser(options = {}) {
|
|
39
|
+
const { footnotes: notes = true, linkify = true, highlight = false, plugins = [] } = options;
|
|
40
|
+
const contents = options.toc ?? false;
|
|
41
|
+
const parser = createMarkdownParser({
|
|
42
|
+
registerDefaultPlugins: false,
|
|
43
|
+
autoUnwrap: false,
|
|
44
|
+
linkify,
|
|
45
|
+
plugins: [
|
|
46
|
+
frontmatter(),
|
|
47
|
+
components(),
|
|
48
|
+
attributes(),
|
|
49
|
+
taskList(),
|
|
50
|
+
alert(),
|
|
51
|
+
...contents ? [toc(contents === true ? {} : contents)] : [],
|
|
52
|
+
...notes ? [footnotes(notes === true ? {} : notes)] : [],
|
|
53
|
+
...highlight ? [highlighter(highlight === true ? {} : highlight)] : [],
|
|
54
|
+
...plugins,
|
|
55
|
+
sanitizer
|
|
56
|
+
]
|
|
57
|
+
});
|
|
58
|
+
return (markdown) => parser(markdown);
|
|
59
|
+
}
|
|
60
|
+
/** `createParser()` with the defaults. */
|
|
61
|
+
const parse = createParser();
|
|
62
|
+
export { NATIVE_TAGS, countWords, createParser, isSafeUrl, parse, readingMinutes, sanitize };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ComarkPlugin } from "comark";
|
|
2
|
+
import { FootnotesConfig } from "comark/plugins/footnotes";
|
|
3
|
+
import { Toc } from "comark/plugins/toc";
|
|
4
|
+
import { RangiOptions } from "comark/plugins/rangi";
|
|
5
|
+
export interface ParserOptions {
|
|
6
|
+
/** `[^1]` footnotes. Pass an object to change the section label or back link. */
|
|
7
|
+
footnotes?: boolean | FootnotesConfig;
|
|
8
|
+
/** Put a table of contents in `document.meta.toc`. Pass `{ depth }` for deeper headings. */
|
|
9
|
+
toc?: boolean | Partial<Toc>;
|
|
10
|
+
/** Turn URL-like text into links. */
|
|
11
|
+
linkify?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Highlight fenced code with rangi (light, no WASM). Pass options for themes or line
|
|
14
|
+
* spans. Its code loads only when a document has code.
|
|
15
|
+
*/
|
|
16
|
+
highlight?: boolean | RangiOptions;
|
|
17
|
+
/** Extra Comark plugins, e.g. `comark/plugins/shiki`. They run before sanitizing. */
|
|
18
|
+
plugins?: ComarkPlugin[];
|
|
19
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// src/placeholder.ts
|
|
2
|
+
/** The URL a file has in the Markdown while it uploads. */
|
|
3
|
+
const UPLOAD_PLACEHOLDER = "#upload-";
|
|
4
|
+
const isUploadPlaceholder = (url) => typeof url === "string" && url.startsWith("#upload-");
|
|
5
|
+
export { UPLOAD_PLACEHOLDER, isUploadPlaceholder };
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Component, VNodeArrayChildren } from "vue";
|
|
2
|
+
import { MarkdownDocument } from "comark";
|
|
3
|
+
export interface RenderOptions {
|
|
4
|
+
/** Registered blocks by Markdown name. The only non-native tags that render. */
|
|
5
|
+
blocks: Readonly<Record<string, Component>>;
|
|
6
|
+
/** Overrides for native elements. */
|
|
7
|
+
components: Readonly<Record<string, Component>>;
|
|
8
|
+
}
|
|
9
|
+
/** Renders a parsed document as Vue nodes. Only native Markdown elements and `blocks` render. */
|
|
10
|
+
export declare function renderDocument(document: MarkdownDocument, options: RenderOptions): VNodeArrayChildren;
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { isUploadPlaceholder } from "./placeholder.js";
|
|
2
|
+
import { NATIVE_TAGS, sanitize } from "./sanitize.js";
|
|
3
|
+
import { resolveAttributes, sanitizeData, toNativeAttributes } from "comark/utils";
|
|
4
|
+
import { Fragment, h } from "vue";
|
|
5
|
+
// src/render.ts
|
|
6
|
+
const childrenOf = (node) => node.slice(2);
|
|
7
|
+
const own = (map, key) => key !== void 0 && Object.hasOwn(map, key) ? map[key] : void 0;
|
|
8
|
+
/** The prop a block declares to receive its raw node, as Comark's renderers name it. */
|
|
9
|
+
const NODE_PROP = "__node";
|
|
10
|
+
const declaresNode = (component) => typeof component === "object" && "props" in component && !!component.props && typeof component.props === "object" && "__node" in component.props;
|
|
11
|
+
/** A named slot: `#title` in a block parses to `<template name="title">`. */
|
|
12
|
+
function slotOf(node) {
|
|
13
|
+
if (typeof node === "string" || node[0] !== "template") return void 0;
|
|
14
|
+
const props = node[1];
|
|
15
|
+
const name = typeof props.name === "string" ? props.name : Object.keys(props).find((k) => k.startsWith("v-slot:"))?.slice(7);
|
|
16
|
+
return name ? {
|
|
17
|
+
name,
|
|
18
|
+
children: childrenOf(node)
|
|
19
|
+
} : void 0;
|
|
20
|
+
}
|
|
21
|
+
/** The text of `nodes`, for accessible names. */
|
|
22
|
+
function plainText(nodes) {
|
|
23
|
+
return nodes.map((node) => typeof node === "string" ? node : node[0] === null ? "" : plainText(childrenOf(node))).join("");
|
|
24
|
+
}
|
|
25
|
+
const isCheckbox = (node) => typeof node !== "string" && !!node && node[0] === "input" && node[1].type === "checkbox";
|
|
26
|
+
/** A task list checkbox, named by the text that follows it: `- [x] Buy salt`. */
|
|
27
|
+
function labelled(checkbox, rest) {
|
|
28
|
+
if (checkbox[1]["aria-label"] !== void 0) return checkbox;
|
|
29
|
+
const label = plainText(rest).trim();
|
|
30
|
+
return label ? [
|
|
31
|
+
"input",
|
|
32
|
+
{
|
|
33
|
+
...checkbox[1],
|
|
34
|
+
"aria-label": label
|
|
35
|
+
},
|
|
36
|
+
...childrenOf(checkbox)
|
|
37
|
+
] : checkbox;
|
|
38
|
+
}
|
|
39
|
+
function renderNode(node, options, data, key, inPre) {
|
|
40
|
+
if (typeof node === "string") return node;
|
|
41
|
+
if (node[0] === null) return null;
|
|
42
|
+
const [tag, attributes] = node;
|
|
43
|
+
const children = childrenOf(node).map((child, i, all) => isCheckbox(child) ? labelled(child, all.slice(i + 1)) : child);
|
|
44
|
+
const as = typeof attributes.as === "string" ? attributes.as : void 0;
|
|
45
|
+
const component = inPre ? void 0 : own(options.blocks, as) ?? own(options.components, tag) ?? own(options.blocks, tag);
|
|
46
|
+
let props = {};
|
|
47
|
+
for (const [k, value] of Object.entries(resolveAttributes(attributes, data, { parseJson: true }))) props[k === "className" ? "class" : k] = value;
|
|
48
|
+
if (!component) props = toNativeAttributes(tag, node, props);
|
|
49
|
+
else if (declaresNode(component)) props[NODE_PROP] = node;
|
|
50
|
+
if (tag === "img" && (!props.src || isUploadPlaceholder(props.src))) return null;
|
|
51
|
+
if (tag === "a" && isUploadPlaceholder(props.href)) {
|
|
52
|
+
const text = children.map((child, i) => renderNode(child, options, data, i, inPre));
|
|
53
|
+
return h(Fragment, { key }, text.filter((child) => child !== null));
|
|
54
|
+
}
|
|
55
|
+
if (tag === "img") {
|
|
56
|
+
props.loading ??= "lazy";
|
|
57
|
+
props.decoding ??= "async";
|
|
58
|
+
}
|
|
59
|
+
if (as && component) delete props.as;
|
|
60
|
+
props.key = key;
|
|
61
|
+
const scope = Object.keys(attributes).some((k) => k !== "$") ? {
|
|
62
|
+
...data,
|
|
63
|
+
props
|
|
64
|
+
} : data;
|
|
65
|
+
const childInPre = inPre || tag === "pre";
|
|
66
|
+
const renderChildren = (nodes) => nodes.map((child, i) => renderNode(child, options, scope, i, childInPre)).filter((child) => child !== null);
|
|
67
|
+
const image = children[0];
|
|
68
|
+
if (tag === "p" && !component && children.length === 1 && typeof image !== "string" && image?.[0] === "img" && typeof image[1].title === "string" && !!image[1].src && !isUploadPlaceholder(image[1].src)) {
|
|
69
|
+
const { title, ...rest } = image[1];
|
|
70
|
+
return h("figure", { key }, [renderNode(["img", rest], options, scope, 0, childInPre), h("figcaption", null, title)]);
|
|
71
|
+
}
|
|
72
|
+
if (!component) return h(tag, props, children.length ? renderChildren(children) : void 0);
|
|
73
|
+
const slots = {};
|
|
74
|
+
const rest = [];
|
|
75
|
+
for (const child of children) {
|
|
76
|
+
const slot = slotOf(child);
|
|
77
|
+
if (slot) slots[slot.name] = () => renderChildren(slot.children);
|
|
78
|
+
else rest.push(child);
|
|
79
|
+
}
|
|
80
|
+
if (rest.length) slots.default ??= () => renderChildren(rest);
|
|
81
|
+
return h(component, props, slots);
|
|
82
|
+
}
|
|
83
|
+
/** Renders a parsed document as Vue nodes. Only native Markdown elements and `blocks` render. */
|
|
84
|
+
function renderDocument(document, options) {
|
|
85
|
+
const isAllowed = (tag) => NATIVE_TAGS.has(tag) || Object.hasOwn(options.blocks, tag);
|
|
86
|
+
const nodes = sanitize(document.nodes ?? [], isAllowed);
|
|
87
|
+
const frontmatter = structuredClone(document.frontmatter ?? {});
|
|
88
|
+
sanitizeData(frontmatter);
|
|
89
|
+
const data = {
|
|
90
|
+
frontmatter,
|
|
91
|
+
meta: document.meta ?? {},
|
|
92
|
+
data: {},
|
|
93
|
+
props: {}
|
|
94
|
+
};
|
|
95
|
+
return nodes.map((node, i) => renderNode(node, options, data, i, false)).filter((node) => node !== null);
|
|
96
|
+
}
|
|
97
|
+
export { renderDocument };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Node } from "comark";
|
|
2
|
+
/** Elements the default Markdown syntax produces. Anything else must be a registered block. */
|
|
3
|
+
export declare const NATIVE_TAGS: ReadonlySet<string>;
|
|
4
|
+
/** A relative URL, a fragment, or an http(s)/mailto/tel URL. */
|
|
5
|
+
export declare function isSafeUrl(value: string): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Returns a safe copy of `nodes`: no executable tags, event handlers or unsafe URLs.
|
|
8
|
+
* With `isAllowed`, any other tag it rejects is replaced by its children, so unknown
|
|
9
|
+
* components never render but their text stays.
|
|
10
|
+
*/
|
|
11
|
+
export declare function sanitize(nodes: readonly Node[], isAllowed?: (tag: string) => boolean): Node[];
|
package/dist/sanitize.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// src/sanitize.ts
|
|
2
|
+
/** Elements the default Markdown syntax produces. Anything else must be a registered block. */
|
|
3
|
+
const NATIVE_TAGS = /* @__PURE__ */ new Set([
|
|
4
|
+
"a",
|
|
5
|
+
"blockquote",
|
|
6
|
+
"br",
|
|
7
|
+
"code",
|
|
8
|
+
"del",
|
|
9
|
+
"em",
|
|
10
|
+
"h1",
|
|
11
|
+
"h2",
|
|
12
|
+
"h3",
|
|
13
|
+
"h4",
|
|
14
|
+
"h5",
|
|
15
|
+
"h6",
|
|
16
|
+
"hr",
|
|
17
|
+
"img",
|
|
18
|
+
"input",
|
|
19
|
+
"kbd",
|
|
20
|
+
"li",
|
|
21
|
+
"mark",
|
|
22
|
+
"ol",
|
|
23
|
+
"p",
|
|
24
|
+
"pre",
|
|
25
|
+
"s",
|
|
26
|
+
"section",
|
|
27
|
+
"span",
|
|
28
|
+
"strong",
|
|
29
|
+
"sub",
|
|
30
|
+
"sup",
|
|
31
|
+
"table",
|
|
32
|
+
"tbody",
|
|
33
|
+
"td",
|
|
34
|
+
"template",
|
|
35
|
+
"tfoot",
|
|
36
|
+
"th",
|
|
37
|
+
"thead",
|
|
38
|
+
"tr",
|
|
39
|
+
"ul"
|
|
40
|
+
]);
|
|
41
|
+
/** Removed with their content: running it, or showing it as text, is never what the author meant. */
|
|
42
|
+
const DROPPED_TAGS = /* @__PURE__ */ new Set([
|
|
43
|
+
"base",
|
|
44
|
+
"embed",
|
|
45
|
+
"frame",
|
|
46
|
+
"frameset",
|
|
47
|
+
"iframe",
|
|
48
|
+
"link",
|
|
49
|
+
"meta",
|
|
50
|
+
"noscript",
|
|
51
|
+
"object",
|
|
52
|
+
"script",
|
|
53
|
+
"style",
|
|
54
|
+
"textarea",
|
|
55
|
+
"title"
|
|
56
|
+
]);
|
|
57
|
+
const URL_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
58
|
+
"href",
|
|
59
|
+
"src",
|
|
60
|
+
"poster",
|
|
61
|
+
"cite",
|
|
62
|
+
"action",
|
|
63
|
+
"background",
|
|
64
|
+
"srcset"
|
|
65
|
+
]);
|
|
66
|
+
const BLOCKED_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
67
|
+
"srcdoc",
|
|
68
|
+
"formaction",
|
|
69
|
+
"is",
|
|
70
|
+
"innerhtml",
|
|
71
|
+
"outerhtml",
|
|
72
|
+
"textcontent"
|
|
73
|
+
]);
|
|
74
|
+
const SCHEME = /^([a-z][a-z\d+.-]*):/i;
|
|
75
|
+
const SAFE_SCHEMES = /* @__PURE__ */ new Set([
|
|
76
|
+
"http",
|
|
77
|
+
"https",
|
|
78
|
+
"mailto",
|
|
79
|
+
"tel"
|
|
80
|
+
]);
|
|
81
|
+
const DATA_IMAGE = /^data:image\/(?:png|jpe?g|gif|webp|avif|svg\+xml);base64,/i;
|
|
82
|
+
const CODE_STYLE = /^(?:\s*(?:color|background-color|font-style|font-weight|text-decoration|--[\w-]+)\s*:\s*[^;:()]*;?)*\s*$/i;
|
|
83
|
+
/** A relative URL, a fragment, or an http(s)/mailto/tel URL. */
|
|
84
|
+
function isSafeUrl(value) {
|
|
85
|
+
const scheme = SCHEME.exec(value.replace(/[\u0000- \u007F]/g, ""))?.[1];
|
|
86
|
+
return !scheme || SAFE_SCHEMES.has(scheme.toLowerCase());
|
|
87
|
+
}
|
|
88
|
+
function cleanAttributes(node, attributes, inCode) {
|
|
89
|
+
const result = {};
|
|
90
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
91
|
+
if (key === "$") {
|
|
92
|
+
result.$ = attributes.$;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const bound = key.startsWith(":");
|
|
96
|
+
const name = (bound ? key.slice(1) : key).toLowerCase();
|
|
97
|
+
if (name.startsWith("on") || BLOCKED_ATTRIBUTES.has(name)) continue;
|
|
98
|
+
if (name === "style" && !(inCode && typeof value === "string" && CODE_STYLE.test(value))) continue;
|
|
99
|
+
if (URL_ATTRIBUTES.has(name)) {
|
|
100
|
+
if (bound || typeof value !== "string") continue;
|
|
101
|
+
const urls = name === "srcset" ? value.split(",").map((part) => part.trim().split(/\s+/)[0] ?? "") : [value];
|
|
102
|
+
if (!(node === "img" && name === "src" && DATA_IMAGE.test(value)) && !urls.every(isSafeUrl)) continue;
|
|
103
|
+
}
|
|
104
|
+
result[key] = value;
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns a safe copy of `nodes`: no executable tags, event handlers or unsafe URLs.
|
|
110
|
+
* With `isAllowed`, any other tag it rejects is replaced by its children, so unknown
|
|
111
|
+
* components never render but their text stays.
|
|
112
|
+
*/
|
|
113
|
+
function sanitize(nodes, isAllowed) {
|
|
114
|
+
return clean(nodes, isAllowed, false);
|
|
115
|
+
}
|
|
116
|
+
function clean(nodes, isAllowed, inCode) {
|
|
117
|
+
const result = [];
|
|
118
|
+
for (const node of nodes) {
|
|
119
|
+
if (typeof node === "string" || node[0] === null) {
|
|
120
|
+
result.push(node);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const tag = node[0].toLowerCase();
|
|
124
|
+
if (DROPPED_TAGS.has(tag)) continue;
|
|
125
|
+
if (tag === "input" && node[1]?.type !== "checkbox") continue;
|
|
126
|
+
const children = clean(node.slice(2), isAllowed, inCode || tag === "pre");
|
|
127
|
+
if (isAllowed && !isAllowed(tag)) {
|
|
128
|
+
result.push(...children);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
result.push([
|
|
132
|
+
tag,
|
|
133
|
+
cleanAttributes(tag, node[1] ?? {}, inCode),
|
|
134
|
+
...children
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
export { NATIVE_TAGS, isSafeUrl, sanitize };
|
package/dist/stats.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Words a reader sees in Markdown: link text but not URLs, no image syntax, code,
|
|
3
|
+
* frontmatter, block fences, attributes or footnote labels. Synchronous and cheap
|
|
4
|
+
* enough to run on every keystroke.
|
|
5
|
+
*/
|
|
6
|
+
export declare function countWords(markdown: string): number;
|
|
7
|
+
/** Minutes to read `words`, at least one. */
|
|
8
|
+
export declare const readingMinutes: (words: number, wordsPerMinute?: number) => number;
|
package/dist/stats.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/stats.ts
|
|
2
|
+
const WORD = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]|[\p{L}\p{N}]+(?:['’][\p{L}\p{N}]+)*/gu;
|
|
3
|
+
/** Words in plain text. */
|
|
4
|
+
function countTextWords(text) {
|
|
5
|
+
return text.match(WORD)?.length ?? 0;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Words a reader sees in Markdown: link text but not URLs, no image syntax, code,
|
|
9
|
+
* frontmatter, block fences, attributes or footnote labels. Synchronous and cheap
|
|
10
|
+
* enough to run on every keystroke.
|
|
11
|
+
*/
|
|
12
|
+
function countWords(markdown) {
|
|
13
|
+
return countTextWords(markdown.replace(/^---\r?\n[\s\S]*?\r?\n---(?:\r?\n|$)/, " ").replace(/^ {0,3}(`{3,}|~{3,})[^\n]*\n[\s\S]*?(?:^ {0,3}\1[ \t]*$|(?![\s\S]))/gm, " ").replace(/(?<=[)\]*_~`])\{[^}\n]*\}/g, "").replace(/[ \t]\{[#.][^}\n]*\}[ \t]*$/gm, " ").replace(/`[^`\n]*`/g, " ").replace(/!\[[^\]\n]*\]\([^)\n]*\)/g, " ").replace(/\[([^\]\n]*)\]\([^)\n]*\)/g, "$1").replace(/\[\^[^\]\s]+\]:?/g, " ").replace(/^\s*:{2,}.*$/gm, " ").replace(/<https?:[^>\s]+>|https?:\/\/\S+/g, " "));
|
|
14
|
+
}
|
|
15
|
+
/** Minutes to read `words`, at least one. */
|
|
16
|
+
const readingMinutes = (words, wordsPerMinute = 230) => Math.max(1, Math.round(words / wordsPerMinute));
|
|
17
|
+
export { countWords, readingMinutes };
|