@simple-mark/codemirror 0.3.4 → 0.3.7
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/README.md +6 -2
- package/THIRD_PARTY_NOTICES.txt +23 -0
- package/index.d.ts +12 -2
- package/index.js +102 -18
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -19,7 +19,11 @@ const view = createEditor(document.getElementById('editor')!, {
|
|
|
19
19
|
table. Korean, Chinese and Japanese line up (drawn exactly two columns wide), and typing
|
|
20
20
|
them with an IME is safe.
|
|
21
21
|
- **Images** and `$$` math preview below their source; `$inline$` math in a tooltip. Math
|
|
22
|
-
(`@simple-mark/math`) loads only when a document has math
|
|
22
|
+
(`@simple-mark/math`) loads only when a document has math; when bundling, alias MathJax's
|
|
23
|
+
`#default-font/*` as its README shows, so MathJax's default font isn't bundled.
|
|
24
|
+
- **Colors** in fenced code blocks, as VS Code's Markdown preview colors them
|
|
25
|
+
(`@simple-mark/highlight`, highlight.js; each language loads when a block needs it), and
|
|
26
|
+
in TeX: commands, brackets by nesting depth, an unmatched brace in red.
|
|
23
27
|
- **Links**: Ctrl+Click (⌘+Click on macOS) follows a link — a plain click keeps placing the
|
|
24
28
|
cursor, since links are editable text. `#heading` links jump within the document; other
|
|
25
29
|
schemes than http(s)/mailto are refused; web pages and paths go to `openLink` (default: a
|
|
@@ -28,4 +32,4 @@ const view = createEditor(document.getElementById('editor')!, {
|
|
|
28
32
|
- Accurate scroll heights for documents with many images and formulas.
|
|
29
33
|
- `simpleMark(theme)` gives the extensions for your own `EditorState`.
|
|
30
34
|
|
|
31
|
-
MIT
|
|
35
|
+
MIT. Values copied from other projects are credited in `THIRD_PARTY_NOTICES.txt`.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
@simple-mark/codemirror: third-party notices
|
|
2
|
+
|
|
3
|
+
@simple-mark/codemirror is MIT-licensed (see LICENSE). Its dependencies come with their own licenses;
|
|
4
|
+
it copies the values described below from these projects.
|
|
5
|
+
|
|
6
|
+
==============================================================================
|
|
7
|
+
DESIGN REFERENCES: values copied into this code
|
|
8
|
+
==============================================================================
|
|
9
|
+
|
|
10
|
+
github-markdown-css (MIT)
|
|
11
|
+
https://github.com/sindresorhus/github-markdown-css
|
|
12
|
+
Used for:
|
|
13
|
+
- codemirror: Error and loading message colors (src/previews.ts).
|
|
14
|
+
|
|
15
|
+
MIT License
|
|
16
|
+
|
|
17
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
18
|
+
|
|
19
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
20
|
+
|
|
21
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EditorState, Extension, Facet, StateCommand } from "@codemirror/state";
|
|
2
2
|
import { DecorationSet, EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view";
|
|
3
|
-
import {
|
|
3
|
+
import { ResolvedLineLook, Theme } from "@simple-mark/model";
|
|
4
4
|
//#region packages/codemirror/src/links.d.ts
|
|
5
5
|
/** What to open: a web address, or a path as written (an image, another document, …). */
|
|
6
6
|
interface LinkTarget {
|
|
@@ -40,15 +40,25 @@ export declare function previews(): Extension;
|
|
|
40
40
|
/** The theme the editor draws with. */
|
|
41
41
|
export declare const markdownTheme: Facet<Theme, Theme>;
|
|
42
42
|
/** A look as an inline CSS declaration list. */
|
|
43
|
-
export declare function lookCss(look:
|
|
43
|
+
export declare function lookCss(look: ResolvedLineLook, theme: Theme): string;
|
|
44
44
|
declare class MarkdownDecorations {
|
|
45
|
+
private view;
|
|
45
46
|
decorations: DecorationSet;
|
|
46
47
|
private theme;
|
|
47
48
|
private fixWide;
|
|
48
49
|
private cache;
|
|
50
|
+
private destroyed;
|
|
51
|
+
private highlight;
|
|
49
52
|
constructor(view: EditorView);
|
|
53
|
+
destroy(): void;
|
|
50
54
|
update(update: ViewUpdate): void;
|
|
51
55
|
private build;
|
|
56
|
+
/**
|
|
57
|
+
* One element behind each run of neighbouring spans with the same background (inline code
|
|
58
|
+
* with its backticks), so the background has rounded corners only at its ends. The spans
|
|
59
|
+
* themselves draw no background. Being longer, a box wraps the spans' elements.
|
|
60
|
+
*/
|
|
61
|
+
private backgroundBoxes;
|
|
52
62
|
/** Draws runs of wide characters exactly two columns each, so table pipes line up. */
|
|
53
63
|
private wideRuns;
|
|
54
64
|
private lineDecoration;
|
package/index.js
CHANGED
|
@@ -2,8 +2,9 @@ import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
|
|
|
2
2
|
import { commonmarkLanguage, markdown } from "@codemirror/lang-markdown";
|
|
3
3
|
import { EditorState, Facet, Prec, StateEffect, StateField } from "@codemirror/state";
|
|
4
4
|
import { Decoration, EditorView, ViewPlugin, WidgetType, hoverTooltip, keymap, showTooltip } from "@codemirror/view";
|
|
5
|
-
import { cellAt, charWidth, defaultTheme, headingAnchors, linkAt, linkKind, markdownExtensions, markdownStyles, mathSource, parseTable, previewBlocks, renderTable, resolveLineLook, resolveLook, rowLine } from "@simple-mark/model";
|
|
5
|
+
import { STROKES, cellAt, charWidth, defaultTheme, headingAnchors, linkAt, linkKind, markdownExtensions, markdownStyles, mathSource, parseTable, previewBlocks, renderTable, resolveLineLook, resolveLook, rowLine } from "@simple-mark/model";
|
|
6
6
|
import { ensureSyntaxTree, syntaxTree } from "@codemirror/language";
|
|
7
|
+
import { createHighlighter } from "@simple-mark/highlight";
|
|
7
8
|
//#region packages/codemirror/src/image-resolver.ts
|
|
8
9
|
/** The URLs a resolver gives for `src`, in order; undefined while the host is still loading. */
|
|
9
10
|
function resolvedUrls(resolve, src) {
|
|
@@ -129,12 +130,26 @@ function lookCss(look, theme) {
|
|
|
129
130
|
if (look.monospace !== void 0) css.push(`font-family: ${look.monospace ? theme.monospaceFamily : theme.fontFamily}`);
|
|
130
131
|
if (look.color !== void 0) css.push(`color: ${look.color}`);
|
|
131
132
|
if (look.background !== void 0) css.push(`background-color: ${look.background}`);
|
|
133
|
+
if (look.radius !== void 0) css.push(`border-radius: ${look.radius}em`);
|
|
132
134
|
if (look.strike !== void 0 || look.underline !== void 0) {
|
|
133
135
|
const lines = [look.strike && "line-through", look.underline && "underline"].filter(Boolean);
|
|
134
136
|
css.push(`text-decoration: ${lines.join(" ") || "none"}`);
|
|
135
137
|
}
|
|
138
|
+
const em = (size) => `${size / (look.fontScale ?? 1)}em`;
|
|
136
139
|
if (look.lineBackground !== void 0) css.push(`background-color: ${look.lineBackground}`);
|
|
137
|
-
if (look.
|
|
140
|
+
if (look.radiusTop || look.radiusBottom) {
|
|
141
|
+
const [top, bottom] = [em(look.radiusTop ?? 0), em(look.radiusBottom ?? 0)];
|
|
142
|
+
css.push(`border-radius: ${top} ${top} ${bottom} ${bottom}`);
|
|
143
|
+
}
|
|
144
|
+
const shadows = [];
|
|
145
|
+
if (look.borderLeft !== void 0) shadows.push(`inset ${em(STROKES.bar)} 0 0 ${look.borderLeft}`);
|
|
146
|
+
if (look.borderBottom !== void 0) shadows.push(`inset 0 -${em(STROKES.border)} 0 ${look.borderBottom}`);
|
|
147
|
+
if (shadows.length) css.push(`box-shadow: ${shadows.join(", ")}`);
|
|
148
|
+
if (look.rule !== void 0) {
|
|
149
|
+
const half = em(STROKES.rule / 2);
|
|
150
|
+
css.push(`background-image: linear-gradient(transparent calc(50% - ${half}), ${look.rule} 0, ${look.rule} calc(50% + ${half}), transparent 0)`);
|
|
151
|
+
}
|
|
152
|
+
if (look.indent !== void 0) css.push(`padding-left: ${em(look.indent)}`);
|
|
138
153
|
return css.join("; ");
|
|
139
154
|
}
|
|
140
155
|
const fontChecks = /* @__PURE__ */ new Map();
|
|
@@ -159,59 +174,127 @@ function cjkFitsColumns(family) {
|
|
|
159
174
|
return fits;
|
|
160
175
|
}
|
|
161
176
|
const headingText = Decoration.mark({ class: "sm-heading-text" });
|
|
177
|
+
/** A code block's language has loaded: draw its colors. */
|
|
178
|
+
const languageLoaded = StateEffect.define();
|
|
162
179
|
/** The lines holding a cursor, as a comparable string. */
|
|
163
180
|
function cursorLinesKey(state) {
|
|
164
181
|
return state.selection.ranges.map((r) => state.doc.lineAt(r.head).from).join(",");
|
|
165
182
|
}
|
|
166
183
|
var MarkdownDecorations = class {
|
|
184
|
+
view;
|
|
167
185
|
decorations;
|
|
168
186
|
theme;
|
|
169
187
|
fixWide;
|
|
170
188
|
cache = /* @__PURE__ */ new Map();
|
|
189
|
+
destroyed = false;
|
|
190
|
+
highlight = createHighlighter(() => {
|
|
191
|
+
if (!this.destroyed) this.view.dispatch({ effects: languageLoaded.of(null) });
|
|
192
|
+
});
|
|
171
193
|
constructor(view) {
|
|
194
|
+
this.view = view;
|
|
172
195
|
this.theme = view.state.facet(markdownTheme);
|
|
173
196
|
this.fixWide = !cjkFitsColumns(this.theme.monospaceFamily);
|
|
174
197
|
this.decorations = this.build(view);
|
|
175
198
|
}
|
|
199
|
+
destroy() {
|
|
200
|
+
this.destroyed = true;
|
|
201
|
+
}
|
|
176
202
|
update(update) {
|
|
177
203
|
const theme = update.state.facet(markdownTheme);
|
|
178
204
|
if (theme !== this.theme) {
|
|
179
205
|
this.theme = theme;
|
|
180
206
|
this.fixWide = !cjkFitsColumns(theme.monospaceFamily);
|
|
181
207
|
this.cache.clear();
|
|
182
|
-
} else if (!update.docChanged && !update.viewportChanged && syntaxTree(update.state) === syntaxTree(update.startState) && !(this.fixWide && update.selectionSet && cursorLinesKey(update.state) !== cursorLinesKey(update.startState))) return;
|
|
208
|
+
} else if (!update.docChanged && !update.viewportChanged && syntaxTree(update.state) === syntaxTree(update.startState) && !update.transactions.some((tr) => tr.effects.some((e) => e.is(languageLoaded))) && !(this.fixWide && update.selectionSet && cursorLinesKey(update.state) !== cursorLinesKey(update.startState))) return;
|
|
183
209
|
this.decorations = this.build(update.view);
|
|
184
210
|
}
|
|
185
211
|
build(view) {
|
|
186
212
|
const { doc } = view.state;
|
|
187
213
|
const { from, to } = view.viewport;
|
|
188
|
-
const { blocks, spans } = markdownStyles(syntaxTree(view.state), from, to
|
|
214
|
+
const { blocks, spans } = markdownStyles(syntaxTree(view.state), from, to, {
|
|
215
|
+
slice: (a, b) => doc.sliceString(a, b),
|
|
216
|
+
highlight: this.highlight
|
|
217
|
+
});
|
|
218
|
+
/** Block styles of each line, and the styles whose block starts or ends there. */
|
|
189
219
|
const lines = /* @__PURE__ */ new Map();
|
|
190
220
|
for (const block of blocks) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
221
|
+
let blockEnd = block.to;
|
|
222
|
+
if (blockEnd > block.from && doc.lineAt(blockEnd).from === blockEnd) blockEnd--;
|
|
223
|
+
const firstLine = doc.lineAt(block.from).from;
|
|
224
|
+
const lastLine = doc.lineAt(blockEnd).from;
|
|
225
|
+
for (let pos = Math.max(block.from, from), end = Math.min(blockEnd, to); pos <= end;) {
|
|
195
226
|
const line = doc.lineAt(pos);
|
|
196
|
-
let
|
|
197
|
-
if (!
|
|
198
|
-
|
|
227
|
+
let entry = lines.get(line.from);
|
|
228
|
+
if (!entry) lines.set(line.from, entry = {
|
|
229
|
+
styles: /* @__PURE__ */ new Set(),
|
|
230
|
+
first: /* @__PURE__ */ new Set(),
|
|
231
|
+
last: /* @__PURE__ */ new Set()
|
|
232
|
+
});
|
|
233
|
+
entry.styles.add(block.style);
|
|
234
|
+
if (line.from === firstLine) entry.first.add(block.style);
|
|
235
|
+
if (line.from === lastLine) entry.last.add(block.style);
|
|
199
236
|
pos = line.to + 1;
|
|
200
237
|
}
|
|
201
238
|
}
|
|
202
239
|
const ranges = [];
|
|
203
|
-
for (const [lineFrom,
|
|
240
|
+
for (const [lineFrom, entry] of lines) ranges.push(this.lineDecoration(entry).range(lineFrom));
|
|
241
|
+
for (const box of this.backgroundBoxes(spans)) ranges.push(box);
|
|
204
242
|
for (const span of spans) ranges.push(this.markDecoration(span.styles).range(span.from, span.to));
|
|
205
|
-
for (const [lineFrom, styles] of lines) {
|
|
243
|
+
for (const [lineFrom, { styles }] of lines) {
|
|
206
244
|
const line = doc.lineAt(lineFrom);
|
|
207
245
|
if (line.length && [...styles].some((s) => /^h[1-6]$/.test(s))) ranges.push(headingText.range(line.from, line.to));
|
|
208
246
|
}
|
|
209
247
|
if (this.fixWide) {
|
|
210
248
|
const cursorLines = new Set(view.state.selection.ranges.map((r) => doc.lineAt(r.head).from));
|
|
211
|
-
for (const [lineFrom, styles] of lines) if (styles.has("table") && !cursorLines.has(lineFrom)) this.wideRuns(doc.lineAt(lineFrom), ranges);
|
|
249
|
+
for (const [lineFrom, { styles }] of lines) if (styles.has("table") && !cursorLines.has(lineFrom)) this.wideRuns(doc.lineAt(lineFrom), ranges);
|
|
212
250
|
}
|
|
213
251
|
return Decoration.set(ranges, true);
|
|
214
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* One element behind each run of neighbouring spans with the same background (inline code
|
|
255
|
+
* with its backticks), so the background has rounded corners only at its ends. The spans
|
|
256
|
+
* themselves draw no background. Being longer, a box wraps the spans' elements.
|
|
257
|
+
*/
|
|
258
|
+
backgroundBoxes(spans) {
|
|
259
|
+
const boxes = [];
|
|
260
|
+
let open = null;
|
|
261
|
+
const flush = () => {
|
|
262
|
+
if (!open) return;
|
|
263
|
+
const { background, radius, monospace } = open.look;
|
|
264
|
+
const look = {
|
|
265
|
+
background,
|
|
266
|
+
...radius !== void 0 ? { radius } : {},
|
|
267
|
+
...monospace !== void 0 ? { monospace } : {}
|
|
268
|
+
};
|
|
269
|
+
const key = `box:${JSON.stringify(look)}`;
|
|
270
|
+
let deco = this.cache.get(key);
|
|
271
|
+
if (!deco) {
|
|
272
|
+
const css = `${lookCss(look, this.theme)}; line-height: 0; -webkit-box-decoration-break: clone; box-decoration-break: clone`;
|
|
273
|
+
deco = Decoration.mark({
|
|
274
|
+
class: "sm-box",
|
|
275
|
+
attributes: { style: css }
|
|
276
|
+
});
|
|
277
|
+
this.cache.set(key, deco);
|
|
278
|
+
}
|
|
279
|
+
boxes.push(deco.range(open.from, open.to));
|
|
280
|
+
open = null;
|
|
281
|
+
};
|
|
282
|
+
for (const span of spans) {
|
|
283
|
+
const look = resolveLook([], span.styles, this.theme);
|
|
284
|
+
if (look.background === void 0) flush();
|
|
285
|
+
else if (open && open.to === span.from && open.look.background === look.background) open.to = span.to;
|
|
286
|
+
else {
|
|
287
|
+
flush();
|
|
288
|
+
open = {
|
|
289
|
+
from: span.from,
|
|
290
|
+
to: span.to,
|
|
291
|
+
look
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
flush();
|
|
296
|
+
return boxes;
|
|
297
|
+
}
|
|
215
298
|
/** Draws runs of wide characters exactly two columns each, so table pipes line up. */
|
|
216
299
|
wideRuns(line, ranges) {
|
|
217
300
|
let start = -1;
|
|
@@ -241,13 +324,14 @@ var MarkdownDecorations = class {
|
|
|
241
324
|
}
|
|
242
325
|
flush(i);
|
|
243
326
|
}
|
|
244
|
-
lineDecoration(
|
|
245
|
-
const
|
|
327
|
+
lineDecoration(entry) {
|
|
328
|
+
const styles = [...entry.styles].sort();
|
|
329
|
+
const key = `line:${styles.join(" ")}|${[...entry.first].sort().join(" ")}|${[...entry.last].sort().join(" ")}`;
|
|
246
330
|
let deco = this.cache.get(key);
|
|
247
331
|
if (!deco) {
|
|
248
332
|
deco = Decoration.line({
|
|
249
333
|
class: styles.map((s) => `sm-line-${s}`).join(" "),
|
|
250
|
-
attributes: { style: lookCss(resolveLineLook(styles, this.theme), this.theme) }
|
|
334
|
+
attributes: { style: lookCss(resolveLineLook(styles, this.theme, entry), this.theme) }
|
|
251
335
|
});
|
|
252
336
|
this.cache.set(key, deco);
|
|
253
337
|
}
|
|
@@ -258,7 +342,7 @@ var MarkdownDecorations = class {
|
|
|
258
342
|
let deco = this.cache.get(key);
|
|
259
343
|
if (!deco) {
|
|
260
344
|
const linked = styles.includes("link") || styles.includes("url");
|
|
261
|
-
const look = resolveLook([], styles, this.theme);
|
|
345
|
+
const { background: _background, radius: _radius, ...look } = resolveLook([], styles, this.theme);
|
|
262
346
|
const keepLine = look.monospace !== void 0 || look.fontScale !== void 0;
|
|
263
347
|
deco = Decoration.mark({
|
|
264
348
|
class: styles.map((s) => `sm-${s}`).join(" "),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simple-mark/codemirror",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"description": "CodeMirror 6 editor that styles Markdown without hiding its syntax.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"@codemirror/state": "^6.7.6",
|
|
29
29
|
"@codemirror/view": "^6.43.15",
|
|
30
30
|
"@lezer/common": "^1.5.3",
|
|
31
|
-
"@simple-mark/
|
|
32
|
-
"@simple-mark/
|
|
31
|
+
"@simple-mark/highlight": "^0.3.7",
|
|
32
|
+
"@simple-mark/math": "^0.3.7",
|
|
33
|
+
"@simple-mark/model": "^0.3.7"
|
|
33
34
|
}
|
|
34
35
|
}
|