@humanonlyweb/writer 0.0.0-stage → 0.1.0
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 +355 -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 +38 -0
- package/dist/config.js +41 -0
- package/dist/editor/commands.d.ts +1 -0
- package/dist/editor/commands.js +294 -0
- package/dist/editor/images.d.ts +1 -0
- package/dist/editor/images.js +388 -0
- package/dist/editor/live.d.ts +2 -0
- package/dist/editor/live.js +410 -0
- package/dist/editor/setup.d.ts +9 -0
- package/dist/editor/setup.js +466 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +8 -0
- package/dist/load-parser.js +12 -0
- package/dist/nuxt/module.d.ts +38 -0
- package/dist/nuxt/module.js +92 -0
- package/dist/nuxt/runtime/plugin.d.ts +2 -0
- package/dist/nuxt/runtime/plugin.js +24 -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 +31 -0
- package/dist/parse.js +62 -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/docs/api.md +258 -0
- package/package.json +96 -4
package/dist/package.js
ADDED
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { NATIVE_TAGS, isSafeUrl, sanitize } from "./sanitize.js";
|
|
2
|
+
import { countWords, readingMinutes } from "./stats.js";
|
|
3
|
+
import { ComarkPlugin, MarkdownDocument, MarkdownDocument as MarkdownDocument$1 } from "comark";
|
|
4
|
+
import { FootnotesConfig } from "comark/plugins/footnotes";
|
|
5
|
+
import { Toc, Toc as Toc$1, TocLink } from "comark/plugins/toc";
|
|
6
|
+
import { RangiOptions, RangiOptions as RangiOptions$1 } from "comark/plugins/rangi";
|
|
7
|
+
export interface ParserOptions {
|
|
8
|
+
/** `[^1]` footnotes. Pass an object to change the section label or back link. */
|
|
9
|
+
footnotes?: boolean | FootnotesConfig;
|
|
10
|
+
/** Put a table of contents in `document.meta.toc`. Pass `{ depth }` for deeper headings. */
|
|
11
|
+
toc?: boolean | Partial<Toc$1>;
|
|
12
|
+
/** Turn URL-like text into links. */
|
|
13
|
+
linkify?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Highlight fenced code with rangi (light, no WASM). Pass options for themes or line
|
|
16
|
+
* spans. Its code loads only when a document has code.
|
|
17
|
+
*/
|
|
18
|
+
highlight?: boolean | RangiOptions$1;
|
|
19
|
+
/** Extra Comark plugins, e.g. `comark/plugins/shiki`. They run before sanitizing. */
|
|
20
|
+
plugins?: ComarkPlugin[];
|
|
21
|
+
}
|
|
22
|
+
export type Parse = (markdown: string) => Promise<MarkdownDocument$1>;
|
|
23
|
+
/**
|
|
24
|
+
* A parser for stored Markdown. Raw HTML is text, never markup. Run it on the server
|
|
25
|
+
* when saving to store the document alongside its source, with the same options the
|
|
26
|
+
* app renders with.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createParser(options?: ParserOptions): Parse;
|
|
29
|
+
/** `createParser()` with the defaults. */
|
|
30
|
+
export declare const parse: Parse;
|
|
31
|
+
export { type MarkdownDocument, NATIVE_TAGS, 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,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 };
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/* @humanonlyweb/writer core styles: layout and Markdown source only, in a layer your CSS overrides. */
|
|
2
|
+
@layer humanonlyweb-writer {
|
|
3
|
+
[data-part="writer-editor"] {
|
|
4
|
+
display: grid;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/* The server-rendered textarea, until the editor loads. */
|
|
8
|
+
[data-part="writer-fallback"] {
|
|
9
|
+
box-sizing: border-box;
|
|
10
|
+
inline-size: 100%;
|
|
11
|
+
min-block-size: 10lh;
|
|
12
|
+
field-sizing: content;
|
|
13
|
+
padding: 0;
|
|
14
|
+
border: 0;
|
|
15
|
+
background: transparent;
|
|
16
|
+
color: inherit;
|
|
17
|
+
font: inherit;
|
|
18
|
+
resize: vertical;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
[data-part="writer-fallback"]:focus-visible,
|
|
22
|
+
[data-part="writer-host"]:has(.cm-focused) {
|
|
23
|
+
outline: 2px solid Highlight;
|
|
24
|
+
outline-offset: 2px;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
[data-part="writer-host"] .cm-content {
|
|
28
|
+
min-block-size: 10lh;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* Markdown source: emphasis shows as itself, syntax marks recede. */
|
|
32
|
+
.cm-md-strong {
|
|
33
|
+
font-weight: bold;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.cm-md-em {
|
|
37
|
+
font-style: italic;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.cm-md-strike {
|
|
41
|
+
text-decoration: line-through;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
* Shared type scale: the editor's Markdown source and rendered content read the same
|
|
46
|
+
* tokens, so a heading is the same size in both. Defaults are the browser's own. Set
|
|
47
|
+
* them once, on any ancestor, to match your site:
|
|
48
|
+
* :root { --writer-h1: 2.5rem; --writer-code-font: "JetBrains Mono", monospace; }
|
|
49
|
+
*/
|
|
50
|
+
.cm-md-code,
|
|
51
|
+
.cm-md-codeblock,
|
|
52
|
+
[data-part="writer-content"] :where(code, kbd, pre) {
|
|
53
|
+
font-family: var(--writer-code-font, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
|
|
54
|
+
font-size: var(--writer-code-size, 0.875em);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* Code inside a code block is already sized by the block. */
|
|
58
|
+
.cm-md-codeblock .cm-md-code,
|
|
59
|
+
[data-part="writer-content"] :where(pre code) {
|
|
60
|
+
font-size: inherit;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.cm-md-link {
|
|
64
|
+
text-decoration: underline;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.cm-md-mark,
|
|
68
|
+
.cm-md-comment {
|
|
69
|
+
opacity: 0.6;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
:where(.cm-md-h1, .cm-md-h2, .cm-md-h3, .cm-md-h4, .cm-md-h5, .cm-md-h6) {
|
|
73
|
+
font-weight: var(--writer-heading-weight, bold);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
[data-part="writer-content"] :where(h1, h2, h3, h4, h5, h6) {
|
|
77
|
+
font-weight: var(--writer-heading-weight, bold);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
:where(.cm-md-h1),
|
|
81
|
+
[data-part="writer-content"] :where(h1) {
|
|
82
|
+
font-size: var(--writer-h1, 2em);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
:where(.cm-md-h2),
|
|
86
|
+
[data-part="writer-content"] :where(h2) {
|
|
87
|
+
font-size: var(--writer-h2, 1.5em);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
:where(.cm-md-h3),
|
|
91
|
+
[data-part="writer-content"] :where(h3) {
|
|
92
|
+
font-size: var(--writer-h3, 1.17em);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
:where(.cm-md-h4),
|
|
96
|
+
[data-part="writer-content"] :where(h4) {
|
|
97
|
+
font-size: var(--writer-h4, 1em);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
:where(.cm-md-h5),
|
|
101
|
+
[data-part="writer-content"] :where(h5) {
|
|
102
|
+
font-size: var(--writer-h5, 0.83em);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
:where(.cm-md-h6),
|
|
106
|
+
[data-part="writer-content"] :where(h6) {
|
|
107
|
+
font-size: var(--writer-h6, 0.67em);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* Images never overflow their column. */
|
|
111
|
+
[data-part="writer-content"] :where(img) {
|
|
112
|
+
max-inline-size: 100%;
|
|
113
|
+
block-size: auto;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* Live preview: rendered blocks and images, and formatting standing in for syntax. */
|
|
117
|
+
.cm-md-preview {
|
|
118
|
+
display: flow-root;
|
|
119
|
+
white-space: normal;
|
|
120
|
+
cursor: text;
|
|
121
|
+
border-radius: 4px;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.cm-md-preview:hover {
|
|
125
|
+
outline: 1px dashed color-mix(in srgb, currentColor 30%, transparent);
|
|
126
|
+
outline-offset: 4px;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
[data-part="writer-content"] :where(blockquote) {
|
|
130
|
+
margin-inline: 0;
|
|
131
|
+
padding-inline-start: var(--writer-quote-indent, 1em);
|
|
132
|
+
border-inline-start: var(--writer-quote-border-width, 3px) solid
|
|
133
|
+
var(--writer-quote-border-color, color-mix(in srgb, currentColor 25%, transparent));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
.cm-md-hr {
|
|
137
|
+
background: linear-gradient(color-mix(in srgb, currentColor 25%, transparent) 0 0) center / 100%
|
|
138
|
+
1px no-repeat;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* A block open for editing: framed (--writer-block-inset, --writer-block-background), its fence lines small. */
|
|
142
|
+
.cm-md-block-fence {
|
|
143
|
+
font-family: var(--writer-code-font, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
|
|
144
|
+
font-size: 0.75em;
|
|
145
|
+
opacity: 0.55;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* Image cards: the published figure, with its caption and alt text editable. */
|
|
149
|
+
.cm-md-image {
|
|
150
|
+
display: flow-root;
|
|
151
|
+
white-space: normal;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.cm-md-image-frame {
|
|
155
|
+
position: relative;
|
|
156
|
+
display: grid;
|
|
157
|
+
border-radius: 4px;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.cm-md-image-frame img {
|
|
161
|
+
display: block;
|
|
162
|
+
inline-size: 100%;
|
|
163
|
+
block-size: auto;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.cm-md-image-frame[data-empty] {
|
|
167
|
+
aspect-ratio: 16 / 9;
|
|
168
|
+
place-items: center;
|
|
169
|
+
border: 1px dashed color-mix(in srgb, currentColor 35%, transparent);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.cm-md-image-frame[data-dropping] {
|
|
173
|
+
outline: 2px dashed Highlight;
|
|
174
|
+
outline-offset: -2px;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/* display: block above would otherwise beat the hidden attribute and take a grid row. */
|
|
178
|
+
.cm-md-image-frame img[hidden],
|
|
179
|
+
.cm-md-image-frame:not([data-empty]) .cm-md-image-empty,
|
|
180
|
+
.cm-md-image-frame:not([data-uploading]) .cm-md-image-status,
|
|
181
|
+
.cm-md-image-frame[data-empty] .cm-md-image-actions {
|
|
182
|
+
display: none;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
.cm-md-image-empty {
|
|
186
|
+
display: flex;
|
|
187
|
+
flex-wrap: wrap;
|
|
188
|
+
gap: 0.5em;
|
|
189
|
+
align-items: baseline;
|
|
190
|
+
justify-content: center;
|
|
191
|
+
padding: 1em;
|
|
192
|
+
font-size: 0.85em;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.cm-md-image-status,
|
|
196
|
+
.cm-md-image-actions {
|
|
197
|
+
position: absolute;
|
|
198
|
+
inset-block-start: 0.5rem;
|
|
199
|
+
display: flex;
|
|
200
|
+
gap: 0.25rem;
|
|
201
|
+
font-size: 0.8rem;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.cm-md-image-status {
|
|
205
|
+
inset-inline-start: 0.5rem;
|
|
206
|
+
padding: 0.15em 0.5em;
|
|
207
|
+
border-radius: 3px;
|
|
208
|
+
background: rgb(0 0 0 / 0.6);
|
|
209
|
+
color: #fff;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
.cm-md-image-actions {
|
|
213
|
+
inset-inline-end: 0.5rem;
|
|
214
|
+
opacity: 0;
|
|
215
|
+
transition: opacity 0.15s;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
.cm-md-image:is(:hover, :focus-within, [data-selected]) .cm-md-image-actions {
|
|
219
|
+
opacity: 1;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
.cm-md-image[data-selected] .cm-md-image-frame {
|
|
223
|
+
outline: 2px solid Highlight;
|
|
224
|
+
outline-offset: 2px;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/* Caption and alt fields read as text, not form fields. */
|
|
228
|
+
.cm-md-image :is(.cm-md-image-caption, .cm-md-image-alt input) {
|
|
229
|
+
box-sizing: border-box;
|
|
230
|
+
inline-size: 100%;
|
|
231
|
+
padding: 0;
|
|
232
|
+
border: 0;
|
|
233
|
+
background: transparent;
|
|
234
|
+
color: inherit;
|
|
235
|
+
font: inherit;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
.cm-md-image-caption:focus,
|
|
239
|
+
.cm-md-image-alt input:focus {
|
|
240
|
+
outline: none;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.cm-md-image-alt {
|
|
244
|
+
display: flex;
|
|
245
|
+
gap: 0.5em;
|
|
246
|
+
align-items: baseline;
|
|
247
|
+
margin-block-start: 0.25em;
|
|
248
|
+
font-size: 0.8em;
|
|
249
|
+
opacity: 0.75;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
.cm-md-image-alt:focus-within {
|
|
253
|
+
opacity: 1;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
.cm-md-image-alt span {
|
|
257
|
+
flex: none;
|
|
258
|
+
font-weight: 600;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
.cm-md-task {
|
|
262
|
+
margin: 0 0.4em 0 0;
|
|
263
|
+
vertical-align: middle;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
[data-part="writer-toolbar"] {
|
|
267
|
+
display: flex;
|
|
268
|
+
flex-wrap: wrap;
|
|
269
|
+
gap: 4px;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
[data-part="writer-toolbar-item"][aria-disabled="true"] {
|
|
273
|
+
opacity: 0.5;
|
|
274
|
+
}
|
|
275
|
+
}
|