@futaba-soft/mbox 0.0.0-stage → 0.2.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 +80 -2
- package/dist/create-editor-BKTpQ8TK.js +2866 -0
- package/dist/editor/create-editor.d.ts +100 -0
- package/dist/editor/document-cache.d.ts +20 -0
- package/dist/editor/focused-line.d.ts +14 -0
- package/dist/editor/plugin.d.ts +191 -0
- package/dist/editor/test-context.d.ts +28 -0
- package/dist/gfm.d.ts +34 -0
- package/dist/gfm.js +1719 -0
- package/dist/hatena.d.ts +32 -0
- package/dist/hatena.js +856 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +77 -0
- package/dist/plugin-DI0ubB-M.js +297 -0
- package/dist/plugins/bracket-pair-kit.d.ts +9 -0
- package/dist/plugins/code-block-kit.d.ts +77 -0
- package/dist/plugins/code-block-language-loader.d.ts +33 -0
- package/dist/plugins/gfm/active-lines.d.ts +11 -0
- package/dist/plugins/gfm/code-block.d.ts +28 -0
- package/dist/plugins/gfm/footnote.d.ts +17 -0
- package/dist/plugins/gfm/heading.d.ts +12 -0
- package/dist/plugins/gfm/image.d.ts +30 -0
- package/dist/plugins/gfm/index.d.ts +19 -0
- package/dist/plugins/gfm/inline-code.d.ts +4 -0
- package/dist/plugins/gfm/inline.d.ts +4 -0
- package/dist/plugins/gfm/link.d.ts +34 -0
- package/dist/plugins/gfm/list.d.ts +32 -0
- package/dist/plugins/gfm/math.d.ts +39 -0
- package/dist/plugins/gfm/parser-cache.d.ts +3 -0
- package/dist/plugins/gfm/parser.d.ts +355 -0
- package/dist/plugins/gfm/quote.d.ts +3 -0
- package/dist/plugins/gfm/table.d.ts +34 -0
- package/dist/plugins/gfm/thematic-break.d.ts +9 -0
- package/dist/plugins/hatena/blockquote.d.ts +12 -0
- package/dist/plugins/hatena/definition.d.ts +5 -0
- package/dist/plugins/hatena/footnote.d.ts +15 -0
- package/dist/plugins/hatena/heading.d.ts +8 -0
- package/dist/plugins/hatena/index.d.ts +12 -0
- package/dist/plugins/hatena/link.d.ts +16 -0
- package/dist/plugins/hatena/list.d.ts +24 -0
- package/dist/plugins/hatena/parser-cache.d.ts +5 -0
- package/dist/plugins/hatena/parser.d.ts +129 -0
- package/dist/plugins/hatena/pre.d.ts +3 -0
- package/dist/plugins/hatena/shared.d.ts +31 -0
- package/dist/plugins/hatena/super-pre.d.ts +23 -0
- package/dist/plugins/hatena/table.d.ts +12 -0
- package/dist/plugins/image-kit.d.ts +84 -0
- package/dist/plugins/inline-notation-kit.d.ts +39 -0
- package/dist/plugins/inline-style-kit.d.ts +38 -0
- package/dist/plugins/link-url.d.ts +1 -0
- package/dist/plugins/logical-lines.d.ts +18 -0
- package/dist/plugins/mbox/bracket.d.ts +4 -0
- package/dist/plugins/mbox/code-block.d.ts +32 -0
- package/dist/plugins/mbox/complete.d.ts +33 -0
- package/dist/plugins/mbox/default.d.ts +13 -0
- package/dist/plugins/mbox/emoji.d.ts +3 -0
- package/dist/plugins/mbox/footnote.d.ts +14 -0
- package/dist/plugins/mbox/heading.d.ts +10 -0
- package/dist/plugins/mbox/inline-code.d.ts +6 -0
- package/dist/plugins/mbox/inline-style.d.ts +7 -0
- package/dist/plugins/mbox/inline-visitor.d.ts +8 -0
- package/dist/plugins/mbox/list.d.ts +13 -0
- package/dist/plugins/mbox/markdown-image.d.ts +13 -0
- package/dist/plugins/mbox/page-link.d.ts +19 -0
- package/dist/plugins/mbox/paragraph.d.ts +6 -0
- package/dist/plugins/mbox/parser-cache.d.ts +3 -0
- package/dist/plugins/mbox/parser.d.ts +2 -0
- package/dist/plugins/mbox/quote.d.ts +5 -0
- package/dist/plugins/mbox/table.d.ts +13 -0
- package/dist/plugins/mbox/task-list.d.ts +19 -0
- package/dist/plugins/mbox/types.d.ts +172 -0
- package/dist/plugins/mbox/url-link.d.ts +3 -0
- package/dist/plugins/mbox/vertical-motion.d.ts +3 -0
- package/dist/plugins/pipe-table-row.d.ts +18 -0
- package/dist/plugins/scrapbox/bracket.d.ts +20 -0
- package/dist/plugins/scrapbox/code-block.d.ts +37 -0
- package/dist/plugins/scrapbox/command-line.d.ts +9 -0
- package/dist/plugins/scrapbox/complete.d.ts +58 -0
- package/dist/plugins/scrapbox/decoration.d.ts +16 -0
- package/dist/plugins/scrapbox/formula.d.ts +43 -0
- package/dist/plugins/scrapbox/image.d.ts +62 -0
- package/dist/plugins/scrapbox/indent.d.ts +40 -0
- package/dist/plugins/scrapbox/index.d.ts +41 -0
- package/dist/plugins/scrapbox/inline-code-highlight.d.ts +16 -0
- package/dist/plugins/scrapbox/inline-code.d.ts +7 -0
- package/dist/plugins/scrapbox/inline-scanner-kit.d.ts +66 -0
- package/dist/plugins/scrapbox/inline-walker.d.ts +2 -0
- package/dist/plugins/scrapbox/inline.d.ts +86 -0
- package/dist/plugins/scrapbox/link.d.ts +110 -0
- package/dist/plugins/scrapbox/list.d.ts +21 -0
- package/dist/plugins/scrapbox/parser-cache.d.ts +5 -0
- package/dist/plugins/scrapbox/parser.d.ts +65 -0
- package/dist/plugins/scrapbox/quote.d.ts +7 -0
- package/dist/plugins/scrapbox/table.d.ts +22 -0
- package/dist/plugins/scrapbox/title.d.ts +3 -0
- package/dist/plugins/scrapbox/visible-lines.d.ts +34 -0
- package/dist/plugins/table-kit.d.ts +142 -0
- package/dist/plugins/task-checkbox-kit.d.ts +18 -0
- package/dist/scrapbox.d.ts +42 -0
- package/dist/scrapbox.js +1903 -0
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +13 -0
- package/dist/themes/code-token-presentation.d.ts +6 -0
- package/dist/themes/default-presentation.d.ts +4 -0
- package/dist/themes/gfm-presentation.d.ts +15 -0
- package/dist/themes/hatena-presentation.d.ts +10 -0
- package/dist/themes/heading-presentation.d.ts +18 -0
- package/dist/themes/scrapbox-presentation.d.ts +10 -0
- package/docs/guide.md +297 -0
- package/package.json +121 -4
package/dist/testing.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EditorSelection as i, EditorState as o } from "@codemirror/state";
|
|
2
|
+
import { g as a, b as s } from "./plugin-DI0ubB-M.js";
|
|
3
|
+
function d(t, e = {}) {
|
|
4
|
+
const r = e.selection === void 0 ? void 0 : typeof e.selection == "number" ? i.cursor(e.selection) : i.single(e.selection.anchor, e.selection.head ?? e.selection.anchor), c = o.create({ doc: t, selection: r }), n = e.activeLines !== void 0 ? new Set(e.activeLines) : e.selection !== void 0 ? a(c) : /* @__PURE__ */ new Set();
|
|
5
|
+
return { state: c, activeLineNumbers: n };
|
|
6
|
+
}
|
|
7
|
+
function m(t, e) {
|
|
8
|
+
s(t.state, e);
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
d as createTestContext,
|
|
12
|
+
m as validateDecorationSpecs
|
|
13
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { MboxThemeSpec } from "../editor/plugin.js";
|
|
2
|
+
/**
|
|
3
|
+
* Builds code token colours that apply only in an opt-in presentation theme;
|
|
4
|
+
* highlighting plugins deliberately do not apply this palette themselves.
|
|
5
|
+
*/
|
|
6
|
+
export declare function codeTokenPresentationThemeSpec(tokenClassNames: readonly string[]): MboxThemeSpec;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Extension } from "@codemirror/state";
|
|
2
|
+
import type { MboxThemeSpec } from "../editor/plugin.js";
|
|
3
|
+
/**
|
|
4
|
+
* Optional default presentation for the GFM notation, mirroring
|
|
5
|
+
* {@link mboxDefaultPresentationThemeSpec}. Structural behaviour lives in each
|
|
6
|
+
* plugin; this theme only supplies opt-in visual defaults through the public
|
|
7
|
+
* `.mbox-gfm-*` classes, every value overridable with a CSS variable.
|
|
8
|
+
*
|
|
9
|
+
* The values here are mbox's own defaults for this namespace. They are a
|
|
10
|
+
* starting point, not a claim about how any other renderer looks: a host that
|
|
11
|
+
* wants a different appearance sets the CSS variables rather than forking the
|
|
12
|
+
* theme.
|
|
13
|
+
*/
|
|
14
|
+
export declare const gfmDefaultPresentationThemeSpec: MboxThemeSpec;
|
|
15
|
+
export declare function gfmDefaultPresentationTheme(): Extension;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Extension } from "@codemirror/state";
|
|
2
|
+
import type { MboxThemeSpec } from "../editor/plugin.js";
|
|
3
|
+
/**
|
|
4
|
+
* Optional default presentation for the Hatena notation, mirroring
|
|
5
|
+
* {@link mboxDefaultPresentationThemeSpec}. Structural behaviour lives in each
|
|
6
|
+
* plugin; this theme only supplies opt-in visual defaults through the public
|
|
7
|
+
* `.mbox-hatena-*` classes, every value overridable with a CSS variable.
|
|
8
|
+
*/
|
|
9
|
+
export declare const hatenaDefaultPresentationThemeSpec: MboxThemeSpec;
|
|
10
|
+
export declare function hatenaDefaultPresentationTheme(): Extension;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { MboxThemeSpec } from "../editor/plugin.js";
|
|
2
|
+
/**
|
|
3
|
+
* One heading level's public class name, paired with the level it maps to
|
|
4
|
+
* (1-6). A notation may map only a subset of levels (e.g. Hatena's h3-h5) —
|
|
5
|
+
* pass just those entries.
|
|
6
|
+
*/
|
|
7
|
+
export interface HeadingPresentationEntry {
|
|
8
|
+
level: number;
|
|
9
|
+
className: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Build an OPT-IN heading presentation theme fragment for the given levels —
|
|
13
|
+
* not a plugin theme. A notation's own `headingPlugin()` ships no font-size
|
|
14
|
+
* or font-weight of its own; a host wanting the mbox-style scale passes its
|
|
15
|
+
* heading class names here and adds the result to its own presentation
|
|
16
|
+
* theme (see `mboxDefaultPresentationThemeSpec`).
|
|
17
|
+
*/
|
|
18
|
+
export declare function headingPresentationThemeSpec(entries: HeadingPresentationEntry[]): MboxThemeSpec;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Extension } from "@codemirror/state";
|
|
2
|
+
import type { MboxThemeSpec } from "../editor/plugin.js";
|
|
3
|
+
/**
|
|
4
|
+
* Optional default presentation for Scrapbox notation, mirroring
|
|
5
|
+
* {@link mboxDefaultPresentationThemeSpec}. Structural behaviour lives in each
|
|
6
|
+
* plugin; this theme only supplies opt-in visual defaults through the public
|
|
7
|
+
* `.mbox-scrapbox-*` classes, every value overridable with a CSS variable.
|
|
8
|
+
*/
|
|
9
|
+
export declare const scrapboxDefaultPresentationThemeSpec: MboxThemeSpec;
|
|
10
|
+
export declare function scrapboxDefaultPresentationTheme(): Extension;
|
package/docs/guide.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# Guide
|
|
2
|
+
|
|
3
|
+
Everything beyond the README's quick start: integration details, options, plugin authoring, styling, and troubleshooting.
|
|
4
|
+
|
|
5
|
+
## Install notes
|
|
6
|
+
|
|
7
|
+
`@codemirror/state`, `view`, `commands`, `language`, `autocomplete`, and `search` are declared as peer dependencies. npm 7+ installs peer dependencies by default. pnpm does too when `autoInstallPeers` is enabled (the default); Yarn requires the consuming project to provide them. mbox's public types (`Extension`, `EditorState`, `EditorView`, etc.) come from these packages, and your app will almost certainly use them directly too, to pass its own extensions or read the resulting state.
|
|
8
|
+
|
|
9
|
+
The package is ESM-only (`require()` is not supported), and creating an editor touches the DOM — in an SSR framework (Next.js etc.), construct the editor in a client-only component or effect.
|
|
10
|
+
|
|
11
|
+
## Browser testing and compatibility
|
|
12
|
+
|
|
13
|
+
The layout suite runs on every change against Playwright's bundled desktop Chromium, Firefox, and WebKit on Ubuntu. mbox is expected to work in current desktop Chrome, Edge, Firefox, and Safari. Edge and Safari have engine-level coverage through Chromium and WebKit; native Edge and Safari on macOS are not run in CI. Please report compatibility issues with the browser name and version.
|
|
14
|
+
|
|
15
|
+
Mobile browsers and older browser versions are outside the supported range before 1.0. Consumers need Node.js 18 or newer to install and import the package, and a DOM to create an editor. Maintainers use Node.js 20.19 or newer for the development toolchain; CI also verifies that the built package imports on Node.js 18.
|
|
16
|
+
|
|
17
|
+
## Reacting to edits
|
|
18
|
+
|
|
19
|
+
To react to edits, pass an update listener like any CodeMirror app:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { EditorView } from "@codemirror/view";
|
|
23
|
+
import { createGfmEditor } from "@futaba-soft/mbox/gfm";
|
|
24
|
+
|
|
25
|
+
createGfmEditor({
|
|
26
|
+
parent: document.querySelector("#editor")!,
|
|
27
|
+
extensions: [
|
|
28
|
+
EditorView.updateListener.of((update) => {
|
|
29
|
+
if (update.docChanged) save(update.state.doc.toString());
|
|
30
|
+
}),
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
In React, create the view in an effect and destroy it in the cleanup:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
function Editor() {
|
|
39
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const view = createGfmEditor({ parent: ref.current!, doc: "# Hello" });
|
|
42
|
+
return () => view.destroy();
|
|
43
|
+
}, []);
|
|
44
|
+
return <div ref={ref} />;
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Choosing a notation
|
|
49
|
+
|
|
50
|
+
Each notation lives at its own sub-path with a matching `createXxxEditor` / `createXxxEditorState` pair. You never need to look up a notation string.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { createGfmEditor } from "@futaba-soft/mbox/gfm"; // GitHub Flavored Markdown
|
|
54
|
+
import { createScrapboxEditor } from "@futaba-soft/mbox/scrapbox"; // Scrapbox notation
|
|
55
|
+
import { createHatenaEditor } from "@futaba-soft/mbox/hatena"; // Hatena notation
|
|
56
|
+
import { createMboxEditor } from "@futaba-soft/mbox"; // mbox's own notation
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`createXxxEditorState` returns just the `EditorState` (for when you want to assemble the `EditorView` yourself). `createXxxEditor` additionally takes a `parent` and returns a fully assembled `EditorView`.
|
|
60
|
+
|
|
61
|
+
If you need to choose a notation at runtime rather than at import time, build your own small dispatch table from these four functions.
|
|
62
|
+
|
|
63
|
+
The GFM layer covers the practical core of the spec — headings, lists, task lists, blockquotes, code blocks, tables, emphasis, links, images, footnotes. HTML and reference links (`[text][id]`) are deliberately not rendered and stay as raw text. Entity references stay raw in body text and link labels, although their decoded values are used where the parser supplies semantics such as a link destination or image source.
|
|
64
|
+
|
|
65
|
+
### Scrapbox icons
|
|
66
|
+
|
|
67
|
+
Scrapbox icon notation supports `[name.icon]` and repeated forms
|
|
68
|
+
`[name.icon*N]` / `[name.iconxN]`, where `N` is 1 through 20. `[[name.icon]]`
|
|
69
|
+
uses the larger strong-icon presentation; repetition also works inside that
|
|
70
|
+
form. Counts outside the range, including zero and leading-zero counts, remain
|
|
71
|
+
ordinary internal links. More than 20 deliberately remains a link rather than
|
|
72
|
+
being silently truncated.
|
|
73
|
+
|
|
74
|
+
In Scrapbox notation, `.icon` is lowercase-only: `[name.ICON]` and
|
|
75
|
+
`[name.Icon]` remain ordinary internal links. The repeat separator `x` is also
|
|
76
|
+
lowercase-only, so `[name.iconX2]` is not an icon. These rules apply to the
|
|
77
|
+
Scrapbox notation, not to mbox's separate native notation.
|
|
78
|
+
|
|
79
|
+
Hosts can adjust the strong-icon multiplier with
|
|
80
|
+
`--mbox-scrapbox-icon-strong-scale`; mbox's default is `3`. This is mbox's
|
|
81
|
+
chosen default, not a measured upstream multiplier.
|
|
82
|
+
|
|
83
|
+
### Scrapbox hashtags
|
|
84
|
+
|
|
85
|
+
A run made only of `#` characters is plain text, not a hashtag: `##`, `###`,
|
|
86
|
+
and `#####` do not link. A token is a hashtag when it starts with `#` and
|
|
87
|
+
contains at least one other character, so `##a`, `#a#`, and `###a` are tags.
|
|
88
|
+
Whitespace still ends the token, therefore `# tag` is plain text.
|
|
89
|
+
|
|
90
|
+
### Scrapbox image URLs
|
|
91
|
+
|
|
92
|
+
Extension-based single-bracket image notation classifies its image token
|
|
93
|
+
lexically: it starts with `http://` or `https://` and ends in `.png`, `.jpg`,
|
|
94
|
+
`.jpeg`, `.gif`, `.svg`, or `.webp` (case-insensitive), optionally followed by
|
|
95
|
+
a non-empty query. An opening `[` is allowed inside that image token and the
|
|
96
|
+
first `]` ends the notation, so `[https://placehold.jp/150[x150.png]` is an
|
|
97
|
+
image. The short extension-only form `[https://.png]` is also classified as an
|
|
98
|
+
image; this does not assert that the URL is fetchable. These allowances do not
|
|
99
|
+
apply to a linked image's non-image URL, ordinary external links, or bare URLs.
|
|
100
|
+
|
|
101
|
+
Gyazo also has an extension-less image shorthand. A URL is image notation when
|
|
102
|
+
it is bracketed and has the form `http(s)://gyazo.com/<32桁hex>`, optionally
|
|
103
|
+
followed by `/raw`. Only the main `gyazo.com` host is supported: subdomains such
|
|
104
|
+
as `i.gyazo.com` are ordinary links. Without brackets, the same URL is also an
|
|
105
|
+
ordinary link.
|
|
106
|
+
|
|
107
|
+
Both `[画像URL ページURL]` and `[[画像URL ページURL]]` produce an ordinary
|
|
108
|
+
image whose source is the first URL and whose click destination is the second
|
|
109
|
+
URL. The double brackets do not give this two-element form the strong-image
|
|
110
|
+
presentation. A one-element `[[画像URL]]` remains a strong image, while a
|
|
111
|
+
one-element `[画像URL]` has no click destination. The unobserved link-first
|
|
112
|
+
double-bracket form `[[ページURL 画像URL]]` is not image notation.
|
|
113
|
+
|
|
114
|
+
### GFM images and how wide they get
|
|
115
|
+
|
|
116
|
+
**The GFM namespace renders images by default** (`renderMode: "preview"`), matching GitHub. That means an editor built with no options **fetches the image URLs in its document** — GitHub proxies those requests, mbox does not, so the reader's browser contacts whatever host the document names. Pass `image: { renderMode: "token" }` to get alt text and no network instead. The mbox and Scrapbox namespaces still default to `"token"`; only GFM changed, because only GFM is GitHub's notation.
|
|
117
|
+
|
|
118
|
+
mbox does **not** put a width limit on an ordinary GFM image. With no CSS of your own a 900px image occupies 900px. Sizing an image is the host's job; the library declines it so that a host which wants a different rule does not have to fight one.
|
|
119
|
+
|
|
120
|
+
**A table cell is the single exception.** mbox lays a table out as a fixed grid sized from each cell's text, and an image has no character width — so left alone, a photograph is squeezed into a column sized for its alt text and shows nothing. Two rules apply inside a cell, and only inside a cell:
|
|
121
|
+
|
|
122
|
+
- a cell holding an image claims a **minimum column width of 32 grid units**, so there is room for a picture worth looking at
|
|
123
|
+
- the picture is capped to the cell's width and to `--mbox-gfm-table-image-max-height` (256px by default), shrinking rather than cropping, so the table stays on screen and never scrolls sideways
|
|
124
|
+
|
|
125
|
+
Both are mbox's own inventions. GitHub's table is an auto-layout HTML table and does none of this.
|
|
126
|
+
|
|
127
|
+
`gfmTablePlugin({ imageMinimumColumnWidth })`, also reachable as `gfmPlugins({ table: { imageMinimumColumnWidth } })`, changes the reservation. The unit is a grid step, not a character and not a pixel; fractions are kept as given. `0` reserves nothing, which returns an image cell to being as narrow as its alt text. A value that is not a finite number at or above zero falls back to 32 **without throwing** — a plugin constructor that throws takes the whole editor down over a layout preference.
|
|
128
|
+
|
|
129
|
+
Consequences worth knowing before you turn previews on:
|
|
130
|
+
|
|
131
|
+
- **a small icon claims the same wide column as a photograph.** The reservation is made before anything is loaded, and an image tag carries no dimensions, so it cannot depend on the real size. Lower the option if your tables are mostly icons.
|
|
132
|
+
- **rows change height when images finish loading**, which moves scroll position and height estimates.
|
|
133
|
+
- **several images in one cell reserve once**, not once each.
|
|
134
|
+
- an image inside a link label reserves too. It is drawn inside the link's anchor rather than on its own, but it occupies the cell the same way. (It reserved nothing while such an image was still flattened to its alt text.)
|
|
135
|
+
|
|
136
|
+
### GFM parser code-block ranges
|
|
137
|
+
|
|
138
|
+
`GfmCodeBlock.content` is the continuous raw source interior of a code block, including any container prefixes and fence indentation. It is therefore not necessarily the range that spells the normalized mdast `value`. Consumers that need source ranges corresponding to `value` should use `contentSegments`: joining their `text` fields with newlines exactly reproduces `value`, including empty lines.
|
|
139
|
+
|
|
140
|
+
### GFM math
|
|
141
|
+
|
|
142
|
+
Pass `math: { render }` to `gfmPlugins` when a fenced `math` block or GitHub math form (`` $`x^2`$ ``, `$x^2$`, or `$$x^2$$`) should use a renderer such as KaTeX or MathJax. A single-dollar form never crosses a physical source line; `$$…$$` may span lines when its delimiters occupy a display-math line. Ambiguous dollar runs remain literal: escaped dollars, whitespace-adjacent single-dollar runs, alphanumeric-adjacent runs, and list-item runs. Emphasis around a `$…$` run is more particular than "already interpreted as emphasis" suggests, and mbox follows GitHub here: **bold and strikethrough let the math through, italic does not**, at any nesting depth. `**$K$**`, `~~$K$~~` and `****$K$****` are math; `*$K$*`, `***$K$***` and `*a **$K$** b*` are not, because an italic is somewhere above the run. Emphasis merely *next to* the run changes nothing (`*x* $K$` is math), and emphasis *inside* it still cancels it (`$a*b*c$` is not). The `$$…$$` form is deliberately not matched to GitHub in combination with emphasis -- GitHub is not self-consistent there -- so mbox keeps the math in all such cases. Without a renderer, the backtick form remains ordinary inline code and bare dollar source remains visible. The renderer's returned DOM is trusted input: create it from trusted content or sanitize it in the renderer. mbox does not apply a generic sanitizer, because it could damage valid renderer output such as MathML.
|
|
143
|
+
|
|
144
|
+
Use the `gfmPlugins()` bundle for math. It coordinates the math replacement with inline-code decoration. If you assemble public individual plugins yourself, `gfmMathPlugin({ render })` together with `gfmInlineCodePlugin()` produces overlapping decorations for recognized inline math; the rendered math still wins visually, but the combination is not supported. The exclusion helper is intentionally internal to keep the public API small.
|
|
145
|
+
|
|
146
|
+
By default mbox supplies the TeX source as a visually hidden accessible equivalent. If the renderer already provides an accessible mathematical equivalent, set `render.providesAccessibleMath = true`; this declares that capability and avoids announcing both representations. If the renderer throws, mbox keeps the editor rendering and visibly falls back to the TeX source.
|
|
147
|
+
|
|
148
|
+
In GFM tables, column widths are calculated from the normalized TeX source, not from arbitrary renderer DOM. A renderer with a tall or wide inline result can therefore increase the rendered row height or overflow its source-sized cell; mbox deliberately does not crop mathematical output. Keep table math compact, or supply host CSS appropriate to your renderer.
|
|
149
|
+
|
|
150
|
+
A pipe table inside a blockquote splits into columns normally: the `> ` prefix is not a cell, at any nesting depth. Focusing one of its lines restores the whole physical line, quote marker included.
|
|
151
|
+
|
|
152
|
+
### Scrapbox formulas
|
|
153
|
+
|
|
154
|
+
Pass `formula: { render }` to `scrapboxPlugins` when Scrapbox `[$ … ]` formulas or Cosense's block-formula `code:tex` / `code:latex` blocks should use a renderer such as KaTeX or MathJax. A block formula replaces its opener with a block widget and hides its more-indented body lines while inactive; focusing any line restores the complete raw source. Its expression joins the body lines after removing the opener's indent plus their one required extra indent, preserving deeper indentation. These two lower-case file names are formulas, not code cards: mbox shows neither a language label nor a Copy button and does not send them to the code highlighter. Without a renderer, mbox displays the formula's raw expression. The renderer's returned DOM is trusted input: create it from trusted content or sanitize it in the renderer. mbox does not apply a generic sanitizer, because it could damage valid renderer output such as MathML.
|
|
155
|
+
|
|
156
|
+
For an inline formula, mbox stays on the same line and gives the first space followed by `]` priority as its terminator. If the line has no such ` ]`, the first `]` terminates the formula.
|
|
157
|
+
|
|
158
|
+
By default mbox supplies the TeX source as a visually hidden accessible equivalent. If the renderer already provides an accessible mathematical equivalent, set `render.providesAccessibleMath = true`; this declares that capability and avoids announcing both representations. If the renderer throws, mbox keeps the editor rendering and visibly falls back to the TeX source.
|
|
159
|
+
|
|
160
|
+
### GFM table row width
|
|
161
|
+
|
|
162
|
+
A body row shorter than the header displays empty cells only while the row is inactive; no text is inserted into the source. A body row longer than the header renders only the header-count cells while inactive, matching GitHub; its remaining physical source is hidden as one editable tail and returns in full when that row becomes active. The idle ARIA grid and column-width calculation therefore include only participating cells.
|
|
163
|
+
|
|
164
|
+
A link whose label is an image — `[](link)`, ordinary CommonMark — renders as a picture inside the anchor, so clicking it follows the **link's** destination, not the image's. The picture appears only when images render as pictures at all; with `image: { renderMode: "token" }` the label stays as alt text. A destination the allowlist rejects still shows the picture, but not as a clickable anchor.
|
|
165
|
+
|
|
166
|
+
Inside a GFM table cell such a link reserves a column exactly as a bare image does, and the picture is capped to the cell the same way. One difference, deliberate and currently unfixed: **the row does not keep its height while that line is being edited.** A bare image in a cell holds its row open with a hidden measure figure. That figure comes from the image plugin, which works from the parser's list of images -- and an image inside a link label is not in it, because the link owns the whole run. The link plugin draws the picture but builds no measure figure of its own. The row shrinks when the caret arrives and grows back when it leaves.
|
|
167
|
+
|
|
168
|
+
An absolute `http(s)` link opens in a **new tab** and sends no referrer: mbox emits `target="_blank" rel="noreferrer"`. Relative, protocol-relative and `mailto` destinations get neither attribute and follow in the same tab. This deliberately differs from GitHub, which emits `rel="nofollow"` and no `target`: mbox renders an editor, and following a link in the same tab would take the reader away from unsaved work. mbox emits no `nofollow`, so a host that wants one adds it itself.
|
|
169
|
+
|
|
170
|
+
Rendered GFM links allow relative URLs and the `http`, `https`, `mailto`, and `tel` protocols by default. Other explicit protocols render as non-link text. Hosts can opt into a custom protocol with `link: { additionalAllowedProtocols: ["obsidian"] }`; entries must be ASCII scheme names without a colon. Explicit `javascript`, `vbscript`, `data`, `file`, and `blob` destinations are always blocked, including when returned by `linkUrl`, and cannot be re-enabled. Relative destinations still resolve against the embedding document's base URL when followed.
|
|
171
|
+
|
|
172
|
+
## Passing your own CodeMirror extensions
|
|
173
|
+
|
|
174
|
+
Every `createXxxEditor` / `createXxxEditorState` accepts `extensions`, which are added alongside mbox's own setup.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { EditorView } from "@codemirror/view";
|
|
178
|
+
import { createGfmEditor } from "@futaba-soft/mbox/gfm";
|
|
179
|
+
|
|
180
|
+
createGfmEditor({
|
|
181
|
+
parent: document.querySelector("#editor")!,
|
|
182
|
+
doc: "# Hello",
|
|
183
|
+
extensions: [EditorView.lineWrapping],
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Read-only display
|
|
188
|
+
|
|
189
|
+
Pass `readOnly: true` for a non-editing, always-rendered display mode: edits
|
|
190
|
+
are rejected, the editor never takes focus, and raw syntax never appears. Text
|
|
191
|
+
stays selectable and copyable. This is not a security boundary: it supplies no
|
|
192
|
+
input-size limit, CPU isolation, URL/network policy, or worker execution.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
createGfmEditor({
|
|
196
|
+
parent: document.querySelector("#viewer")!,
|
|
197
|
+
doc: "# Hello",
|
|
198
|
+
readOnly: true,
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
This is more than CodeMirror's `EditorState.readOnly` on its own, which still lets the editor take focus — and a focused line reveals its raw syntax.
|
|
203
|
+
|
|
204
|
+
## Accessibility
|
|
205
|
+
|
|
206
|
+
Every editor exposes CodeMirror's multiline `textbox` role and has the default accessible name `Text editor`. Pass `ariaLabel` when a page has more than one editor so each can be distinguished.
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
createGfmEditor({ parent: document.querySelector("#article-editor")!, ariaLabel: "Article body" });
|
|
210
|
+
createGfmEditor({ parent: document.querySelector("#notes-editor")!, ariaLabel: "Editorial notes" });
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
When a visible heading already names the editor, keep that relationship in a normal CodeMirror extension. `aria-labelledby` takes precedence over the default `aria-label`. Use `ariaLabel` or a host-provided `aria-label` only when there is no `aria-labelledby` relationship.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
createGfmEditor({
|
|
217
|
+
parent: document.querySelector("#editor")!,
|
|
218
|
+
extensions: [EditorView.contentAttributes.of({ "aria-labelledby": "article-heading" })],
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Writing your own plugin
|
|
223
|
+
|
|
224
|
+
An `MboxPlugin` is just data. It carries arbitrary CodeMirror `extensions`, a `keymap`, a `theme`, and a `decorations` function that receives the current `MboxDecorationContext` and returns decoration specs. Pass it through `extraPlugins`.
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
import type { MboxPlugin } from "@futaba-soft/mbox";
|
|
228
|
+
import { createGfmEditor } from "@futaba-soft/mbox/gfm";
|
|
229
|
+
|
|
230
|
+
const todoHighlightPlugin: MboxPlugin = {
|
|
231
|
+
theme: {
|
|
232
|
+
".todo-highlight": { backgroundColor: "#fff3b0" },
|
|
233
|
+
},
|
|
234
|
+
decorations: ({ state }) => {
|
|
235
|
+
const index = state.doc.toString().indexOf("TODO");
|
|
236
|
+
return index === -1
|
|
237
|
+
? []
|
|
238
|
+
: [{ type: "mark", from: index, to: index + 4, className: "todo-highlight" }];
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
createGfmEditor({
|
|
243
|
+
parent: document.querySelector("#editor")!,
|
|
244
|
+
doc: "TODO: write docs",
|
|
245
|
+
extraPlugins: [todoHighlightPlugin],
|
|
246
|
+
});
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
A notation is nothing more than a list of these. `extraPlugins` adds yours to an existing one; to build a notation of your own instead, pass your list to `createMboxExtension` and hand the result to `createEditorState` as an extension. The four that ship are assembled exactly this way, so a notation you write gets the same focused-line reveal, the same containment, and the same test helpers. Your `decorations` receives the `EditorState`, so its syntax is whatever you parse out of the document text — cache the parse against `state.doc`, which is a fresh object only when the text actually changes.
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { createEditorState, createEditorView, createMboxExtension } from "@futaba-soft/mbox";
|
|
253
|
+
|
|
254
|
+
createEditorView({
|
|
255
|
+
parent: document.querySelector("#editor")!,
|
|
256
|
+
state: createEditorState({
|
|
257
|
+
doc: "TODO: write docs",
|
|
258
|
+
extensions: [createMboxExtension({ plugins: [todoHighlightPlugin] })],
|
|
259
|
+
}),
|
|
260
|
+
});
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
`createContentMarkSpec` creates one `MboxMarkDecorationSpec` for a mark that spans the whole *content* of something (a list item's body, or the content of an emphasis/strong span). Such a mark needs `inclusiveStart` / `inclusiveEnd`: when its boundaries exactly match an embedded widget (a link or image), CodeMirror otherwise makes that widget a sibling of the mark, so the mark's styling does not reach the widget. The helper encapsulates those required boundary options, which TypeScript cannot require for an ordinary mark spec.
|
|
264
|
+
|
|
265
|
+
Plugin failures are contained. If a plugin's `decorations()` throws, that provider's entire output for the update is dropped and everything else keeps rendering. If lowering finds a reversed range, an out-of-bounds coordinate, or a `replace` range crossing a line break, only that spec is dropped; the provider's other specs remain. Empty mark and replace ranges are normalized instead, so they do not report an error. These failures are silent by default; pass `onDecorationError` to `createXxxEditor` to route them to your error tracking. Exceptions thrown by an error handler are swallowed, so a broken reporter cannot interrupt rendering or prevent other handlers from receiving the failure.
|
|
266
|
+
|
|
267
|
+
Give a plugin a `name` when you need reports to identify it: `onDecorationError`
|
|
268
|
+
includes `pluginName` and `pluginIndex`, and lowering errors also include the
|
|
269
|
+
failed `spec`.
|
|
270
|
+
|
|
271
|
+
When testing a plugin like this, `@futaba-soft/mbox/testing` exports `createTestContext`, which builds an `MboxDecorationContext` directly from a document string, and `validateDecorationSpecs`. The latter runs specs through the same normalization and lowering used in production: empty mark and replace ranges are normalized, while invalid positions and reversed ranges fail the test before they reach an editor.
|
|
272
|
+
|
|
273
|
+
## Public API surface
|
|
274
|
+
|
|
275
|
+
Every symbol exported from `@futaba-soft/mbox`, `@futaba-soft/mbox/gfm`, `@futaba-soft/mbox/hatena`, `@futaba-soft/mbox/scrapbox`, and `@futaba-soft/mbox/testing` is supported API. Some internal modules ship inside the published package's `dist/` folder as build artifacts, but they are not listed in `package.json`'s `exports`, importing them directly is unsupported, and they may change or be removed without notice.
|
|
276
|
+
|
|
277
|
+
## Styling
|
|
278
|
+
|
|
279
|
+
Each notation has an opt-in default presentation theme (e.g. `gfmDefaultPresentationTheme()`), which `createXxxEditor` / `createXxxEditorState` include automatically unless you pass `presentation: false`. To style things yourself, each notation exports its own `.mbox-*` class names (e.g. `headingClassNames`, `gfmLinkClassName`) for use in your own stylesheet. Passing `presentation: false` skips the default theme entirely; passing `presentation: { notation: false }` keeps the container's default sizing and spacing while dropping only the notation-specific colors and fonts. The default presentation is light-oriented — for dark UIs, override the CSS variables provided for parts of the default styling (e.g. `--mbox-gfm-link-color`) or disable the presentation and style the class names yourself.
|
|
280
|
+
|
|
281
|
+
Table layout is structural rather than part of the optional presentation theme. Each table plugin applies its calculated column width, and with no host CSS at all a table's columns line up while none of its rows is being edited. Put the caret in any row of a table and the rest of that table falls back to content-sized cells until the caret leaves: the cell boxes those widths need come from the table's ARIA structure, which is stood down while a row shows its raw source. A host that styles `.mbox-*-table-cell` with a `display` of its own — as the demo stylesheet does — keeps the columns aligned throughout. To replace every calculated cell width in an editor, set the shared `--mbox-table-cell-width` variable on the host:
|
|
282
|
+
|
|
283
|
+
```css
|
|
284
|
+
#editor { --mbox-table-cell-width: 30ch; }
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
This is the supported override and works for the mbox, GFM, Scrapbox, and Hatena table plugins. It also avoids a selector specificity contest because the variable is a different property from the plugin's `width` declaration.
|
|
288
|
+
|
|
289
|
+
This structural rule is a public behavior change for existing host CSS. A one-class rule such as `.mbox-table-cell { width: 30ch; }` has specificity (0,1,0) and no longer overrides the plugin theme's generated (0,2,0) selector. Prefer `--mbox-table-cell-width`; alternatively use a more specific selector such as `#editor .mbox-table-cell`, add `!important`, or append your own `EditorView.theme()` after the table plugin.
|
|
290
|
+
|
|
291
|
+
`codeHighlight` is opt-in: omit it or pass `false` to add no highlighter; `true` adds the zero-dependency JS/TS default. Pass an object such as `{ languages: "all" }` to configure `languages` and `onLoadError`.
|
|
292
|
+
The default presentation theme colors tokens; with `presentation: false`, highlighting still runs but the host supplies colors. Override token colors with `--mbox-code-token-*-color`, for example `--mbox-code-token-keyword-color`.
|
|
293
|
+
The default palette is tuned for a light background; every token color is overridable.
|
|
294
|
+
|
|
295
|
+
## Troubleshooting
|
|
296
|
+
|
|
297
|
+
**Editor misbehaves in impossible-looking ways** (extensions ignored, `instanceof` failures): you probably have two copies of `@codemirror/state` / `@codemirror/view` installed. Check with `npm ls @codemirror/state` and deduplicate (`npm dedupe`, or your package manager's resolutions/overrides).
|
package/package.json
CHANGED
|
@@ -1,6 +1,123 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@futaba-soft/mbox",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "CodeMirror 6 based structured text editor with focused-line raw syntax editing.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=18"
|
|
8
|
+
},
|
|
9
|
+
"packageManager": "npm@10.9.8",
|
|
10
|
+
"author": "futaba-soft",
|
|
11
|
+
"homepage": "https://github.com/futaba-soft/mbox#readme",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/futaba-soft/mbox.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/futaba-soft/mbox/issues"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./testing": {
|
|
32
|
+
"types": "./dist/testing.d.ts",
|
|
33
|
+
"import": "./dist/testing.js"
|
|
34
|
+
},
|
|
35
|
+
"./gfm": {
|
|
36
|
+
"types": "./dist/gfm.d.ts",
|
|
37
|
+
"import": "./dist/gfm.js"
|
|
38
|
+
},
|
|
39
|
+
"./hatena": {
|
|
40
|
+
"types": "./dist/hatena.d.ts",
|
|
41
|
+
"import": "./dist/hatena.js"
|
|
42
|
+
},
|
|
43
|
+
"./scrapbox": {
|
|
44
|
+
"types": "./dist/scrapbox.d.ts",
|
|
45
|
+
"import": "./dist/scrapbox.js"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"README.md",
|
|
51
|
+
"LICENSE",
|
|
52
|
+
"docs/guide.md"
|
|
53
|
+
],
|
|
54
|
+
"keywords": [
|
|
55
|
+
"codemirror",
|
|
56
|
+
"editor",
|
|
57
|
+
"markdown",
|
|
58
|
+
"structured-text",
|
|
59
|
+
"wysiwyg"
|
|
60
|
+
],
|
|
61
|
+
"license": "MIT",
|
|
62
|
+
"scripts": {
|
|
63
|
+
"dev": "vite --host 127.0.0.1",
|
|
64
|
+
"build": "vite build && tsc -p tsconfig.build.json",
|
|
65
|
+
"build:demo": "vite build --config vite.demo.config.ts",
|
|
66
|
+
"prepublishOnly": "npm run test:release",
|
|
67
|
+
"bench:parser": "esbuild scripts/bench-parser.ts --bundle --platform=node --format=esm | node --input-type=module -",
|
|
68
|
+
"bench:inline-code-highlight": "esbuild scripts/bench-inline-code-highlight.ts --bundle --platform=node --format=esm | node --input-type=module -",
|
|
69
|
+
"preview": "vite preview --host 127.0.0.1",
|
|
70
|
+
"knip": "knip",
|
|
71
|
+
"fixtures:github": "node scripts/update-github-markdown-fixture.mjs",
|
|
72
|
+
"test": "vitest run --project performance && vitest run --project unit --project dom",
|
|
73
|
+
"test:demo": "vitest run --project demo",
|
|
74
|
+
"test:layout": "playwright test",
|
|
75
|
+
"test:coverage": "vitest run --project unit --project dom --coverage",
|
|
76
|
+
"test:package": "npm run build && node scripts/package-smoke.mjs",
|
|
77
|
+
"audit:production": "npm audit --omit=dev --audit-level=low",
|
|
78
|
+
"lint:package": "publint && attw --pack . --profile esm-only",
|
|
79
|
+
"test:release": "npm run typecheck && npm run knip && npm run test:coverage && npm run test:demo && npm run test:package && npm run lint:package && npm run audit:production && npm run test:layout",
|
|
80
|
+
"test:all": "npm run test:release",
|
|
81
|
+
"typecheck": "tsc --noEmit"
|
|
82
|
+
},
|
|
83
|
+
"dependencies": {
|
|
84
|
+
"@codemirror/lang-javascript": "^6.2.5",
|
|
85
|
+
"@codemirror/lang-markdown": "^6.5.0",
|
|
86
|
+
"@codemirror/language-data": "^6.5.2",
|
|
87
|
+
"@lezer/highlight": "^1.2.3",
|
|
88
|
+
"@types/mdast": "^4.0.4",
|
|
89
|
+
"mdast-util-from-markdown": "^2.0.3",
|
|
90
|
+
"mdast-util-gfm": "^3.1.0",
|
|
91
|
+
"micromark-extension-gfm": "^3.0.0",
|
|
92
|
+
"micromark-util-decode-string": "^2.0.1"
|
|
93
|
+
},
|
|
94
|
+
"peerDependencies": {
|
|
95
|
+
"@codemirror/autocomplete": "^6.20.3",
|
|
96
|
+
"@codemirror/commands": "^6.10.4",
|
|
97
|
+
"@codemirror/language": "^6.12.4",
|
|
98
|
+
"@codemirror/search": "^6.7.1",
|
|
99
|
+
"@codemirror/state": "^6.7.0",
|
|
100
|
+
"@codemirror/view": "^6.43.4"
|
|
101
|
+
},
|
|
102
|
+
"devDependencies": {
|
|
103
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
104
|
+
"@codemirror/autocomplete": "^6.20.3",
|
|
105
|
+
"@codemirror/commands": "^6.10.4",
|
|
106
|
+
"@codemirror/language": "^6.12.4",
|
|
107
|
+
"@codemirror/search": "^6.7.1",
|
|
108
|
+
"@codemirror/state": "^6.7.0",
|
|
109
|
+
"@codemirror/view": "^6.43.4",
|
|
110
|
+
"@playwright/test": "^1.61.1",
|
|
111
|
+
"@types/node": "^20.19.43",
|
|
112
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
113
|
+
"esbuild": "^0.28.1",
|
|
114
|
+
"fast-check": "^4.9.0",
|
|
115
|
+
"happy-dom": "^20.10.6",
|
|
116
|
+
"katex": "^0.16.47",
|
|
117
|
+
"knip": "^6.27.0",
|
|
118
|
+
"publint": "0.3.21",
|
|
119
|
+
"typescript": "^5.9.3",
|
|
120
|
+
"vite": "^7.0.0",
|
|
121
|
+
"vitest": "^4.1.9"
|
|
122
|
+
}
|
|
123
|
+
}
|