@sveltia/ui 0.91.0 → 0.92.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/dist/components/bottom-navigation/bottom-navigation.svelte +3 -9
- package/dist/components/bottom-navigation/bottom-navigation.svelte.d.ts +4 -39
- package/dist/components/button/button-group.svelte +2 -4
- package/dist/components/button/button-group.svelte.d.ts +3 -19
- package/dist/components/button/select-button-group.svelte +2 -13
- package/dist/components/button/select-button-group.svelte.d.ts +5 -67
- package/dist/components/calendar/calendar.d.ts +9 -0
- package/dist/components/calendar/calendar.js +43 -0
- package/dist/components/calendar/calendar.svelte +26 -27
- package/dist/components/checkbox/checkbox-group.svelte +2 -8
- package/dist/components/checkbox/checkbox-group.svelte.d.ts +4 -39
- package/dist/components/dialog/alert-dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/confirmation-dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/dialog.svelte +6 -7
- package/dist/components/dialog/dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/prompt-dialog.svelte +2 -2
- package/dist/components/dialog/prompt-dialog.svelte.d.ts +2 -3
- package/dist/components/disclosure/disclosure.svelte +14 -16
- package/dist/components/disclosure/disclosure.svelte.d.ts +4 -38
- package/dist/components/drawer/drawer.svelte +22 -39
- package/dist/components/drawer/drawer.svelte.d.ts +3 -24
- package/dist/components/grid/grid-body.svelte +2 -3
- package/dist/components/grid/grid-body.svelte.d.ts +3 -18
- package/dist/components/grid/grid-cell.svelte +2 -8
- package/dist/components/grid/grid-cell.svelte.d.ts +3 -22
- package/dist/components/grid/grid-col-header.svelte +2 -4
- package/dist/components/grid/grid-col-header.svelte.d.ts +3 -19
- package/dist/components/grid/grid-foot.svelte +4 -9
- package/dist/components/grid/grid-foot.svelte.d.ts +3 -22
- package/dist/components/grid/grid-head.svelte +4 -9
- package/dist/components/grid/grid-head.svelte.d.ts +3 -22
- package/dist/components/grid/grid-row-header.svelte +2 -8
- package/dist/components/grid/grid-row-header.svelte.d.ts +3 -22
- package/dist/components/grid/grid-row.svelte +4 -5
- package/dist/components/grid/grid-row.svelte.d.ts +3 -19
- package/dist/components/grid/grid.d.ts +10 -0
- package/dist/components/grid/grid.js +11 -0
- package/dist/components/grid/grid.svelte +6 -7
- package/dist/components/grid/grid.svelte.d.ts +5 -21
- package/dist/components/listbox/listbox.svelte +34 -15
- package/dist/components/listbox/listbox.svelte.d.ts +5 -69
- package/dist/components/listbox/option-group.svelte +2 -8
- package/dist/components/listbox/option-group.svelte.d.ts +4 -39
- package/dist/components/listbox/option.svelte +2 -1
- package/dist/components/menu/menu-item-group.svelte +2 -6
- package/dist/components/menu/menu-item-group.svelte.d.ts +3 -22
- package/dist/components/menu/menu-item.svelte +5 -1
- package/dist/components/menu/menu.svelte +2 -8
- package/dist/components/menu/menu.svelte.d.ts +4 -39
- package/dist/components/radio/radio-group.svelte +2 -17
- package/dist/components/radio/radio-group.svelte.d.ts +5 -67
- package/dist/components/radio/radio.svelte +19 -22
- package/dist/components/radio/radio.svelte.d.ts +10 -40
- package/dist/components/resizable-pane/resizable-handle.svelte +42 -49
- package/dist/components/resizable-pane/resizable-pane-group.svelte +44 -58
- package/dist/components/resizable-pane/resizable-pane-group.svelte.d.ts +3 -19
- package/dist/components/resizable-pane/resizable-pane.svelte +2 -5
- package/dist/components/resizable-pane/resizable-pane.svelte.d.ts +3 -19
- package/dist/components/resizable-pane/sizing.d.ts +24 -0
- package/dist/components/resizable-pane/sizing.js +83 -0
- package/dist/components/select/combobox.svelte +35 -39
- package/dist/components/select/select-tags.svelte +3 -13
- package/dist/components/select/select-tags.svelte.d.ts +5 -67
- package/dist/components/slider/slider.d.ts +5 -4
- package/dist/components/slider/slider.js +28 -23
- package/dist/components/slider/slider.svelte +64 -62
- package/dist/components/slider/slider.svelte.d.ts +4 -37
- package/dist/components/switch/switch.svelte +2 -13
- package/dist/components/switch/switch.svelte.d.ts +5 -67
- package/dist/components/table/table-body.svelte +2 -4
- package/dist/components/table/table-body.svelte.d.ts +3 -19
- package/dist/components/table/table-cell.svelte +2 -8
- package/dist/components/table/table-cell.svelte.d.ts +3 -22
- package/dist/components/table/table-col-header.svelte +2 -8
- package/dist/components/table/table-col-header.svelte.d.ts +3 -22
- package/dist/components/table/table-foot.svelte +2 -8
- package/dist/components/table/table-foot.svelte.d.ts +3 -22
- package/dist/components/table/table-head.svelte +2 -8
- package/dist/components/table/table-head.svelte.d.ts +3 -22
- package/dist/components/table/table-part.svelte +2 -4
- package/dist/components/table/table-part.svelte.d.ts +3 -19
- package/dist/components/table/table-row-header.svelte +2 -8
- package/dist/components/table/table-row-header.svelte.d.ts +3 -22
- package/dist/components/table/table-row.svelte +2 -8
- package/dist/components/table/table-row.svelte.d.ts +3 -22
- package/dist/components/table/table.svelte +2 -4
- package/dist/components/table/table.svelte.d.ts +3 -19
- package/dist/components/tabs/tab-box.svelte +2 -4
- package/dist/components/tabs/tab-box.svelte.d.ts +3 -19
- package/dist/components/tabs/tab-list.svelte +2 -7
- package/dist/components/tabs/tab-list.svelte.d.ts +4 -37
- package/dist/components/tabs/tab-panel.svelte +2 -8
- package/dist/components/tabs/tab-panel.svelte.d.ts +3 -22
- package/dist/components/tabs/tab-panels.svelte +2 -8
- package/dist/components/tabs/tab-panels.svelte.d.ts +3 -22
- package/dist/components/text-editor/code-editor.js +14 -7
- package/dist/components/text-editor/code-editor.svelte +14 -22
- package/dist/components/text-editor/code-editor.svelte.d.ts +5 -103
- package/dist/components/text-editor/config.d.ts +73 -0
- package/dist/components/text-editor/config.js +110 -0
- package/dist/components/text-editor/conversion/editor-components.js +4 -4
- package/dist/components/text-editor/conversion/html/export.js +1 -1
- package/dist/components/text-editor/conversion/import.js +32 -26
- package/dist/components/text-editor/conversion/markdown/block-conversion.d.ts +4 -0
- package/dist/components/text-editor/conversion/markdown/block-conversion.js +154 -0
- package/dist/components/text-editor/conversion/markdown/block-structure.d.ts +16 -0
- package/dist/components/text-editor/conversion/markdown/block-structure.js +254 -0
- package/dist/components/text-editor/conversion/markdown/code-blocks.d.ts +57 -0
- package/dist/components/text-editor/conversion/markdown/code-blocks.js +279 -0
- package/dist/components/text-editor/conversion/markdown/code-spans.d.ts +19 -0
- package/dist/components/text-editor/conversion/markdown/code-spans.js +71 -0
- package/dist/components/text-editor/conversion/markdown/formatting.d.ts +22 -0
- package/dist/components/text-editor/conversion/markdown/formatting.js +451 -0
- package/dist/components/text-editor/conversion/markdown/index.d.ts +10 -0
- package/dist/components/text-editor/conversion/markdown/index.js +55 -0
- package/dist/components/text-editor/conversion/markdown/placeholders.d.ts +2 -0
- package/dist/components/text-editor/conversion/markdown/placeholders.js +229 -0
- package/dist/components/text-editor/conversion/markdown/syntax.d.ts +99 -0
- package/dist/components/text-editor/conversion/markdown/syntax.js +206 -0
- package/dist/components/text-editor/conversion/placeholder-chars.d.ts +43 -0
- package/dist/components/text-editor/conversion/placeholder-chars.js +51 -0
- package/dist/components/text-editor/conversion/transformers/block-escape.d.ts +1 -0
- package/dist/components/text-editor/conversion/transformers/block-escape.js +53 -26
- package/dist/components/text-editor/conversion/transformers/code.d.ts +3 -14
- package/dist/components/text-editor/conversion/transformers/code.js +78 -16
- package/dist/components/text-editor/conversion/transformers/formatted-code.js +3 -18
- package/dist/components/text-editor/conversion/transformers/inline-code.js +3 -1
- package/dist/components/text-editor/conversion/transformers/link.js +3 -1
- package/dist/components/text-editor/conversion/transformers/raw-syntax.d.ts +0 -19
- package/dist/components/text-editor/conversion/transformers/raw-syntax.js +22 -93
- package/dist/components/text-editor/conversion/transformers/table.js +57 -16
- package/dist/components/text-editor/conversion/update.d.ts +19 -2
- package/dist/components/text-editor/conversion/update.js +63 -39
- package/dist/components/text-editor/core.d.ts +12 -51
- package/dist/components/text-editor/core.js +44 -66
- package/dist/components/text-editor/editing/raw-markdown.d.ts +3 -0
- package/dist/components/text-editor/editing/raw-markdown.js +46 -2
- package/dist/components/text-editor/lexical-root.svelte +49 -47
- package/dist/components/text-editor/lexical-root.svelte.d.ts +4 -67
- package/dist/components/text-editor/shiki/{facade.d.ts → engine.d.ts} +8 -8
- package/dist/components/text-editor/shiki/{facade.js → engine.js} +53 -156
- package/dist/components/text-editor/shiki/highlighter.js +5 -5
- package/dist/components/text-editor/shiki/lexical.d.ts +6 -0
- package/dist/components/text-editor/shiki/lexical.js +165 -0
- package/dist/components/text-editor/shiki/preload.js +1 -1
- package/dist/components/text-editor/shiki/source-highlight.svelte.d.ts +15 -0
- package/dist/components/text-editor/shiki/source-highlight.svelte.js +120 -0
- package/dist/components/text-editor/shiki/version.d.ts +1 -1
- package/dist/components/text-editor/shiki/version.js +1 -1
- package/dist/components/text-editor/store.svelte.js +65 -16
- package/dist/components/text-editor/text-editor.svelte +53 -145
- package/dist/components/text-editor/text-editor.svelte.d.ts +5 -103
- package/dist/components/text-editor/toolbar/actions.d.ts +25 -0
- package/dist/components/text-editor/toolbar/actions.js +107 -0
- package/dist/components/text-editor/toolbar/align-menu-button.svelte +16 -14
- package/dist/components/text-editor/toolbar/format-text-button.svelte +26 -24
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +23 -23
- package/dist/components/text-editor/toolbar/insert-item-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +47 -51
- package/dist/components/text-editor/toolbar/insert-menu-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/insert-table-button.svelte +35 -52
- package/dist/components/text-editor/toolbar/table-action-menu.svelte +19 -17
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +16 -4
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte.d.ts +0 -8
- package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +34 -33
- package/dist/components/text-editor/widgets/emoji-autocomplete.svelte +7 -6
- package/dist/components/text-editor/widgets/floating-link-editor.svelte +19 -32
- package/dist/components/text-editor/widgets/table-cell-menu-button.svelte +28 -32
- package/dist/components/text-field/date-time-input.svelte +7 -8
- package/dist/components/text-field/date-time-input.svelte.d.ts +3 -32
- package/dist/components/text-field/number-input.d.ts +0 -2
- package/dist/components/text-field/number-input.js +25 -25
- package/dist/components/text-field/number-input.svelte +18 -9
- package/dist/components/text-field/number-input.svelte.d.ts +1 -1
- package/dist/components/text-field/text-area.d.ts +2 -0
- package/dist/components/text-field/text-area.js +31 -0
- package/dist/components/text-field/text-area.svelte +12 -25
- package/dist/components/text-field/text-area.svelte.d.ts +5 -67
- package/dist/components/toast/toast.svelte +31 -8
- package/dist/components/toast/toast.svelte.d.ts +6 -6
- package/dist/components/toolbar/toolbar.svelte +2 -7
- package/dist/components/toolbar/toolbar.svelte.d.ts +4 -37
- package/dist/components/tree/tree-item.svelte +2 -6
- package/dist/components/tree/tree-item.svelte.d.ts +3 -22
- package/dist/components/tree/tree.svelte +3 -10
- package/dist/components/tree/tree.svelte.d.ts +4 -39
- package/dist/components/util/group.svelte +2 -7
- package/dist/components/util/group.svelte.d.ts +4 -37
- package/dist/components/util/modal.d.ts +9 -0
- package/dist/components/util/modal.js +30 -0
- package/dist/components/util/modal.svelte +16 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/locales/hu.yaml +176 -0
- package/dist/services/composite-widget.svelte.d.ts +76 -0
- package/dist/services/composite-widget.svelte.js +157 -0
- package/dist/services/focus.d.ts +2 -0
- package/dist/services/focus.js +68 -0
- package/dist/services/group.svelte.d.ts +17 -69
- package/dist/services/group.svelte.js +128 -391
- package/dist/services/label.d.ts +9 -0
- package/dist/services/label.js +22 -0
- package/dist/services/menu.d.ts +8 -0
- package/dist/services/menu.js +237 -0
- package/dist/services/navigation.d.ts +9 -0
- package/dist/services/navigation.js +13 -0
- package/dist/services/number.d.ts +2 -0
- package/dist/services/number.js +27 -0
- package/dist/services/pointer-drag.d.ts +14 -0
- package/dist/services/pointer-drag.js +89 -0
- package/dist/services/popup.svelte.js +123 -103
- package/dist/services/text.d.ts +3 -0
- package/dist/services/text.js +17 -0
- package/dist/services/tree.svelte.d.ts +29 -43
- package/dist/services/tree.svelte.js +58 -105
- package/dist/services/type-ahead.d.ts +10 -1
- package/dist/services/type-ahead.js +56 -34
- package/dist/typedefs.d.ts +103 -4
- package/dist/typedefs.js +60 -4
- package/package.json +1 -1
- package/dist/components/text-editor/conversion/markdown.d.ts +0 -56
- package/dist/components/text-editor/conversion/markdown.js +0 -949
- package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte +0 -28
- package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte.d.ts +0 -37
- package/dist/components/text-editor/toolbar/insert-component.d.ts +0 -3
- package/dist/components/text-editor/toolbar/insert-component.js +0 -37
|
@@ -1,949 +0,0 @@
|
|
|
1
|
-
import { INFO_BACKTICK } from './transformers/code.js';
|
|
2
|
-
import {
|
|
3
|
-
HTML_BLOCK_PLACEHOLDER,
|
|
4
|
-
HTML_TAG_SOURCE,
|
|
5
|
-
INLINE_HTML_REGEX,
|
|
6
|
-
} from './transformers/raw-syntax.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @typedef {object} CodeBlock
|
|
10
|
-
* @property {number} start Index of the line with the opening fence.
|
|
11
|
-
* @property {number} end Index of the line with the closing fence, or of the last line if the code
|
|
12
|
-
* block is not closed, in which case it runs to the end of the document.
|
|
13
|
-
* @property {string} fence Opening fence, made of three or more backticks or tildes.
|
|
14
|
-
* @property {boolean} closed Whether the code block has a closing fence.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Find the fenced code blocks in the lines of a Markdown string.
|
|
19
|
-
* @param {string[]} lines Lines of a Markdown string.
|
|
20
|
-
* @returns {CodeBlock[]} Code blocks.
|
|
21
|
-
*/
|
|
22
|
-
const findCodeBlocks = (lines) => {
|
|
23
|
-
/** @type {CodeBlock[]} */
|
|
24
|
-
const blocks = [];
|
|
25
|
-
/** @type {Omit<CodeBlock, 'end' | 'closed'> | undefined} */
|
|
26
|
-
let openBlock;
|
|
27
|
-
|
|
28
|
-
lines.forEach((line, index) => {
|
|
29
|
-
// Like Lexical’s `CODE_START_REGEX`, allow an indented fence. A backtick fence can’t be
|
|
30
|
-
// followed by a backtick on the same line; see `CODE`
|
|
31
|
-
const fence = line.match(/^[ \t]*(`{3,}(?=[^`]*$)|~{3,})/)?.[1];
|
|
32
|
-
|
|
33
|
-
if (!openBlock) {
|
|
34
|
-
if (fence) {
|
|
35
|
-
openBlock = { start: index, fence };
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Like Lexical’s `CODE_END_REGEX`, a code block is only closed by a bare fence of the same
|
|
42
|
-
// character that is at least as long as the opening one, so a shorter fence within the block,
|
|
43
|
-
// like ```` ``` ```` within ```` ```` ````, is part of the code
|
|
44
|
-
if (
|
|
45
|
-
fence?.[0] === openBlock.fence[0] &&
|
|
46
|
-
fence.length >= openBlock.fence.length &&
|
|
47
|
-
!line.trim().slice(fence.length)
|
|
48
|
-
) {
|
|
49
|
-
blocks.push({ ...openBlock, end: index, closed: true });
|
|
50
|
-
openBlock = undefined;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
if (openBlock) {
|
|
55
|
-
blocks.push({ ...openBlock, end: lines.length - 1, closed: false });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return blocks;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Apply a function to the parts of a Markdown string outside fenced code blocks, leaving the code
|
|
63
|
-
* blocks, fences included, untouched. Each part is given with the line breaks around it, if any, so
|
|
64
|
-
* a regular expression can tell the start and the end of a line from those of the part.
|
|
65
|
-
* @param {string} value Markdown string to process.
|
|
66
|
-
* @param {(part: string) => string} callback Function that returns a processed part. It must keep
|
|
67
|
-
* the line breaks at the start and the end of the part.
|
|
68
|
-
* @returns {string} Processed Markdown string.
|
|
69
|
-
*/
|
|
70
|
-
const mapOutsideCodeBlocks = (value, callback) => {
|
|
71
|
-
const lines = value.split('\n');
|
|
72
|
-
/** @type {string[]} */
|
|
73
|
-
const output = [];
|
|
74
|
-
let index = 0;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Process the lines from the current index to the given one, exclusive.
|
|
78
|
-
* @param {number} end Index of the line to stop at.
|
|
79
|
-
*/
|
|
80
|
-
const flush = (end) => {
|
|
81
|
-
if (end > index) {
|
|
82
|
-
const before = index > 0 ? '\n' : '';
|
|
83
|
-
const after = end < lines.length ? '\n' : '';
|
|
84
|
-
const part = callback(`${before}${lines.slice(index, end).join('\n')}${after}`);
|
|
85
|
-
|
|
86
|
-
output.push(part.slice(before.length, part.length - after.length));
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
findCodeBlocks(lines).forEach(({ start, end }) => {
|
|
91
|
-
flush(start);
|
|
92
|
-
output.push(lines.slice(start, end + 1).join('\n'));
|
|
93
|
-
index = end + 1;
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
flush(lines.length);
|
|
97
|
-
|
|
98
|
-
return output.join('\n');
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Apply a function to each line of a Markdown string, leaving the lines inside fenced code blocks
|
|
103
|
-
* untouched.
|
|
104
|
-
* @param {string} value Markdown string to process.
|
|
105
|
-
* @param {(line: string) => string} callback Function that returns a processed line.
|
|
106
|
-
* @returns {string} Processed Markdown string.
|
|
107
|
-
*/
|
|
108
|
-
const mapLinesOutsideCodeBlocks = (value, callback) => {
|
|
109
|
-
const lines = value.split('\n');
|
|
110
|
-
|
|
111
|
-
const codeLines = new Set(
|
|
112
|
-
findCodeBlocks(lines).flatMap(({ start, end }) =>
|
|
113
|
-
Array.from({ length: end - start + 1 }, (_, offset) => start + offset),
|
|
114
|
-
),
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
return lines.map((line, index) => (codeLines.has(index) ? line : callback(line))).join('\n');
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Regular expression to match a setext heading underline, like `===` or `---`.
|
|
122
|
-
*/
|
|
123
|
-
const SETEXT_UNDERLINE_REGEX = /^ {0,3}(=+|-+)[ \t]*$/;
|
|
124
|
-
/**
|
|
125
|
-
* Regular expression to match an ATX heading, like `## Heading`.
|
|
126
|
-
*/
|
|
127
|
-
const ATX_HEADING_REGEX = /^ {0,3}#{1,6}(?:[ \t]|$)/;
|
|
128
|
-
/**
|
|
129
|
-
* Regular expression to match a thematic break, like `***` or `- - -`.
|
|
130
|
-
*/
|
|
131
|
-
const THEMATIC_BREAK_REGEX = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Names of the HTML elements that start an HTML block that can interrupt a paragraph, as opposed to
|
|
135
|
-
* inline HTML, like `<span>`, which a paragraph can start with.
|
|
136
|
-
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
137
|
-
*/
|
|
138
|
-
const HTML_BLOCK_TAG_NAMES = [
|
|
139
|
-
'address',
|
|
140
|
-
'article',
|
|
141
|
-
'aside',
|
|
142
|
-
'base',
|
|
143
|
-
'basefont',
|
|
144
|
-
'blockquote',
|
|
145
|
-
'body',
|
|
146
|
-
'caption',
|
|
147
|
-
'center',
|
|
148
|
-
'col',
|
|
149
|
-
'colgroup',
|
|
150
|
-
'dd',
|
|
151
|
-
'details',
|
|
152
|
-
'dialog',
|
|
153
|
-
'dir',
|
|
154
|
-
'div',
|
|
155
|
-
'dl',
|
|
156
|
-
'dt',
|
|
157
|
-
'fieldset',
|
|
158
|
-
'figcaption',
|
|
159
|
-
'figure',
|
|
160
|
-
'footer',
|
|
161
|
-
'form',
|
|
162
|
-
'frame',
|
|
163
|
-
'frameset',
|
|
164
|
-
'h[1-6]',
|
|
165
|
-
'head',
|
|
166
|
-
'header',
|
|
167
|
-
'hr',
|
|
168
|
-
'html',
|
|
169
|
-
'iframe',
|
|
170
|
-
'legend',
|
|
171
|
-
'li',
|
|
172
|
-
'link',
|
|
173
|
-
'main',
|
|
174
|
-
'menu',
|
|
175
|
-
'menuitem',
|
|
176
|
-
'nav',
|
|
177
|
-
'noframes',
|
|
178
|
-
'ol',
|
|
179
|
-
'optgroup',
|
|
180
|
-
'option',
|
|
181
|
-
'p',
|
|
182
|
-
'param',
|
|
183
|
-
'search',
|
|
184
|
-
'section',
|
|
185
|
-
'summary',
|
|
186
|
-
'table',
|
|
187
|
-
'tbody',
|
|
188
|
-
'td',
|
|
189
|
-
'tfoot',
|
|
190
|
-
'th',
|
|
191
|
-
'thead',
|
|
192
|
-
'title',
|
|
193
|
-
'tr',
|
|
194
|
-
'track',
|
|
195
|
-
'ul',
|
|
196
|
-
];
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Start and end conditions of the HTML blocks that can interrupt a paragraph: a raw text element, a
|
|
200
|
-
* comment, a processing instruction, a declaration, a CDATA section or a block element. The start
|
|
201
|
-
* is what follows the `<` at the start of the line. The end of the block is the line that matches
|
|
202
|
-
* the end condition, or the line before a blank line if there’s none.
|
|
203
|
-
* @type {[string, RegExp | null][]}
|
|
204
|
-
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
205
|
-
*/
|
|
206
|
-
const HTML_BLOCK_CONDITION_SOURCES = [
|
|
207
|
-
['(?:script|pre|style|textarea)(?:[ \\t>]|$)', /<\/(?:pre|script|style|textarea)>/i],
|
|
208
|
-
['!--', /-->/],
|
|
209
|
-
['\\?', /\?>/],
|
|
210
|
-
['![A-Za-z]', />/],
|
|
211
|
-
['!\\[CDATA\\[', /\]\]>/],
|
|
212
|
-
[`/?(?:${HTML_BLOCK_TAG_NAMES.join('|')})(?:[ \\t>]|/>|$)`, null],
|
|
213
|
-
];
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Regular expression to match the start of an HTML block that can interrupt a paragraph; see
|
|
217
|
-
* {@link HTML_BLOCK_CONDITION_SOURCES}.
|
|
218
|
-
*/
|
|
219
|
-
const HTML_BLOCK_START_REGEX = new RegExp(
|
|
220
|
-
`<(?:${HTML_BLOCK_CONDITION_SOURCES.map(([start]) => start).join('|')})`,
|
|
221
|
-
'i',
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Start and end conditions of the HTML blocks that can interrupt a paragraph, with the start as a
|
|
226
|
-
* regular expression to match a line; see {@link HTML_BLOCK_CONDITION_SOURCES}.
|
|
227
|
-
* @type {[RegExp, RegExp | null][]}
|
|
228
|
-
*/
|
|
229
|
-
const HTML_BLOCK_CONDITIONS = HTML_BLOCK_CONDITION_SOURCES.map(([start, end]) => [
|
|
230
|
-
new RegExp(`^ {0,3}<(?:${start})`, 'i'),
|
|
231
|
-
end,
|
|
232
|
-
]);
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Global version of `INLINE_HTML_REGEX`.
|
|
236
|
-
*/
|
|
237
|
-
const GLOBAL_INLINE_HTML_REGEX = new RegExp(INLINE_HTML_REGEX, 'g');
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Regular expression to match a line that starts an HTML block that can’t interrupt a paragraph.
|
|
241
|
-
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
242
|
-
*/
|
|
243
|
-
const STANDALONE_HTML_TAG_REGEX = new RegExp(
|
|
244
|
-
`^ {0,3}(?!</?(?:pre|script|style|textarea)\\b)(?:${HTML_TAG_SOURCE})[ \\t]*$`,
|
|
245
|
-
'i',
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Regular expression to match a line that doesn’t continue a paragraph: an ATX heading, a
|
|
250
|
-
* blockquote, a list item, a code fence, a table row, an HTML block, an indented code block, a
|
|
251
|
-
* thematic break or a setext heading underline.
|
|
252
|
-
*/
|
|
253
|
-
const NON_PARAGRAPH_LINE_REGEX = new RegExp(
|
|
254
|
-
[
|
|
255
|
-
ATX_HEADING_REGEX.source,
|
|
256
|
-
/^ {0,3}(?:>|[-+*](?:[ \t]|$)|\d{1,9}[.)](?:[ \t]|$)|`{3,}|~{3,}|\|)/.source,
|
|
257
|
-
`^ {0,3}${HTML_BLOCK_START_REGEX.source}`,
|
|
258
|
-
/^(?: {4}|\t)/.source,
|
|
259
|
-
THEMATIC_BREAK_REGEX.source,
|
|
260
|
-
SETEXT_UNDERLINE_REGEX.source,
|
|
261
|
-
].join('|'),
|
|
262
|
-
'i',
|
|
263
|
-
);
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Regular expression to match a line that is a block on its own, so the line after it isn’t in a
|
|
267
|
-
* paragraph: an ATX heading or a thematic break. A setext heading underline is one as well, but
|
|
268
|
-
* only after a paragraph line.
|
|
269
|
-
*/
|
|
270
|
-
const SINGLE_LINE_BLOCK_REGEX = new RegExp(
|
|
271
|
-
[ATX_HEADING_REGEX.source, THEMATIC_BREAK_REGEX.source].join('|'),
|
|
272
|
-
);
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Regular expression to match a line that ends the paragraph before it: a line that doesn’t
|
|
276
|
-
* continue a paragraph, other than an indented line, which does.
|
|
277
|
-
*/
|
|
278
|
-
const PARAGRAPH_INTERRUPTION_REGEX = new RegExp(
|
|
279
|
-
NON_PARAGRAPH_LINE_REGEX.source.replace(`|${/^(?: {4}|\t)/.source}`, ''),
|
|
280
|
-
'i',
|
|
281
|
-
);
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Create a regular expression to match formatting that spans one or more line breaks, like
|
|
285
|
-
* `**foo\nbar**`. It captures the text between the markers, line breaks included. The text of each
|
|
286
|
-
* line is read as CommonMark does: a backslash escapes the next character, so an escaped marker is
|
|
287
|
-
* part of the text, and the text can end with an escaped backslash, like `_foo\\`, which is
|
|
288
|
-
* followed by a soft line break, not a hard one. A line can end with whitespace or the backslash of
|
|
289
|
-
* a hard line break, and start with whitespace, outside its text. The markers can’t be escaped, or
|
|
290
|
-
* next to whitespace within the formatting. The formatting must be at the start or the end of the
|
|
291
|
-
* value, or next to whitespace or punctuation other than its own marker, so a marker within a word,
|
|
292
|
-
* like `_` in `snake_case`, is left alone. As per the CommonMark flanking rules, `*` can open and
|
|
293
|
-
* close formatting within a word as well, like `**foo\nbar**baz`, unless the text starts or ends
|
|
294
|
-
* with punctuation there. The surrounding characters are checked with lookarounds rather than
|
|
295
|
-
* matched with `\s+`, which would take quadratic time on a long run of whitespace not followed by a
|
|
296
|
-
* marker. The text of a line can’t contain the marker, and it can’t start or end with whitespace,
|
|
297
|
-
* so the text and the line breaks can only be matched one way, which keeps the time linear.
|
|
298
|
-
* @param {string} char Marker character.
|
|
299
|
-
* @param {number} length Number of marker characters.
|
|
300
|
-
* @returns {RegExp} Regular expression.
|
|
301
|
-
* @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
|
|
302
|
-
*/
|
|
303
|
-
const createMultilineFormattingRegex = (char, length) => {
|
|
304
|
-
const marker = `[${char}]`.repeat(length);
|
|
305
|
-
// A character other than the marker and a line break, or an escape. The first and the last of the
|
|
306
|
-
// text can’t be whitespace
|
|
307
|
-
const unit = String.raw`(?:\\[^\s]|[^${char}\\\n])`;
|
|
308
|
-
const edgeUnit = String.raw`(?:\\[^\s]|[^${char}\s\\])`;
|
|
309
|
-
const text = `${edgeUnit}(?:${unit}*?${edgeUnit})?`;
|
|
310
|
-
const boundary = String.raw`[\s\p{P}\p{S}]`;
|
|
311
|
-
const isWithinWordAllowed = char === '*';
|
|
312
|
-
|
|
313
|
-
const opening = isWithinWordAllowed
|
|
314
|
-
? `(?:(?<=^|${boundary})${marker}|${marker}(?!${boundary}))`
|
|
315
|
-
: `(?<=^|${boundary})${marker}`;
|
|
316
|
-
|
|
317
|
-
const closing = isWithinWordAllowed
|
|
318
|
-
? `(?:(?<!${boundary})${marker}|${marker}(?=${boundary}|$))`
|
|
319
|
-
: `${marker}(?=${boundary}|$)`;
|
|
320
|
-
|
|
321
|
-
const lineBreak = String.raw`(?:\\|[ \t]*)\n[ \t]*`;
|
|
322
|
-
|
|
323
|
-
return new RegExp(
|
|
324
|
-
String.raw`(?<![${char}\\])${opening}(${text}(?:${lineBreak}${text})+)${closing}(?![${char}])`,
|
|
325
|
-
'gu',
|
|
326
|
-
);
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Regular expressions to match formatting that spans line breaks, with the marker of each.
|
|
331
|
-
* @type {[string, RegExp][]}
|
|
332
|
-
*/
|
|
333
|
-
const MULTILINE_FORMATTING_REGEXES = [
|
|
334
|
-
['***', createMultilineFormattingRegex('*', 3)],
|
|
335
|
-
['___', createMultilineFormattingRegex('_', 3)],
|
|
336
|
-
['**', createMultilineFormattingRegex('*', 2)],
|
|
337
|
-
['__', createMultilineFormattingRegex('_', 2)],
|
|
338
|
-
['~~', createMultilineFormattingRegex('~', 2)],
|
|
339
|
-
['*', createMultilineFormattingRegex('*', 1)],
|
|
340
|
-
['_', createMultilineFormattingRegex('_', 1)],
|
|
341
|
-
];
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* @typedef {object} FormattedLine
|
|
345
|
-
* @property {string} indent Whitespace at the start of the line, outside the formatting.
|
|
346
|
-
* @property {string} text Text of the line within the formatting.
|
|
347
|
-
* @property {string} lineEnd Whitespace or the backslash of a hard line break at the end of the
|
|
348
|
-
* line, outside the formatting.
|
|
349
|
-
*/
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Split the text of formatting that spans line breaks into lines. The text of a line doesn’t start
|
|
353
|
-
* or end with whitespace, and the backslash of a hard line break is the last of an odd number of
|
|
354
|
-
* backslashes at the end of a line, as the others escape each other.
|
|
355
|
-
* @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
|
|
356
|
-
* @returns {FormattedLine[]} Lines.
|
|
357
|
-
*/
|
|
358
|
-
const splitFormattedLines = (text) =>
|
|
359
|
-
text.split('\n').map((line) => {
|
|
360
|
-
const start = line.length - line.trimStart().length;
|
|
361
|
-
// Not a regular expression like `[ \t]*$`, which would take quadratic time on a long run of
|
|
362
|
-
// whitespace within the line
|
|
363
|
-
let end = line.length;
|
|
364
|
-
let backslashes = 0;
|
|
365
|
-
|
|
366
|
-
while (end > start && /[ \t]/.test(line[end - 1])) {
|
|
367
|
-
end -= 1;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
while (line[end - backslashes - 1] === '\\') {
|
|
371
|
-
backslashes += 1;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
if (backslashes % 2) {
|
|
375
|
-
end -= 1;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
return { indent: line.slice(0, start), text: line.slice(start, end), lineEnd: line.slice(end) };
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* Check if the text of formatting that spans line breaks has other formatting that spans line
|
|
383
|
-
* breaks, like `_foo\nbar_` in `**_foo\nbar_**` or `**_foo\nbar_\nbaz**`, which has to be split
|
|
384
|
-
* first.
|
|
385
|
-
* @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
|
|
386
|
-
* @returns {boolean} Result.
|
|
387
|
-
*/
|
|
388
|
-
const hasMultilineFormatting = (text) =>
|
|
389
|
-
MULTILINE_FORMATTING_REGEXES.some(([, regex]) => text.search(regex) !== -1);
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* @typedef {object} CodeSpan
|
|
393
|
-
* @property {number} start Index of the opening backticks.
|
|
394
|
-
* @property {number} end Index after the closing backticks.
|
|
395
|
-
* @property {number} fenceLength Number of backticks on either side.
|
|
396
|
-
*/
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* Find the code spans in a line like Lexical does: a run of backticks that isn’t escaped opens a
|
|
400
|
-
* code span, which is closed by the next run of as many backticks.
|
|
401
|
-
* @param {string} line Line.
|
|
402
|
-
* @returns {CodeSpan[]} Code spans.
|
|
403
|
-
* @see https://spec.commonmark.org/0.31.2/#code-spans
|
|
404
|
-
*/
|
|
405
|
-
export const findCodeSpans = (line) => {
|
|
406
|
-
const runs = [...line.matchAll(/`+/g)].map(({ 0: { length }, index }) => {
|
|
407
|
-
let backslashes = 0;
|
|
408
|
-
|
|
409
|
-
while (line[index - backslashes - 1] === '\\') {
|
|
410
|
-
backslashes += 1;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
return { index, length, escaped: backslashes % 2 === 1 };
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
/** @type {CodeSpan[]} */
|
|
417
|
-
const spans = [];
|
|
418
|
-
|
|
419
|
-
for (let open = 0; open < runs.length; open += 1) {
|
|
420
|
-
let close = open + 1;
|
|
421
|
-
|
|
422
|
-
while (close < runs.length && runs[close].length !== runs[open].length) {
|
|
423
|
-
close += 1;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
if (!runs[open].escaped && close < runs.length) {
|
|
427
|
-
const { index, length } = runs[close];
|
|
428
|
-
|
|
429
|
-
spans.push({ start: runs[open].index, end: index + length, fenceLength: length });
|
|
430
|
-
open = close;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
return spans;
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* Regular expression to match a link whose text spans a line break, like `[foo\nbar](url)`, but not
|
|
439
|
-
* an image. The text before the line break can’t end with the backslash of a hard line break.
|
|
440
|
-
*/
|
|
441
|
-
const MULTILINE_LINK_REGEX = new RegExp(
|
|
442
|
-
String.raw`(?<!(?:^|[^\\])(?:\\\\)*[\\!])\[([^[\]\n]*[^[\]\s\\])([ \t]*\n[ \t]*)([^[\]\n]+)\]` +
|
|
443
|
-
String.raw`\(([^()\s]*(?:[ \t]+"[^"\n]*")?)\)`,
|
|
444
|
-
'g',
|
|
445
|
-
);
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* Split a code span whose code spans a line break into one per line, like `` `foo\nbar` `` into
|
|
449
|
-
* `` `foo`\n`bar` ``, so the formatting around it can be split into separate lines as well.
|
|
450
|
-
* Backslashes and spaces are literal in a code span.
|
|
451
|
-
* @param {string} part Part of a Markdown string outside fenced code blocks.
|
|
452
|
-
* @returns {string} Processed part.
|
|
453
|
-
*/
|
|
454
|
-
const splitMultilineCodeSpans = (part) =>
|
|
455
|
-
part.replace(
|
|
456
|
-
/(?<=^|[\s\p{P}\p{S}])(?<![`\\])`([^`\n]+?)\n([^`\n]+?)`(?=[\s\p{P}\p{S}]|$)(?!`)/gu,
|
|
457
|
-
'`$1`\n`$2`',
|
|
458
|
-
);
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* Split a link whose text spans a line break in the text of formatting into one per line, like
|
|
462
|
-
* `[foo\nbar](url)` into `[foo](url)\n[bar](url)`, so the formatting can be split into separate
|
|
463
|
-
* lines. Only the formatting needs it, so links elsewhere, including those in raw HTML, are left as
|
|
464
|
-
* they are. An image is left as is, as it would be duplicated, and so is anything in a code span.
|
|
465
|
-
* @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
|
|
466
|
-
* @returns {string} Processed text.
|
|
467
|
-
*/
|
|
468
|
-
const splitMultilineLinks = (text) => {
|
|
469
|
-
if (!text.includes('[')) {
|
|
470
|
-
return text;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
const codeSpans = findCodeSpans(text);
|
|
474
|
-
|
|
475
|
-
return text.replace(MULTILINE_LINK_REGEX, (match, first, lineBreak, second, target, offset) =>
|
|
476
|
-
codeSpans.some(({ start, end }) => offset >= start && offset < end)
|
|
477
|
-
? match
|
|
478
|
-
: `[${first}](${target})${lineBreak}[${second}](${target})`,
|
|
479
|
-
);
|
|
480
|
-
};
|
|
481
|
-
|
|
482
|
-
/**
|
|
483
|
-
* Check if the text of formatting that spans line breaks has a line break within a code span or a
|
|
484
|
-
* link text, which {@link splitMultilineCodeSpans} and {@link splitMultilineLinks} couldn’t split.
|
|
485
|
-
* @param {string} text Text matched by one of {@link MULTILINE_FORMATTING_REGEXES}.
|
|
486
|
-
* @returns {boolean} Result.
|
|
487
|
-
*/
|
|
488
|
-
const hasLineBreakInSpan = (text) =>
|
|
489
|
-
findCodeSpans(text).some(({ start, end }) => text.slice(start, end).includes('\n')) ||
|
|
490
|
-
// An inline link or a full reference link. The first run can’t have a line break, so a `[`
|
|
491
|
-
// followed by many lines isn’t matched in quadratic time. A bracket after an escaped backslash,
|
|
492
|
-
// like `\\[`, isn’t escaped
|
|
493
|
-
/(?<!(?:^|[^\\])(?:\\\\)*\\)\[[^[\]\n]*\n[^[\]]*\](?=[([])/.test(text);
|
|
494
|
-
|
|
495
|
-
/**
|
|
496
|
-
* Split multiline formatting into separate lines to prevent Markdown parsing issues. Fenced code
|
|
497
|
-
* blocks are left untouched, as the formatting syntax is not parsed there. Formatting is left as is
|
|
498
|
-
* if a line after the first starts a block, like a heading or a list item, which ends the paragraph
|
|
499
|
-
* and therefore the formatting.
|
|
500
|
-
* @param {string} value Markdown string to process.
|
|
501
|
-
* @returns {string} Processed Markdown string.
|
|
502
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/548
|
|
503
|
-
*/
|
|
504
|
-
export const splitMultilineFormatting = (value) =>
|
|
505
|
-
mapOutsideCodeBlocks(value, (part) => {
|
|
506
|
-
let result = splitMultilineCodeSpans(part);
|
|
507
|
-
let previous;
|
|
508
|
-
|
|
509
|
-
// Split the inner formatting first, so `**_foo\nbar_**` becomes `**_foo_**\n**_bar_**`, not
|
|
510
|
-
// `**_foo**_\n_**bar_**`. The outer one is split on the next pass. A text can’t have its own
|
|
511
|
-
// marker, so formatting is nested a few levels deep at most
|
|
512
|
-
while (result !== previous) {
|
|
513
|
-
previous = result;
|
|
514
|
-
result = MULTILINE_FORMATTING_REGEXES.reduce(
|
|
515
|
-
(current, [marker, regex]) =>
|
|
516
|
-
current.replace(regex, (match, formattedText) => {
|
|
517
|
-
if (hasMultilineFormatting(formattedText)) {
|
|
518
|
-
return match;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
const text = splitMultilineLinks(formattedText);
|
|
522
|
-
|
|
523
|
-
// A line break left within a code span or a link text can’t be split there, as the
|
|
524
|
-
// markers would become part of the code or the link
|
|
525
|
-
if (hasLineBreakInSpan(text)) {
|
|
526
|
-
return match;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
const lines = splitFormattedLines(text);
|
|
530
|
-
|
|
531
|
-
// A line with only markers, like `**` in `_**\n**_` of `**_(a)_**\n**_(b)_**`, means
|
|
532
|
-
// the match runs from the closing marker of one line to the opening marker of the next,
|
|
533
|
-
// which isn’t formatting. Splitting it would add markers that match again, without end
|
|
534
|
-
if (
|
|
535
|
-
lines.some(({ text: lineText }) => /^[*_~]+$/.test(lineText)) ||
|
|
536
|
-
lines
|
|
537
|
-
.slice(1)
|
|
538
|
-
.some(({ indent, text: lineText }) =>
|
|
539
|
-
PARAGRAPH_INTERRUPTION_REGEX.test(`${indent}${lineText}`),
|
|
540
|
-
)
|
|
541
|
-
) {
|
|
542
|
-
return match;
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
return lines
|
|
546
|
-
.map(
|
|
547
|
-
({ indent, text: lineText, lineEnd }) =>
|
|
548
|
-
`${indent}${marker}${lineText}${marker}${lineEnd}`,
|
|
549
|
-
)
|
|
550
|
-
.join('\n');
|
|
551
|
-
}),
|
|
552
|
-
result,
|
|
553
|
-
);
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
return result;
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
/**
|
|
560
|
-
* Placeholder put after an escaped backslash at the end of a line; see `addImportPlaceholders()`.
|
|
561
|
-
*/
|
|
562
|
-
const ESCAPED_BACKSLASH_PLACEHOLDER = '\uE002';
|
|
563
|
-
/**
|
|
564
|
-
* Placeholder put with a period between a backslash and a formatting marker; see
|
|
565
|
-
* `addImportPlaceholders()`.
|
|
566
|
-
*/
|
|
567
|
-
const PUNCTUATION_PLACEHOLDER = '\uE003';
|
|
568
|
-
/**
|
|
569
|
-
* Placeholder put in place of a tab in a code span; see `addImportPlaceholders()`.
|
|
570
|
-
*/
|
|
571
|
-
const CODE_TAB_PLACEHOLDER = '\uE005';
|
|
572
|
-
/**
|
|
573
|
-
* Placeholder put in place of a space in a code span made only of spaces; see
|
|
574
|
-
* `addImportPlaceholders()`.
|
|
575
|
-
*/
|
|
576
|
-
const CODE_SPACE_PLACEHOLDER = '\uE006';
|
|
577
|
-
|
|
578
|
-
/**
|
|
579
|
-
* Add placeholders around the backslashes at the end of a line, so Lexical reads them as CommonMark
|
|
580
|
-
* does. Lexical looks for a hard line break, a backslash at the end of a line, after it has removed
|
|
581
|
-
* the escapes from the text, so an escaped backslash, like `foo\\`, `` `foo\` `` in a code span or
|
|
582
|
-
* `_foo\\_`, becomes a hard line break, losing the backslash, and `foo\\\` loses its hard line
|
|
583
|
-
* break. A placeholder after the escaped backslashes, or before the closing backticks of a code
|
|
584
|
-
* span, keeps the backslash from ending the text.
|
|
585
|
-
* @param {string} line Line.
|
|
586
|
-
* @returns {string} Processed line.
|
|
587
|
-
* @see https://spec.commonmark.org/0.31.2/#hard-line-breaks
|
|
588
|
-
*/
|
|
589
|
-
const markLineEndBackslashes = (line) => {
|
|
590
|
-
if (!/[\\`][*_~]*$/.test(line)) {
|
|
591
|
-
return line;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
const codeSpan = findCodeSpans(line).findLast(({ end }) => /^[*_~]*$/.test(line.slice(end)));
|
|
595
|
-
|
|
596
|
-
// Backslashes are literal in a code span
|
|
597
|
-
if (codeSpan) {
|
|
598
|
-
const index = codeSpan.end - codeSpan.fenceLength;
|
|
599
|
-
|
|
600
|
-
return line[index - 1] === '\\'
|
|
601
|
-
? `${line.slice(0, index)}${ESCAPED_BACKSLASH_PLACEHOLDER}${line.slice(index)}`
|
|
602
|
-
: line;
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
// The escaped backslashes are followed by formatting markers, if any, and the backslash of a hard
|
|
606
|
-
// line break, if any. They’re counted from the end rather than matched with a regular expression,
|
|
607
|
-
// which would take quadratic time on a long run of backslashes or markers
|
|
608
|
-
const index = [true, false]
|
|
609
|
-
.map((hasHardLineBreak) => {
|
|
610
|
-
let end = line.length - (hasHardLineBreak && line.endsWith('\\') ? 1 : 0);
|
|
611
|
-
let backslashes = 0;
|
|
612
|
-
|
|
613
|
-
while (end > 0 && '*_~'.includes(line[end - 1])) {
|
|
614
|
-
end -= 1;
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
while (line[end - backslashes - 1] === '\\') {
|
|
618
|
-
backslashes += 1;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
return backslashes && backslashes % 2 === 0 ? end : -1;
|
|
622
|
-
})
|
|
623
|
-
.find((end) => end !== -1);
|
|
624
|
-
|
|
625
|
-
return index === undefined
|
|
626
|
-
? line
|
|
627
|
-
: `${line.slice(0, index)}${ESCAPED_BACKSLASH_PLACEHOLDER}${line.slice(index)}`;
|
|
628
|
-
};
|
|
629
|
-
|
|
630
|
-
/**
|
|
631
|
-
* Regular expression to match the place between a formatting marker run that isn’t escaped and a
|
|
632
|
-
* backslash: after the run, like in `_foo_\`, or before it, after an escaped backslash, like in
|
|
633
|
-
* `\\_foo_`.
|
|
634
|
-
*/
|
|
635
|
-
const MARKER_BACKSLASH_REGEX =
|
|
636
|
-
// Each lookahead comes first, so its lookbehind is only tried next to a marker or a backslash
|
|
637
|
-
/(?=\\)(?<=(?:^|[^\\])(?:\\\\)*[*_~])|(?=[*_~])(?<=(?:^|[^\\])(?:\\\\)+)/g;
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Put a period, which Lexical takes as punctuation, between a backslash and a formatting marker.
|
|
641
|
-
* Unlike CommonMark, Lexical doesn’t take a backslash as punctuation, so it reads the marker as
|
|
642
|
-
* being within a word: it doesn’t read `_foo_\` or `**foo.**\` as formatted, as the marker can’t
|
|
643
|
-
* close the formatting there, nor `\\_foo_`, as it can’t open it. The period gives the marker the
|
|
644
|
-
* same neighbor as in CommonMark. A placeholder on the backslash side lets it be removed, and keeps
|
|
645
|
-
* it from being escaped by the backslash.
|
|
646
|
-
* @param {string} line Line.
|
|
647
|
-
* @returns {string} Processed line.
|
|
648
|
-
* @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
|
|
649
|
-
*/
|
|
650
|
-
const markMarkersNextToBackslashes = (line) =>
|
|
651
|
-
/[*_~]\\|\\[*_~]/.test(line)
|
|
652
|
-
? line.replace(MARKER_BACKSLASH_REGEX, (_match, index) =>
|
|
653
|
-
// The period goes next to the marker
|
|
654
|
-
'*_~'.includes(line[index]) ? `${PUNCTUATION_PLACEHOLDER}.` : `.${PUNCTUATION_PLACEHOLDER}`,
|
|
655
|
-
)
|
|
656
|
-
: line;
|
|
657
|
-
|
|
658
|
-
/**
|
|
659
|
-
* Regular expression to match the place between a formatting marker and a punctuation character or
|
|
660
|
-
* a symbol other than ASCII, like an en dash or a curly quote.
|
|
661
|
-
*/
|
|
662
|
-
const MARKER_UNICODE_PUNCTUATION_REGEX =
|
|
663
|
-
/(?<=[*_~])(?=(?![\0-\x7F])[\p{P}\p{S}])|(?<=(?![\0-\x7F])[\p{P}\p{S}])(?=[*_~])/gu;
|
|
664
|
-
|
|
665
|
-
/**
|
|
666
|
-
* Put a period, which Lexical takes as punctuation, between a formatting marker and a punctuation
|
|
667
|
-
* character or a symbol other than ASCII. Unlike CommonMark, Lexical only takes ASCII characters as
|
|
668
|
-
* punctuation, so it reads the marker as being next to a letter: it doesn’t read `**[a](u)**` in
|
|
669
|
-
* `–**[a](u)**` or `“**[a](u)**”` as bold, as the marker can’t open or close the formatting there.
|
|
670
|
-
* The period gives the marker the same neighbor as in CommonMark. A placeholder on the other side
|
|
671
|
-
* lets it be removed.
|
|
672
|
-
* @param {string} line Line.
|
|
673
|
-
* @returns {string} Processed line.
|
|
674
|
-
* @see https://spec.commonmark.org/0.31.2/#unicode-punctuation-character
|
|
675
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/1107
|
|
676
|
-
*/
|
|
677
|
-
const markMarkersNextToUnicodePunctuation = (line) =>
|
|
678
|
-
/[^\0-\x7F]/.test(line)
|
|
679
|
-
? line.replace(MARKER_UNICODE_PUNCTUATION_REGEX, (_match, index) =>
|
|
680
|
-
// The period goes next to the marker
|
|
681
|
-
'*_~'.includes(line[index - 1])
|
|
682
|
-
? `.${PUNCTUATION_PLACEHOLDER}`
|
|
683
|
-
: `${PUNCTUATION_PLACEHOLDER}.`,
|
|
684
|
-
)
|
|
685
|
-
: line;
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
* Replace the tabs in the code spans of a line, and the spaces in a code span made only of spaces,
|
|
689
|
-
* with placeholders. Lexical turns a tab into a tab node, which has no format, so a tab in a code
|
|
690
|
-
* span would leave the code span, and a code span with only tabs would be exported as text. Also,
|
|
691
|
-
* Lexical takes a paragraph with up to 3 whitespace characters as empty, and removes it, even if
|
|
692
|
-
* they’re code, like `` ` ` ``. The spaces of other code spans are left alone, as one of them is
|
|
693
|
-
* stripped from both ends of the code span if the code isn’t all spaces.
|
|
694
|
-
* @param {string} line Line.
|
|
695
|
-
* @returns {string} Processed line.
|
|
696
|
-
* @see https://spec.commonmark.org/0.31.2/#code-spans
|
|
697
|
-
*/
|
|
698
|
-
const markCodeSpanWhitespace = (line) => {
|
|
699
|
-
if (!/`[ \t]|\t/.test(line)) {
|
|
700
|
-
return line;
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
let result = '';
|
|
704
|
-
let index = 0;
|
|
705
|
-
|
|
706
|
-
findCodeSpans(line).forEach(({ start, end, fenceLength }) => {
|
|
707
|
-
const content = line.slice(start + fenceLength, end - fenceLength);
|
|
708
|
-
|
|
709
|
-
result += `${line.slice(index, start + fenceLength)}${
|
|
710
|
-
/^ +$/.test(content)
|
|
711
|
-
? CODE_SPACE_PLACEHOLDER.repeat(content.length)
|
|
712
|
-
: content.replaceAll('\t', CODE_TAB_PLACEHOLDER)
|
|
713
|
-
}`;
|
|
714
|
-
index = end - fenceLength;
|
|
715
|
-
});
|
|
716
|
-
|
|
717
|
-
return result + line.slice(index);
|
|
718
|
-
};
|
|
719
|
-
|
|
720
|
-
/**
|
|
721
|
-
* Mark the lines of the HTML blocks in a part of a Markdown string with a placeholder at the start,
|
|
722
|
-
* so they’re imported as raw HTML, without the Markdown syntax being read; see `RAW_HTML`. Their
|
|
723
|
-
* tabs, and those in the inline HTML of the other lines, are replaced with a placeholder, as
|
|
724
|
-
* Lexical would otherwise split the raw HTML into separate nodes around a tab node.
|
|
725
|
-
* @param {string} part Part of a Markdown string outside fenced code blocks.
|
|
726
|
-
* @returns {string} Processed part.
|
|
727
|
-
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
728
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/1106
|
|
729
|
-
*/
|
|
730
|
-
const markHtml = (part) => {
|
|
731
|
-
/**
|
|
732
|
-
* End condition of the current HTML block: a regular expression, `null` for a blank line, or
|
|
733
|
-
* `undefined` if the line is not in an HTML block.
|
|
734
|
-
* @type {RegExp | null | undefined}
|
|
735
|
-
*/
|
|
736
|
-
let end;
|
|
737
|
-
let isInParagraph = false;
|
|
738
|
-
|
|
739
|
-
return part
|
|
740
|
-
.split('\n')
|
|
741
|
-
.map((line) => {
|
|
742
|
-
const isBlankLine = !line.trim();
|
|
743
|
-
|
|
744
|
-
if (end === null && isBlankLine) {
|
|
745
|
-
end = undefined;
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
if (end === undefined) {
|
|
749
|
-
const condition = HTML_BLOCK_CONDITIONS.find(([start]) => start.test(line));
|
|
750
|
-
|
|
751
|
-
if (condition) {
|
|
752
|
-
[, end] = condition;
|
|
753
|
-
} else if (!isInParagraph && STANDALONE_HTML_TAG_REGEX.test(line)) {
|
|
754
|
-
end = null;
|
|
755
|
-
} else {
|
|
756
|
-
// A heading or a thematic break doesn’t leave a paragraph open, so a standalone tag
|
|
757
|
-
// after it starts an HTML block. A line like `===` is a heading underline only after a
|
|
758
|
-
// paragraph line, and paragraph text otherwise
|
|
759
|
-
isInParagraph =
|
|
760
|
-
!isBlankLine &&
|
|
761
|
-
!SINGLE_LINE_BLOCK_REGEX.test(line) &&
|
|
762
|
-
!(isInParagraph && SETEXT_UNDERLINE_REGEX.test(line));
|
|
763
|
-
|
|
764
|
-
return line.includes('\t')
|
|
765
|
-
? line.replace(GLOBAL_INLINE_HTML_REGEX, (tag) =>
|
|
766
|
-
tag.replaceAll('\t', CODE_TAB_PLACEHOLDER),
|
|
767
|
-
)
|
|
768
|
-
: line;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
if (end?.test(line)) {
|
|
773
|
-
end = undefined;
|
|
774
|
-
}
|
|
775
|
-
|
|
776
|
-
isInParagraph = false;
|
|
777
|
-
|
|
778
|
-
return `${HTML_BLOCK_PLACEHOLDER}${line.replaceAll('\t', CODE_TAB_PLACEHOLDER)}`;
|
|
779
|
-
})
|
|
780
|
-
.join('\n');
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
/**
|
|
784
|
-
* Add placeholders to a Markdown string, so Lexical reads it as CommonMark does; see
|
|
785
|
-
* `markLineEndBackslashes()`, `markMarkersNextToBackslashes()`,
|
|
786
|
-
* `markMarkersNextToUnicodePunctuation()` and `markCodeSpanWhitespace()`.
|
|
787
|
-
* Lines in fenced code blocks are left untouched. The placeholders are removed from the imported
|
|
788
|
-
* text with `removeImportPlaceholders()`.
|
|
789
|
-
* @param {string} value Markdown string to process.
|
|
790
|
-
* @returns {string} Processed Markdown string.
|
|
791
|
-
*/
|
|
792
|
-
export const addImportPlaceholders = (value) =>
|
|
793
|
-
mapLinesOutsideCodeBlocks(
|
|
794
|
-
value.includes('<') ? mapOutsideCodeBlocks(value, markHtml) : value,
|
|
795
|
-
(line) =>
|
|
796
|
-
markCodeSpanWhitespace(
|
|
797
|
-
markMarkersNextToUnicodePunctuation(
|
|
798
|
-
markMarkersNextToBackslashes(markLineEndBackslashes(line)),
|
|
799
|
-
),
|
|
800
|
-
),
|
|
801
|
-
);
|
|
802
|
-
|
|
803
|
-
/**
|
|
804
|
-
* Remove the placeholders added by `addImportPlaceholders()` from imported text.
|
|
805
|
-
* @param {string} text Text.
|
|
806
|
-
* @returns {string} Text without the placeholders.
|
|
807
|
-
*/
|
|
808
|
-
export const removeImportPlaceholders = (text) =>
|
|
809
|
-
text
|
|
810
|
-
.replace(new RegExp(`\\.${PUNCTUATION_PLACEHOLDER}|${PUNCTUATION_PLACEHOLDER}\\.`, 'g'), '')
|
|
811
|
-
.replaceAll(ESCAPED_BACKSLASH_PLACEHOLDER, '')
|
|
812
|
-
.replaceAll(HTML_BLOCK_PLACEHOLDER, '')
|
|
813
|
-
.replaceAll(CODE_TAB_PLACEHOLDER, '\t')
|
|
814
|
-
.replaceAll(CODE_SPACE_PLACEHOLDER, ' ');
|
|
815
|
-
|
|
816
|
-
/**
|
|
817
|
-
* Convert the fenced code blocks using tildes, like `~~~`, to ones using backticks, because Lexical
|
|
818
|
-
* only supports the latter. The new fence is made longer than any backtick fence in the code, so
|
|
819
|
-
* the code block isn’t closed early. Backticks can’t be used in the info string of a backtick
|
|
820
|
-
* fence, so those after the language are replaced with a placeholder, which the code block
|
|
821
|
-
* transformer turns back into backticks, exporting a tilde fence again; see `CODE`. Other backticks
|
|
822
|
-
* are removed.
|
|
823
|
-
* @param {string} value Markdown string to process.
|
|
824
|
-
* @returns {string} Processed Markdown string.
|
|
825
|
-
*/
|
|
826
|
-
export const convertTildeFences = (value) => {
|
|
827
|
-
if (!/^[ \t]*~{3,}/m.test(value)) {
|
|
828
|
-
return value;
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
const lines = value.split('\n');
|
|
832
|
-
|
|
833
|
-
findCodeBlocks(lines)
|
|
834
|
-
.filter(({ fence }) => fence.startsWith('~'))
|
|
835
|
-
.forEach(({ start, end, closed }) => {
|
|
836
|
-
const codeLines = lines.slice(start + 1, closed ? end : end + 1);
|
|
837
|
-
|
|
838
|
-
const longestFence = Math.max(
|
|
839
|
-
2,
|
|
840
|
-
...codeLines.map(
|
|
841
|
-
(line) => /** @type {RegExpMatchArray} */ (line.match(/^[ \t]*(`*)/))[1].length,
|
|
842
|
-
),
|
|
843
|
-
);
|
|
844
|
-
|
|
845
|
-
const backticks = '`'.repeat(longestFence + 1);
|
|
846
|
-
|
|
847
|
-
// Unlike that of a tilde fence, the info string of a backtick fence can’t contain a backtick
|
|
848
|
-
lines[start] = lines[start].replace(
|
|
849
|
-
/^([ \t]*)~+([ \t]*[\w-]+)?(.*)$/,
|
|
850
|
-
(_, indent, language, rest) =>
|
|
851
|
-
`${indent}${backticks}${language ?? ''}${rest.replaceAll('`', language ? INFO_BACKTICK : '')}`,
|
|
852
|
-
);
|
|
853
|
-
|
|
854
|
-
if (closed) {
|
|
855
|
-
lines[end] = lines[end].replace(/^([ \t]*)~+[ \t]*$/, `$1${backticks}`);
|
|
856
|
-
}
|
|
857
|
-
});
|
|
858
|
-
|
|
859
|
-
return lines.join('\n');
|
|
860
|
-
};
|
|
861
|
-
|
|
862
|
-
/**
|
|
863
|
-
* Check if the given line is part of a paragraph.
|
|
864
|
-
* @param {string} line Line.
|
|
865
|
-
* @returns {boolean} Result.
|
|
866
|
-
*/
|
|
867
|
-
const isParagraphLine = (line) => !!line.trim() && !NON_PARAGRAPH_LINE_REGEX.test(line);
|
|
868
|
-
|
|
869
|
-
/**
|
|
870
|
-
* Convert setext headings, underlined with `===` or `---`, to ATX headings, like `# Heading`,
|
|
871
|
-
* because Lexical only supports the latter. Without it, the underline is imported as text or a
|
|
872
|
-
* horizontal rule. Fenced code blocks are left untouched. To avoid misreading a horizontal rule,
|
|
873
|
-
* only a paragraph that follows a blank line, an ATX heading or the start of the document is
|
|
874
|
-
* converted; for example, the second `---` of a block like front matter, which starts with a
|
|
875
|
-
* horizontal rule, is left as is.
|
|
876
|
-
* @param {string} value Markdown string to process.
|
|
877
|
-
* @returns {string} Processed Markdown string.
|
|
878
|
-
*/
|
|
879
|
-
export const convertSetextHeadings = (value) => {
|
|
880
|
-
if (!/^ {0,3}(?:=+|-+)[ \t]*$/m.test(value)) {
|
|
881
|
-
return value;
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
return mapOutsideCodeBlocks(value, (part) => {
|
|
885
|
-
/** @type {string[]} */
|
|
886
|
-
const lines = [];
|
|
887
|
-
|
|
888
|
-
part.split('\n').forEach((line) => {
|
|
889
|
-
const [, underline] = line.match(SETEXT_UNDERLINE_REGEX) ?? [];
|
|
890
|
-
let start = lines.length;
|
|
891
|
-
|
|
892
|
-
while (start > 0 && isParagraphLine(lines[start - 1])) {
|
|
893
|
-
start -= 1;
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
const previous = lines[start - 1];
|
|
897
|
-
|
|
898
|
-
if (
|
|
899
|
-
!underline ||
|
|
900
|
-
start === lines.length ||
|
|
901
|
-
(previous !== undefined && previous.trim() && !ATX_HEADING_REGEX.test(previous))
|
|
902
|
-
) {
|
|
903
|
-
lines.push(line);
|
|
904
|
-
|
|
905
|
-
return;
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
const text = lines
|
|
909
|
-
.splice(start)
|
|
910
|
-
.map((paragraphLine) => paragraphLine.trim())
|
|
911
|
-
.join(' ');
|
|
912
|
-
|
|
913
|
-
lines.push(`${underline.startsWith('=') ? '#' : '##'} ${text}`);
|
|
914
|
-
});
|
|
915
|
-
|
|
916
|
-
return lines.join('\n');
|
|
917
|
-
});
|
|
918
|
-
};
|
|
919
|
-
|
|
920
|
-
/**
|
|
921
|
-
* Add a trailing space to a blank blockquote line (`>`) so Lexical treats it as a blank line within
|
|
922
|
-
* the blockquote. Without the space, the line does not match Lexical's `/^>\s/` quote pattern and
|
|
923
|
-
* is imported as literal `>` text, which is then exported as `> >`, an empty nested blockquote.
|
|
924
|
-
* @param {string} value Markdown string to process.
|
|
925
|
-
* @returns {string} Processed Markdown string.
|
|
926
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/995
|
|
927
|
-
*/
|
|
928
|
-
export const padBlankBlockquoteLines = (value) => {
|
|
929
|
-
if (!/^>$/m.test(value)) {
|
|
930
|
-
return value;
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
return mapLinesOutsideCodeBlocks(value, (line) => (line === '>' ? '> ' : line));
|
|
934
|
-
};
|
|
935
|
-
|
|
936
|
-
/**
|
|
937
|
-
* Remove the trailing space Lexical outputs for a blank blockquote line (`> `) so the line
|
|
938
|
-
* round-trips as `>`, the canonical form.
|
|
939
|
-
* @param {string} value Markdown string to process.
|
|
940
|
-
* @returns {string} Processed Markdown string.
|
|
941
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/995
|
|
942
|
-
*/
|
|
943
|
-
export const trimBlankBlockquoteLines = (value) => {
|
|
944
|
-
if (!/^> $/m.test(value)) {
|
|
945
|
-
return value;
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
return mapLinesOutsideCodeBlocks(value, (line) => (line === '> ' ? '>' : line));
|
|
949
|
-
};
|