@14ch/svelte-md-outliner 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +190 -0
- package/README.md +190 -0
- package/dist/assets/styles/editor.css +1459 -0
- package/dist/assets/styles/theme.css +486 -0
- package/dist/components/ContextMenu.svelte +279 -0
- package/dist/components/ContextMenu.svelte.d.ts +9 -0
- package/dist/components/EditorSurface.svelte +99 -0
- package/dist/components/EditorSurface.svelte.d.ts +16 -0
- package/dist/components/FocusBreadcrumb.svelte +138 -0
- package/dist/components/FocusBreadcrumb.svelte.d.ts +16 -0
- package/dist/components/MarkdownEditor.svelte +393 -0
- package/dist/components/MarkdownEditor.svelte.d.ts +119 -0
- package/dist/components/ShortcutHelpDialog.svelte +155 -0
- package/dist/components/ShortcutHelpDialog.svelte.d.ts +12 -0
- package/dist/components/internal/Dialog.svelte +157 -0
- package/dist/components/internal/Dialog.svelte.d.ts +23 -0
- package/dist/components/internal/Modal.svelte +181 -0
- package/dist/components/internal/Modal.svelte.d.ts +18 -0
- package/dist/components/internal/Popup.svelte +248 -0
- package/dist/components/internal/Popup.svelte.d.ts +26 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +28 -0
- package/dist/markdown.d.ts +13 -0
- package/dist/markdown.js +10 -0
- package/dist/messages/en.d.ts +2 -0
- package/dist/messages/en.js +107 -0
- package/dist/messages/index.d.ts +13 -0
- package/dist/messages/index.js +31 -0
- package/dist/messages/ja.d.ts +2 -0
- package/dist/messages/ja.js +112 -0
- package/dist/messages/messages.types.d.ts +59 -0
- package/dist/messages/messages.types.js +1 -0
- package/dist/models/document/bodySelection.d.ts +32 -0
- package/dist/models/document/bodySelection.js +43 -0
- package/dist/models/document/document.types.d.ts +27 -0
- package/dist/models/document/document.types.js +8 -0
- package/dist/models/document/foldable.d.ts +18 -0
- package/dist/models/document/foldable.js +21 -0
- package/dist/models/document/schema.d.ts +2 -0
- package/dist/models/document/schema.js +428 -0
- package/dist/models/document/sectionTree.d.ts +15 -0
- package/dist/models/document/sectionTree.js +123 -0
- package/dist/models/markdown/blockOutline.d.ts +9 -0
- package/dist/models/markdown/blockOutline.js +82 -0
- package/dist/models/markdown/frontMatter.d.ts +98 -0
- package/dist/models/markdown/frontMatter.js +300 -0
- package/dist/models/markdown/markdown.types.d.ts +60 -0
- package/dist/models/markdown/markdown.types.js +1 -0
- package/dist/models/markdown/markdownIt.d.ts +1 -0
- package/dist/models/markdown/markdownIt.js +92 -0
- package/dist/models/markdown/parser.d.ts +4 -0
- package/dist/models/markdown/parser.js +505 -0
- package/dist/models/markdown/plainText.d.ts +1 -0
- package/dist/models/markdown/plainText.js +25 -0
- package/dist/models/markdown/serializer.d.ts +17 -0
- package/dist/models/markdown/serializer.js +453 -0
- package/dist/models/viewState/viewState.types.d.ts +38 -0
- package/dist/models/viewState/viewState.types.js +17 -0
- package/dist/models/viewState/viewStateCodec.d.ts +31 -0
- package/dist/models/viewState/viewStateCodec.js +173 -0
- package/dist/operations/autoformat.d.ts +9 -0
- package/dist/operations/autoformat.js +86 -0
- package/dist/operations/commands/block.d.ts +24 -0
- package/dist/operations/commands/block.js +139 -0
- package/dist/operations/commands/codeBlock.d.ts +7 -0
- package/dist/operations/commands/codeBlock.js +26 -0
- package/dist/operations/commands/element.d.ts +67 -0
- package/dist/operations/commands/element.js +269 -0
- package/dist/operations/commands/frontMatter.d.ts +35 -0
- package/dist/operations/commands/frontMatter.js +120 -0
- package/dist/operations/commands/lineBreak.d.ts +9 -0
- package/dist/operations/commands/lineBreak.js +16 -0
- package/dist/operations/commands/link.d.ts +7 -0
- package/dist/operations/commands/link.js +21 -0
- package/dist/operations/commands/list.d.ts +126 -0
- package/dist/operations/commands/list.js +652 -0
- package/dist/operations/commands/mermaidBlock.d.ts +9 -0
- package/dist/operations/commands/mermaidBlock.js +28 -0
- package/dist/operations/commands/section.d.ts +217 -0
- package/dist/operations/commands/section.js +865 -0
- package/dist/operations/commands/sectionFold.d.ts +36 -0
- package/dist/operations/commands/sectionFold.js +219 -0
- package/dist/operations/commands/sectionJump.d.ts +8 -0
- package/dist/operations/commands/sectionJump.js +144 -0
- package/dist/operations/commands/selectedBlock.d.ts +37 -0
- package/dist/operations/commands/selectedBlock.js +170 -0
- package/dist/operations/commands/table.d.ts +64 -0
- package/dist/operations/commands/table.js +437 -0
- package/dist/operations/keymap/assemble.d.ts +22 -0
- package/dist/operations/keymap/assemble.js +165 -0
- package/dist/operations/keymap/coreActions.d.ts +2 -0
- package/dist/operations/keymap/coreActions.js +711 -0
- package/dist/operations/keymap/menuItems.d.ts +52 -0
- package/dist/operations/keymap/menuItems.js +114 -0
- package/dist/operations/keymap/shortcutKeys.d.ts +42 -0
- package/dist/operations/keymap/shortcutKeys.js +114 -0
- package/dist/operations/keymap/viewActions.d.ts +30 -0
- package/dist/operations/keymap/viewActions.js +182 -0
- package/dist/operations/operations.types.d.ts +121 -0
- package/dist/operations/operations.types.js +1 -0
- package/dist/operations/plugins/blockHandle.d.ts +25 -0
- package/dist/operations/plugins/blockHandle.js +112 -0
- package/dist/operations/plugins/contextMenu.d.ts +33 -0
- package/dist/operations/plugins/contextMenu.js +204 -0
- package/dist/operations/plugins/focus.d.ts +39 -0
- package/dist/operations/plugins/focus.js +350 -0
- package/dist/operations/plugins/foldClick.d.ts +11 -0
- package/dist/operations/plugins/foldClick.js +117 -0
- package/dist/operations/plugins/foldGuard.d.ts +15 -0
- package/dist/operations/plugins/foldGuard.js +31 -0
- package/dist/operations/plugins/frontMatterGuard.d.ts +21 -0
- package/dist/operations/plugins/frontMatterGuard.js +150 -0
- package/dist/operations/plugins/frontMatterView.d.ts +34 -0
- package/dist/operations/plugins/frontMatterView.js +137 -0
- package/dist/operations/plugins/linkPaste.d.ts +3 -0
- package/dist/operations/plugins/linkPaste.js +20 -0
- package/dist/operations/plugins/listStructure.d.ts +13 -0
- package/dist/operations/plugins/listStructure.js +18 -0
- package/dist/operations/plugins/markdownClipboard.d.ts +8 -0
- package/dist/operations/plugins/markdownClipboard.js +208 -0
- package/dist/operations/plugins/mermaidView.d.ts +20 -0
- package/dist/operations/plugins/mermaidView.js +206 -0
- package/dist/operations/plugins/requiredSelectionEntry.d.ts +10 -0
- package/dist/operations/plugins/requiredSelectionEntry.js +124 -0
- package/dist/operations/plugins/sectionView.d.ts +12 -0
- package/dist/operations/plugins/sectionView.js +44 -0
- package/dist/operations/plugins/tabEscape.d.ts +21 -0
- package/dist/operations/plugins/tabEscape.js +41 -0
- package/dist/operations/plugins/tableHandles.d.ts +24 -0
- package/dist/operations/plugins/tableHandles.js +89 -0
- package/dist/operations/transaction.d.ts +26 -0
- package/dist/operations/transaction.js +51 -0
- package/dist/operations/transactionOrigin.d.ts +17 -0
- package/dist/operations/transactionOrigin.js +9 -0
- package/dist/states/contextMenuController.svelte.d.ts +57 -0
- package/dist/states/contextMenuController.svelte.js +206 -0
- package/dist/states/editorSession.d.ts +12 -0
- package/dist/states/editorSession.js +268 -0
- package/dist/states/editorSession.types.d.ts +57 -0
- package/dist/states/editorSession.types.js +1 -0
- package/dist/states/outlineViewController.svelte.d.ts +52 -0
- package/dist/states/outlineViewController.svelte.js +134 -0
- package/dist/utils/dateText.d.ts +25 -0
- package/dist/utils/dateText.js +71 -0
- package/dist/utils/geometry.types.d.ts +13 -0
- package/dist/utils/geometry.types.js +1 -0
- package/dist/utils/json.types.d.ts +4 -0
- package/dist/utils/json.types.js +1 -0
- package/dist/utils/platform.d.ts +8 -0
- package/dist/utils/platform.js +10 -0
- package/dist/utils/result.types.d.ts +7 -0
- package/dist/utils/result.types.js +1 -0
- package/package.json +109 -0
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import { Fragment, Mark } from 'prosemirror-model';
|
|
2
|
+
import { MAX_HEADING_LEVEL } from '../document/document.types';
|
|
3
|
+
import { outlineSchema } from '../document/schema';
|
|
4
|
+
import { splitFrontMatter } from './frontMatter.js';
|
|
5
|
+
import { markdownIt } from './markdownIt.js';
|
|
6
|
+
const n = outlineSchema.nodes;
|
|
7
|
+
const m = outlineSchema.marks;
|
|
8
|
+
/**
|
|
9
|
+
* チェックボックス。後ろの空白は**中身があるときだけ**要る。
|
|
10
|
+
*
|
|
11
|
+
* 中身の無い項目は行が `- [ ]` で終わり、印の後ろの空白は読み込みの時点で落ちている。
|
|
12
|
+
* 空白を必須にすると、書きかけのチェック項目が読み直しでチェックを失い、`[ ]` という
|
|
13
|
+
* 文字の箇条書きになる。
|
|
14
|
+
*/
|
|
15
|
+
const TASK_MARKER = /^\[([ xX])\](\s+|$)/;
|
|
16
|
+
/**
|
|
17
|
+
* 行の原文にエスケープ記号(記号の前に置く `\`)があるか。
|
|
18
|
+
* マークダウンでエスケープできるのは ASCII の記号だけなので、その範囲に限る
|
|
19
|
+
* (`C:\path` のような素のバックスラッシュを拾わない)。
|
|
20
|
+
*/
|
|
21
|
+
const ESCAPE_IN_SOURCE = /\\[!-/:-@[-`{-~]/;
|
|
22
|
+
export function parseMarkdown(markdown) {
|
|
23
|
+
try {
|
|
24
|
+
const { content, body } = splitFrontMatter(markdown);
|
|
25
|
+
const tokens = markdownIt.parse(body, {});
|
|
26
|
+
return { ok: true, value: buildDoc(tokens, content) };
|
|
27
|
+
}
|
|
28
|
+
catch (cause) {
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
error: {
|
|
32
|
+
kind: 'invalid-structure',
|
|
33
|
+
detail: cause instanceof Error ? cause.message : String(cause)
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** `frontMatterContent` は区切りの内側。フロントマターが無ければ空文字列 */
|
|
39
|
+
function buildDoc(tokens, frontMatterContent) {
|
|
40
|
+
const introBlocks = [];
|
|
41
|
+
const rootSections = [];
|
|
42
|
+
const stack = [];
|
|
43
|
+
const closeTo = (depth) => {
|
|
44
|
+
while (stack.length > depth) {
|
|
45
|
+
const done = stack.pop();
|
|
46
|
+
const section = n.section.createChecked(null, [
|
|
47
|
+
done.heading,
|
|
48
|
+
...done.blocks,
|
|
49
|
+
...done.sections
|
|
50
|
+
]);
|
|
51
|
+
if (stack.length > 0)
|
|
52
|
+
stack[stack.length - 1].sections.push(section);
|
|
53
|
+
else
|
|
54
|
+
rootSections.push(section);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const pushBlock = (block) => {
|
|
58
|
+
if (stack.length === 0)
|
|
59
|
+
introBlocks.push(block);
|
|
60
|
+
else
|
|
61
|
+
stack[stack.length - 1].blocks.push(block);
|
|
62
|
+
};
|
|
63
|
+
for (let i = 0; i < tokens.length; i += 1) {
|
|
64
|
+
const token = tokens[i];
|
|
65
|
+
if (token.type === 'heading_open') {
|
|
66
|
+
const sourceLevel = Number(token.tag.slice(1));
|
|
67
|
+
// 同じか浅い見出しに出会ったら、その分だけ入れ子から抜ける
|
|
68
|
+
while (stack.length > 0 && stack[stack.length - 1].sourceLevel >= sourceLevel) {
|
|
69
|
+
closeTo(stack.length - 1);
|
|
70
|
+
}
|
|
71
|
+
// レベルは原文の `#` の個数ではなく、構造上の深さから決める
|
|
72
|
+
const level = clampLevel(stack.length + 1);
|
|
73
|
+
const inline = tokens[i + 1];
|
|
74
|
+
const heading = n.heading.createChecked({ level }, parseInline(inline, n.heading));
|
|
75
|
+
stack.push({ sourceLevel, heading, blocks: [], sections: [] });
|
|
76
|
+
i += 2; // inline と heading_close を読み飛ばす
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const consumed = readBlock(tokens, i);
|
|
80
|
+
if (!consumed)
|
|
81
|
+
continue;
|
|
82
|
+
for (const block of consumed.nodes)
|
|
83
|
+
pushBlock(block);
|
|
84
|
+
i = consumed.next - 1;
|
|
85
|
+
}
|
|
86
|
+
closeTo(0);
|
|
87
|
+
const children = [];
|
|
88
|
+
if (introBlocks.length > 0)
|
|
89
|
+
children.push(n.intro_section.createChecked(null, introBlocks));
|
|
90
|
+
children.push(...rootSections);
|
|
91
|
+
if (children.length === 0) {
|
|
92
|
+
// 文書は常に 1 行以上を持つ
|
|
93
|
+
children.push(n.intro_section.createChecked(null, n.paragraph.createChecked(null)));
|
|
94
|
+
}
|
|
95
|
+
// フロントマターは中身が空でも置く。無くすと書き足す入口が失われる
|
|
96
|
+
const frontMatter = n.front_matter.createChecked(null, frontMatterContent ? outlineSchema.text(frontMatterContent) : null);
|
|
97
|
+
return n.doc.createChecked(null, [frontMatter, ...children]);
|
|
98
|
+
}
|
|
99
|
+
/** 位置 `start` から始まるブロックを読み、消費し終わった次の位置を返す */
|
|
100
|
+
function readBlock(tokens, start) {
|
|
101
|
+
const token = tokens[start];
|
|
102
|
+
switch (token.type) {
|
|
103
|
+
/**
|
|
104
|
+
* 引用や項目の中の見出し。ここへ来るのは**入れ子の中の見出しだけ**である
|
|
105
|
+
* (最上位の見出しは `buildDoc` が先に受け止め、セクションの入れ子にする)。
|
|
106
|
+
*
|
|
107
|
+
* レベルは `#` の個数ではなくトークンの種別から採る。下線を敷く書き方
|
|
108
|
+
* (`タイトル` の次の行に `===`)も同じ規則で覆えるためである。
|
|
109
|
+
*/
|
|
110
|
+
case 'heading_open': {
|
|
111
|
+
const inline = tokens[start + 1];
|
|
112
|
+
return {
|
|
113
|
+
nodes: [
|
|
114
|
+
n.nested_heading.createChecked({ level: clampLevel(Number(token.tag.slice(1))) }, parseInline(inline, n.nested_heading))
|
|
115
|
+
],
|
|
116
|
+
next: start + 3
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
case 'paragraph_open': {
|
|
120
|
+
const inline = tokens[start + 1];
|
|
121
|
+
return {
|
|
122
|
+
nodes: [n.paragraph.createChecked(null, parseInline(inline, n.paragraph))],
|
|
123
|
+
next: start + 3
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
case 'fence':
|
|
127
|
+
case 'code_block': {
|
|
128
|
+
const language = token.info.trim().split(/\s+/)[0] ?? '';
|
|
129
|
+
const content = token.content ? outlineSchema.text(token.content) : null;
|
|
130
|
+
// 書き出しは常に `mermaid` を出すため、表記ゆれ(`Mermaid` 等)を取り込むと
|
|
131
|
+
// 往復でフェンスの文字が変わる。小文字完全一致だけを専用ブロックにする
|
|
132
|
+
return {
|
|
133
|
+
nodes: [
|
|
134
|
+
language === 'mermaid'
|
|
135
|
+
? n.mermaid_block.createChecked(null, content)
|
|
136
|
+
: n.code_block.createChecked({ language }, content)
|
|
137
|
+
],
|
|
138
|
+
next: start + 1
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
case 'hr':
|
|
142
|
+
return { nodes: [n.horizontal_rule.createChecked(null)], next: start + 1 };
|
|
143
|
+
case 'html_block':
|
|
144
|
+
// 原文のまま保持する。中身は解釈しない
|
|
145
|
+
return {
|
|
146
|
+
nodes: [n.unknown_block.createChecked({ source: token.content.replace(/\r?\n$/, '') })],
|
|
147
|
+
next: start + 1
|
|
148
|
+
};
|
|
149
|
+
case 'blockquote_open': {
|
|
150
|
+
const end = matchingClose(tokens, start, 'blockquote_open', 'blockquote_close');
|
|
151
|
+
const blocks = readBlocks(tokens, start + 1, end);
|
|
152
|
+
return {
|
|
153
|
+
nodes: [
|
|
154
|
+
n.blockquote.createChecked(null, blocks.length > 0 ? blocks : [n.paragraph.createChecked(null)])
|
|
155
|
+
],
|
|
156
|
+
next: end + 1
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
case 'bullet_list_open':
|
|
160
|
+
case 'ordered_list_open':
|
|
161
|
+
return readList(tokens, start);
|
|
162
|
+
case 'table_open':
|
|
163
|
+
return readTable(tokens, start);
|
|
164
|
+
default:
|
|
165
|
+
// 想定していない開始トークンは、原文の断片を落とさないよう文字として取り込む
|
|
166
|
+
if (token.content) {
|
|
167
|
+
return {
|
|
168
|
+
nodes: [n.paragraph.createChecked(null, outlineSchema.text(token.content))],
|
|
169
|
+
next: start + 1
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
function readBlocks(tokens, start, end) {
|
|
176
|
+
const blocks = [];
|
|
177
|
+
for (let i = start; i < end;) {
|
|
178
|
+
const consumed = readBlock(tokens, i);
|
|
179
|
+
if (!consumed) {
|
|
180
|
+
i += 1;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
blocks.push(...consumed.nodes);
|
|
184
|
+
i = consumed.next;
|
|
185
|
+
}
|
|
186
|
+
return blocks;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 箇条書きを読む。入れ子は `list` の中に `list` を置く形で表す(flat-list の持ち方)。
|
|
190
|
+
* 1 つの項目が 1 つの `list` ノードになるため、markdown の `ul` は項目数ぶんのノードへ展開される。
|
|
191
|
+
*/
|
|
192
|
+
function readList(tokens, start) {
|
|
193
|
+
const open = tokens[start];
|
|
194
|
+
const ordered = open.type === 'ordered_list_open';
|
|
195
|
+
const closeType = ordered ? 'ordered_list_close' : 'bullet_list_close';
|
|
196
|
+
const end = matchingClose(tokens, start, open.type, closeType);
|
|
197
|
+
const items = [];
|
|
198
|
+
let isFirstItem = true;
|
|
199
|
+
for (let i = start + 1; i < end;) {
|
|
200
|
+
if (tokens[i].type !== 'list_item_open') {
|
|
201
|
+
i += 1;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const itemEnd = matchingClose(tokens, i, 'list_item_open', 'list_item_close');
|
|
205
|
+
const blocks = readBlocks(tokens, i + 1, itemEnd);
|
|
206
|
+
items.push(createListItem(blocks, ordered, isFirstItem, tokens[i]));
|
|
207
|
+
isFirstItem = false;
|
|
208
|
+
i = itemEnd + 1;
|
|
209
|
+
}
|
|
210
|
+
if (items.length === 0) {
|
|
211
|
+
items.push(n.list.createChecked({ kind: 'bullet' }, n.paragraph.createChecked(null)));
|
|
212
|
+
}
|
|
213
|
+
return { nodes: items, next: end + 1 };
|
|
214
|
+
}
|
|
215
|
+
function createListItem(blocks, ordered, isFirstItem, itemToken) {
|
|
216
|
+
const content = withOwnLine(blocks);
|
|
217
|
+
if (!ordered) {
|
|
218
|
+
const task = extractTaskMarker(content[0]);
|
|
219
|
+
if (task) {
|
|
220
|
+
return n.list.createChecked({ kind: 'task', checked: task.checked }, [
|
|
221
|
+
task.paragraph,
|
|
222
|
+
...content.slice(1)
|
|
223
|
+
]);
|
|
224
|
+
}
|
|
225
|
+
return n.list.createChecked({ kind: 'bullet' }, content);
|
|
226
|
+
}
|
|
227
|
+
const start = Number(itemToken.info);
|
|
228
|
+
const order = isFirstItem && Number.isFinite(start) && start !== 1 ? start : null;
|
|
229
|
+
return n.list.createChecked({ kind: 'ordered', order }, keepTaskMarker(content));
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* 番号付きの項目に書かれたチェックボックスを、原文のまま残す。
|
|
233
|
+
*
|
|
234
|
+
* **押せるチェックにはしない。** 箇条書きの仕組みはチェックと番号付きを排他の種別
|
|
235
|
+
* として扱っており(→ `research.md` 調査 5)、押せる形にするには描画を自前で広げる
|
|
236
|
+
* ことになる。**印を文字のまま保つ**のが上限であり、マークを付けないと書き出しが
|
|
237
|
+
* 角括弧をエスケープして `1. \[ \] a` になる。
|
|
238
|
+
*
|
|
239
|
+
* マークを付けるのは**印の部分だけ**で、後ろの本文は普通の文字のままにする。
|
|
240
|
+
*/
|
|
241
|
+
function keepTaskMarker(blocks) {
|
|
242
|
+
const first = blocks[0];
|
|
243
|
+
if (first?.type !== n.paragraph || first.childCount === 0)
|
|
244
|
+
return blocks;
|
|
245
|
+
const head = first.child(0);
|
|
246
|
+
if (!head.isText || !head.text)
|
|
247
|
+
return blocks;
|
|
248
|
+
const match = TASK_MARKER.exec(head.text);
|
|
249
|
+
if (!match)
|
|
250
|
+
return blocks;
|
|
251
|
+
// 印だけで段落が終わらない形は、点の箇条書きと同じ線引きで印と見なさない
|
|
252
|
+
if (!match[2] && first.childCount > 1)
|
|
253
|
+
return blocks;
|
|
254
|
+
const marker = `[${match[1]}]`;
|
|
255
|
+
const rest = head.text.slice(marker.length);
|
|
256
|
+
const children = [outlineSchema.text(marker, m.verbatim.create().addToSet(head.marks))];
|
|
257
|
+
if (rest)
|
|
258
|
+
children.push(outlineSchema.text(rest, head.marks));
|
|
259
|
+
for (let i = 1; i < first.childCount; i += 1)
|
|
260
|
+
children.push(first.child(i));
|
|
261
|
+
return [n.paragraph.createChecked(null, children), ...blocks.slice(1)];
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* 項目に自分の行を持たせる。
|
|
265
|
+
*
|
|
266
|
+
* **配下から始まる項目は、自分の行を持たない。** マークダウンには「1 行目が空の項目」と
|
|
267
|
+
* 「配下から始まる項目」を書き分ける形が無く(`-` の次の行を字下げすればどちらも同じ)、
|
|
268
|
+
* 補わないと空の項目を入れ子にした形が読み直しのたびに 1 行へ潰れる。
|
|
269
|
+
* このエディタでは項目 1 つが 1 行なので、空の行を補って項目の姿をそろえる。
|
|
270
|
+
*/
|
|
271
|
+
function withOwnLine(blocks) {
|
|
272
|
+
if (blocks.length === 0)
|
|
273
|
+
return [n.paragraph.createChecked(null)];
|
|
274
|
+
// 1 行で書けるブロックだけが「項目の行」になれる。コードブロックは文字を持つが、
|
|
275
|
+
// 1 行目は開きの記号であり、項目の記号と同じ行には並べられない
|
|
276
|
+
if (blocks[0].type === n.paragraph || blocks[0].type === n.nested_heading)
|
|
277
|
+
return blocks;
|
|
278
|
+
return [n.paragraph.createChecked(null), ...blocks];
|
|
279
|
+
}
|
|
280
|
+
/** 先頭が `[ ] ` / `[x] ` ならチェック項目として切り出す */
|
|
281
|
+
function extractTaskMarker(block) {
|
|
282
|
+
if (block.type !== n.paragraph || block.childCount === 0)
|
|
283
|
+
return null;
|
|
284
|
+
const first = block.child(0);
|
|
285
|
+
if (!first.isText || !first.text)
|
|
286
|
+
return null;
|
|
287
|
+
const match = TASK_MARKER.exec(first.text);
|
|
288
|
+
if (!match)
|
|
289
|
+
return null;
|
|
290
|
+
// 空白の無い形は、印だけで段落が終わるときに限る。`[x]**太字**` を巻き込まない
|
|
291
|
+
if (!match[2] && block.childCount > 1)
|
|
292
|
+
return null;
|
|
293
|
+
const rest = first.text.slice(match[0].length);
|
|
294
|
+
const remaining = [];
|
|
295
|
+
if (rest)
|
|
296
|
+
remaining.push(outlineSchema.text(rest, first.marks));
|
|
297
|
+
for (let i = 1; i < block.childCount; i += 1)
|
|
298
|
+
remaining.push(block.child(i));
|
|
299
|
+
return {
|
|
300
|
+
checked: match[1].toLowerCase() === 'x',
|
|
301
|
+
paragraph: n.paragraph.createChecked(null, remaining)
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function readTable(tokens, start) {
|
|
305
|
+
const end = matchingClose(tokens, start, 'table_open', 'table_close');
|
|
306
|
+
const rows = [];
|
|
307
|
+
let cells = [];
|
|
308
|
+
let isHeaderRow = false;
|
|
309
|
+
for (let i = start + 1; i < end; i += 1) {
|
|
310
|
+
const token = tokens[i];
|
|
311
|
+
if (token.type === 'thead_open')
|
|
312
|
+
isHeaderRow = true;
|
|
313
|
+
else if (token.type === 'thead_close')
|
|
314
|
+
isHeaderRow = false;
|
|
315
|
+
else if (token.type === 'tr_open')
|
|
316
|
+
cells = [];
|
|
317
|
+
else if (token.type === 'tr_close')
|
|
318
|
+
rows.push(n.table_row.createChecked(null, cells));
|
|
319
|
+
else if (token.type === 'th_open' || token.type === 'td_open') {
|
|
320
|
+
const inline = tokens[i + 1];
|
|
321
|
+
const type = isHeaderRow ? n.table_header : n.table_cell;
|
|
322
|
+
cells.push(type.createChecked({ align: alignOf(token) }, parseInline(inline, type)));
|
|
323
|
+
i += 2;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return { nodes: [n.table.createChecked(null, rows)], next: end + 1 };
|
|
327
|
+
}
|
|
328
|
+
function alignOf(token) {
|
|
329
|
+
const style = token.attrGet('style');
|
|
330
|
+
if (!style)
|
|
331
|
+
return null;
|
|
332
|
+
const match = /text-align:\s*(left|center|right)/.exec(String(style));
|
|
333
|
+
return match?.[1] ?? null;
|
|
334
|
+
}
|
|
335
|
+
// --- インライン ---
|
|
336
|
+
/**
|
|
337
|
+
* 段落内改行を置けるかは**行き先のノードに聞く**(見出しと表のセルは置けない)。
|
|
338
|
+
* 規則をスキーマだけが持つことで、置ける場所を変えたときに読み込み側が取り残されない。
|
|
339
|
+
* 置けない場所では、読めなくなるより繋がる方を採り、空白へ畳む。
|
|
340
|
+
*/
|
|
341
|
+
function parseInline(inline, into) {
|
|
342
|
+
const tokens = inline?.children ?? [];
|
|
343
|
+
const nodes = [];
|
|
344
|
+
const lineBreak = into.contentMatch.matchType(n.hard_break) !== null;
|
|
345
|
+
const insideHtml = htmlElementContent(tokens, inline?.content ?? '');
|
|
346
|
+
let marks = Mark.none;
|
|
347
|
+
const push = (text) => {
|
|
348
|
+
if (text)
|
|
349
|
+
nodes.push(outlineSchema.text(text, marks));
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* 文書モデルで表現できない記法を、**原文の形の文字**として置く。
|
|
353
|
+
* 書き出しで手を加えないマークを付けるのは、素の文字として置くと角括弧がエスケープされ、
|
|
354
|
+
* 記法が別の文字列へ変わってしまうためである(→ スキーマの `verbatim`)。
|
|
355
|
+
*/
|
|
356
|
+
const pushVerbatim = (text, extra) => {
|
|
357
|
+
if (!text)
|
|
358
|
+
return;
|
|
359
|
+
const own = extra ? extra.addToSet(marks) : marks;
|
|
360
|
+
nodes.push(outlineSchema.text(text, m.verbatim.create().addToSet(own)));
|
|
361
|
+
};
|
|
362
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
363
|
+
const token = tokens[index];
|
|
364
|
+
switch (token.type) {
|
|
365
|
+
case 'text':
|
|
366
|
+
if (insideHtml.has(index))
|
|
367
|
+
pushVerbatim(token.content);
|
|
368
|
+
else
|
|
369
|
+
push(token.content);
|
|
370
|
+
break;
|
|
371
|
+
case 'code_inline':
|
|
372
|
+
nodes.push(outlineSchema.text(token.content, m.code.create().addToSet(marks)));
|
|
373
|
+
break;
|
|
374
|
+
// 単なる改行(`softbreak`)は段落の続きであり、改行として書かれたものではない
|
|
375
|
+
case 'softbreak':
|
|
376
|
+
push(' ');
|
|
377
|
+
break;
|
|
378
|
+
case 'hardbreak':
|
|
379
|
+
if (lineBreak)
|
|
380
|
+
nodes.push(n.hard_break.create());
|
|
381
|
+
else
|
|
382
|
+
push(' ');
|
|
383
|
+
break;
|
|
384
|
+
case 'strong_open':
|
|
385
|
+
marks = m.strong.create().addToSet(marks);
|
|
386
|
+
break;
|
|
387
|
+
case 'strong_close':
|
|
388
|
+
marks = m.strong.removeFromSet(marks);
|
|
389
|
+
break;
|
|
390
|
+
case 'em_open':
|
|
391
|
+
marks = m.em.create().addToSet(marks);
|
|
392
|
+
break;
|
|
393
|
+
case 'em_close':
|
|
394
|
+
marks = m.em.removeFromSet(marks);
|
|
395
|
+
break;
|
|
396
|
+
case 's_open':
|
|
397
|
+
marks = m.strikethrough.create().addToSet(marks);
|
|
398
|
+
break;
|
|
399
|
+
case 's_close':
|
|
400
|
+
marks = m.strikethrough.removeFromSet(marks);
|
|
401
|
+
break;
|
|
402
|
+
case 'link_open':
|
|
403
|
+
marks = m.link
|
|
404
|
+
.create({ href: token.attrGet('href') ?? '', title: token.attrGet('title') })
|
|
405
|
+
.addToSet(marks);
|
|
406
|
+
break;
|
|
407
|
+
case 'link_close':
|
|
408
|
+
marks = m.link.removeFromSet(marks);
|
|
409
|
+
break;
|
|
410
|
+
case 'image':
|
|
411
|
+
pushVerbatim(imageNotation(token));
|
|
412
|
+
break;
|
|
413
|
+
// 印であることが画面から分かるように、見た目のマークだけ重ねる
|
|
414
|
+
case 'footnote_ref':
|
|
415
|
+
pushVerbatim(token.content, m.footnote.create());
|
|
416
|
+
break;
|
|
417
|
+
default:
|
|
418
|
+
// html_inline を含む、表現を持たない記法すべて。**記法の名前で分岐しない**
|
|
419
|
+
pushVerbatim(token.content);
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return Fragment.from(nodes);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* インラインの HTML の**要素の中身**にあたるトークンの位置。
|
|
427
|
+
*
|
|
428
|
+
* パーサは `<span>[x]</span>` を「開きのマーク・素の文字 `[x]`・閉じのマーク」の 3 つに割る。
|
|
429
|
+
* マークそのものだけを原文として扱うと、中身の角括弧が書き出しでエスケープされ、**中に角括弧が
|
|
430
|
+
* あるかどうかだけで壊れるかが決まる**(`<b>太字</b>` は無事、`<span>[x]</span>` は壊れる)。
|
|
431
|
+
* 要素の中身も、書き手が書いた HTML の一部として原文のまま持つ。
|
|
432
|
+
*
|
|
433
|
+
* **開きと閉じが揃ったマークのあいだだけを中身とする。** 閉じの無いマーク(`<img>` `<br>`)で
|
|
434
|
+
* 深さを数えると、そこから段落の終わりまでが中身になり、後ろの地の文の角括弧まで
|
|
435
|
+
* エスケープされなくなる(普通に書いた `[a](url)` がリンクとして読み直される)。
|
|
436
|
+
*
|
|
437
|
+
* **書き手が自分でエスケープした行では、この規則を止める。** パースを通った時点で
|
|
438
|
+
* `\[a\]` と `[a]` は同じものになり、区別が付かない。中身を原文として扱うと、
|
|
439
|
+
* 書き手が文字として見せるために付けたエスケープ記号まで「原文」として扱われ、
|
|
440
|
+
* **外れて、読み直すとリンクになる**(`<span>\[a\](url)</span>` → `<span>[a](url)</span>`)。
|
|
441
|
+
* 行の原文にはエスケープ記号が残っているので、それを見て止める。
|
|
442
|
+
*
|
|
443
|
+
* 止めた行では角括弧がエスケープされる(`<span>\[x\]</span>`)。**マークダウンとして
|
|
444
|
+
* 読めば `[x]` であり、意味は変わらない**——エスケープが外れて意味が変わるのとは違う。
|
|
445
|
+
*/
|
|
446
|
+
function htmlElementContent(tokens, source) {
|
|
447
|
+
const inside = new Set();
|
|
448
|
+
// 書き手が自分でエスケープした行では、この規則を止める(→ 上の注)
|
|
449
|
+
if (ESCAPE_IN_SOURCE.test(source))
|
|
450
|
+
return inside;
|
|
451
|
+
const open = [];
|
|
452
|
+
tokens.forEach((token, index) => {
|
|
453
|
+
if (token.type !== 'html_inline')
|
|
454
|
+
return;
|
|
455
|
+
const closing = /^<\/([a-zA-Z][^\s/>]*)/.exec(token.content);
|
|
456
|
+
if (closing) {
|
|
457
|
+
const tag = closing[1].toLowerCase();
|
|
458
|
+
const at = open.findLastIndex((entry) => entry.tag === tag);
|
|
459
|
+
if (at < 0)
|
|
460
|
+
return;
|
|
461
|
+
for (let i = open[at].index + 1; i < index; i += 1)
|
|
462
|
+
inside.add(i);
|
|
463
|
+
open.length = at;
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const opening = /^<([a-zA-Z][^\s/>]*)/.exec(token.content);
|
|
467
|
+
if (opening && !token.content.endsWith('/>')) {
|
|
468
|
+
open.push({ index, tag: opening[1].toLowerCase() });
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
return inside;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* 画像の記法を、トークンの属性から組み立てる。
|
|
475
|
+
*
|
|
476
|
+
* **原文そのものは復元できない。** パーサが返すのは代替文字と属性だけで、
|
|
477
|
+
* 記法の原文(`markup`)も位置(`map`)も持たない。したがって間隔・`<>`・引用符の
|
|
478
|
+
* 種類は正準形へ揃う(`` → ``)。
|
|
479
|
+
* **壊れているのは記法が文字になることであって、書き方ではない。**
|
|
480
|
+
*
|
|
481
|
+
* 参照の形(`![alt][ref]`)も同じ経路を通る。行き先が属性に入っているためである。
|
|
482
|
+
*/
|
|
483
|
+
function imageNotation(token) {
|
|
484
|
+
const src = token.attrGet('src') ?? '';
|
|
485
|
+
const title = String(token.attrGet('title') ?? '');
|
|
486
|
+
const suffix = title ? ` "${title.replace(/"/g, '\\"')}"` : '';
|
|
487
|
+
return ``;
|
|
488
|
+
}
|
|
489
|
+
// --- ヘルパー ---
|
|
490
|
+
function matchingClose(tokens, start, openType, closeType) {
|
|
491
|
+
let depth = 0;
|
|
492
|
+
for (let i = start; i < tokens.length; i += 1) {
|
|
493
|
+
if (tokens[i].type === openType)
|
|
494
|
+
depth += 1;
|
|
495
|
+
else if (tokens[i].type === closeType) {
|
|
496
|
+
depth -= 1;
|
|
497
|
+
if (depth === 0)
|
|
498
|
+
return i;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return tokens.length - 1;
|
|
502
|
+
}
|
|
503
|
+
function clampLevel(depth) {
|
|
504
|
+
return Math.min(Math.max(depth, 1), MAX_HEADING_LEVEL);
|
|
505
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function readPlainText(markdown: string): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { markdownIt } from './markdownIt.js';
|
|
2
|
+
export function readPlainText(markdown) {
|
|
3
|
+
return textOf(markdownIt.parseInline(markdown, {}));
|
|
4
|
+
}
|
|
5
|
+
function textOf(tokens) {
|
|
6
|
+
return tokens.map(readToken).join('');
|
|
7
|
+
}
|
|
8
|
+
function readToken(token) {
|
|
9
|
+
switch (token.type) {
|
|
10
|
+
// コードの中の記号は記法ではない。生の HTML と脚注のマーカーは原文のまま残す
|
|
11
|
+
// (`parser.ts` が受け皿へ入れるものと揃える)
|
|
12
|
+
case 'text':
|
|
13
|
+
case 'code_inline':
|
|
14
|
+
case 'html_inline':
|
|
15
|
+
case 'footnote_ref':
|
|
16
|
+
return token.content;
|
|
17
|
+
// 行の区切りは空白 1 つにする(段落内の改行の扱いを `parser.ts` と揃える)
|
|
18
|
+
case 'softbreak':
|
|
19
|
+
case 'hardbreak':
|
|
20
|
+
return ' ';
|
|
21
|
+
// 開き・閉じのトークンは記法そのもの。中身は `children` から出る
|
|
22
|
+
default:
|
|
23
|
+
return token.children ? textOf(token.children) : '';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Node, type Slice } from 'prosemirror-model';
|
|
2
|
+
/**
|
|
3
|
+
* 文書を Markdown へ書き出す。
|
|
4
|
+
*
|
|
5
|
+
* 見出しのレベルは属性ではなく**構造上の深さ**から決める。属性は深さの写しにすぎず、
|
|
6
|
+
* 階層操作の途中で食い違いうるため、出力の真実は構造側に置く。
|
|
7
|
+
*/
|
|
8
|
+
export declare function serializeMarkdown(doc: Node): string;
|
|
9
|
+
/**
|
|
10
|
+
* 選択範囲を階層構造を保ったまま書き出す(コピー・サブツリーの取り出しに使う)。
|
|
11
|
+
* 見出しのレベルは範囲の中での深さから決まるため、もっとも浅い見出しが最上位になる。
|
|
12
|
+
*
|
|
13
|
+
* **1 行の中で完結する選択は、行ではなく文字として書き出す。** 記法(項目の `-`、
|
|
14
|
+
* 見出しの `#`)は行そのものを表すもので、その一部を写したものには付かない。付けると、
|
|
15
|
+
* 項目の一部をコピーしただけで独立した項目になる。
|
|
16
|
+
*/
|
|
17
|
+
export declare function serializeSlice(slice: Slice): string;
|