@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,453 @@
|
|
|
1
|
+
import { MarkdownSerializer } from 'prosemirror-markdown';
|
|
2
|
+
import { Fragment } from 'prosemirror-model';
|
|
3
|
+
import { MAX_HEADING_LEVEL } from '../document/document.types';
|
|
4
|
+
import { outlineSchema } from '../document/schema';
|
|
5
|
+
const n = outlineSchema.nodes;
|
|
6
|
+
/**
|
|
7
|
+
* 文書を Markdown へ書き出す。
|
|
8
|
+
*
|
|
9
|
+
* 見出しのレベルは属性ではなく**構造上の深さ**から決める。属性は深さの写しにすぎず、
|
|
10
|
+
* 階層操作の途中で食い違いうるため、出力の真実は構造側に置く。
|
|
11
|
+
*/
|
|
12
|
+
export function serializeMarkdown(doc) {
|
|
13
|
+
const body = writeSections(doc, 0).join('\n\n');
|
|
14
|
+
return body ? `${body}\n` : '';
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 選択範囲を階層構造を保ったまま書き出す(コピー・サブツリーの取り出しに使う)。
|
|
18
|
+
* 見出しのレベルは範囲の中での深さから決まるため、もっとも浅い見出しが最上位になる。
|
|
19
|
+
*
|
|
20
|
+
* **1 行の中で完結する選択は、行ではなく文字として書き出す。** 記法(項目の `-`、
|
|
21
|
+
* 見出しの `#`)は行そのものを表すもので、その一部を写したものには付かない。付けると、
|
|
22
|
+
* 項目の一部をコピーしただけで独立した項目になる。
|
|
23
|
+
*/
|
|
24
|
+
export function serializeSlice(slice) {
|
|
25
|
+
const line = partialLine(slice);
|
|
26
|
+
if (line) {
|
|
27
|
+
// コードブロックは記法を持たない。書き出しのエスケープ(`\*` など)も入れずに素の文字を返す
|
|
28
|
+
return line.type.spec.code ? line.textContent : writeInline(line.content);
|
|
29
|
+
}
|
|
30
|
+
// スライスは単体では文書として成立しないため、内容検証を伴わないコンテナへ載せる
|
|
31
|
+
return serializeMarkdown(n.doc.create(null, wrapTableRows(slice.content)));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 選択が 1 つの行(テキストブロック)の中で完結しているなら、その行を返す。
|
|
35
|
+
*
|
|
36
|
+
* 両端が開いている深さ(`openStart` / `openEnd`)は、選択がどのコンテナの途中で始まり
|
|
37
|
+
* 終わったかを表す。**浅いほうの深さまで**コンテナをたどり、そこに行が 1 つだけあるなら、
|
|
38
|
+
* 選択はその行の中に収まっている。行をまたぐ選択では、たどる前に枝分かれするか、
|
|
39
|
+
* どちらかの端がコンテナの境目(深さ 0)で閉じているため、ここには入らない。
|
|
40
|
+
*/
|
|
41
|
+
function partialLine(slice) {
|
|
42
|
+
let content = slice.content;
|
|
43
|
+
let depth = Math.min(slice.openStart, slice.openEnd);
|
|
44
|
+
while (depth > 0 && content.childCount === 1) {
|
|
45
|
+
const child = content.firstChild;
|
|
46
|
+
if (child.isTextblock)
|
|
47
|
+
return child;
|
|
48
|
+
if (!child.isBlock)
|
|
49
|
+
return null;
|
|
50
|
+
content = child.content;
|
|
51
|
+
depth -= 1;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 行の並びを表として包む。
|
|
57
|
+
*
|
|
58
|
+
* セル範囲を選んでコピーすると、渡される断片は表ではなく**行の並び**になる。
|
|
59
|
+
* 包まずに書き出すと行を書き出す手段がなく例外で落ちるため、コピーしただけで
|
|
60
|
+
* エディタが壊れる。包んだ先頭の行が見出し行になり、配置指定はセルの属性から出る。
|
|
61
|
+
*/
|
|
62
|
+
function wrapTableRows(content) {
|
|
63
|
+
if (content.childCount === 0)
|
|
64
|
+
return content;
|
|
65
|
+
let rowsOnly = true;
|
|
66
|
+
content.forEach((child) => {
|
|
67
|
+
if (child.type !== n.table_row)
|
|
68
|
+
rowsOnly = false;
|
|
69
|
+
});
|
|
70
|
+
return rowsOnly ? Fragment.from(n.table.create(null, content)) : content;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* セクションの入れ子をたどり、見出しの並びへ戻す。
|
|
74
|
+
* 見出し以外のブロックは `MarkdownSerializer` に任せる。
|
|
75
|
+
*
|
|
76
|
+
* `depth` は `parent` 自身の構造上の深さ(`doc` は 0)。見出しのレベルはこの値になる。
|
|
77
|
+
*/
|
|
78
|
+
function writeSections(parent, depth) {
|
|
79
|
+
const parts = [];
|
|
80
|
+
let pendingBlocks = [];
|
|
81
|
+
const flush = () => {
|
|
82
|
+
if (pendingBlocks.length === 0)
|
|
83
|
+
return;
|
|
84
|
+
const chunk = writeBlocks(Fragment.fromArray(pendingBlocks));
|
|
85
|
+
if (chunk)
|
|
86
|
+
parts.push(chunk);
|
|
87
|
+
pendingBlocks = [];
|
|
88
|
+
};
|
|
89
|
+
parent.forEach((child) => {
|
|
90
|
+
/**
|
|
91
|
+
* フロントマターは区切りで囲んだ 1 つのブロックとして積む。中身が空なら何も積まない
|
|
92
|
+
* (フロントマターを持たない文書は、持たないまま書き出される)。
|
|
93
|
+
*
|
|
94
|
+
* 分岐を書き出しの入口ではなくここに置くのは、**コピーの経路
|
|
95
|
+
* (`serializeSlice`)が同じ関数を通る**ためである。
|
|
96
|
+
*/
|
|
97
|
+
if (child.type === n.front_matter) {
|
|
98
|
+
flush();
|
|
99
|
+
const content = child.textContent;
|
|
100
|
+
if (content)
|
|
101
|
+
parts.push(`---\n${content}\n---`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (child.type === n.section) {
|
|
105
|
+
flush();
|
|
106
|
+
parts.push(...writeSections(child, depth + 1));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (child.type === n.intro_section) {
|
|
110
|
+
flush();
|
|
111
|
+
// 内容が空行だけの先頭セクションは出力しない
|
|
112
|
+
// (出力すると、見出しから始まる文書を開いて保存するたびに先頭へ空行が増える)
|
|
113
|
+
if (!isBlank(child))
|
|
114
|
+
parts.push(...writeSections(child, depth));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (child.type === n.heading) {
|
|
118
|
+
flush();
|
|
119
|
+
const level = Math.min(depth, MAX_HEADING_LEVEL);
|
|
120
|
+
// タイトルが空の見出し(まだ書いていない見出し)では、記号の後ろに空白を残さない
|
|
121
|
+
const title = writeInline(child.content);
|
|
122
|
+
parts.push(title ? `${'#'.repeat(level)} ${title}` : '#'.repeat(level));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
pendingBlocks.push(child);
|
|
126
|
+
});
|
|
127
|
+
flush();
|
|
128
|
+
return parts;
|
|
129
|
+
}
|
|
130
|
+
function writeBlocks(content) {
|
|
131
|
+
return markdownSerializer
|
|
132
|
+
.serialize(n.doc.create(null, content), { tightLists: true })
|
|
133
|
+
.replace(/\n+$/, '');
|
|
134
|
+
}
|
|
135
|
+
function writeInline(content) {
|
|
136
|
+
return writeBlocks(Fragment.from(n.paragraph.create(null, content))).trim();
|
|
137
|
+
}
|
|
138
|
+
const serializeParagraph = (state, node) => {
|
|
139
|
+
state.renderInline(node);
|
|
140
|
+
state.closeBlock(node);
|
|
141
|
+
};
|
|
142
|
+
const serializeBlockquote = (state, node) => {
|
|
143
|
+
state.wrapBlock('> ', null, node, () => state.renderContent(node));
|
|
144
|
+
};
|
|
145
|
+
const serializeCodeBlock = (state, node) => {
|
|
146
|
+
writeFencedBlock(state, node, node.attrs.language ?? '');
|
|
147
|
+
};
|
|
148
|
+
// 図や表示状態は文書の内容ではないため、ソース以外を読まない
|
|
149
|
+
const serializeMermaidBlock = (state, node) => {
|
|
150
|
+
writeFencedBlock(state, node, 'mermaid');
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* 中身が改行で終わっているときは、閉じの記号の前に改行を足さない。
|
|
154
|
+
*
|
|
155
|
+
* 中身を書き終えた時点で、書き手はすでに次の行の頭に立っている。そこへもう 1 つ
|
|
156
|
+
* 改行を足すと、**字下げの中(項目の中のコードブロック)で「字下げだけの行」が挟まる**。
|
|
157
|
+
* その行は読み直したときに中身の空行として数えられ、往復のたびに行が 1 つずつ増える。
|
|
158
|
+
*
|
|
159
|
+
* 字下げの無い場所では、足しても足さなくても同じ文字になるため、書き出しは変わらない。
|
|
160
|
+
* 中身が空のときは 1 文字も書かない(字下げだけの行を作らないため)。
|
|
161
|
+
*/
|
|
162
|
+
function writeFencedBlock(state, node, language) {
|
|
163
|
+
const content = node.textContent;
|
|
164
|
+
const fence = longestFence(content);
|
|
165
|
+
state.write(`${fence}${language}\n`);
|
|
166
|
+
if (content)
|
|
167
|
+
state.text(content, false);
|
|
168
|
+
if (!content.endsWith('\n'))
|
|
169
|
+
state.ensureNewLine();
|
|
170
|
+
state.write(fence);
|
|
171
|
+
state.closeBlock(node);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 段落の中の改行。行末に `\` を置いて改行する(→ スキーマの `hard_break`)。
|
|
175
|
+
*
|
|
176
|
+
* **段落の末尾に続く改行は書かない。** 後ろに文字の無い `\` は改行にならず、
|
|
177
|
+
* 読み直すと `\` という文字だけが残る。書ける形にならないものは書かない。
|
|
178
|
+
*/
|
|
179
|
+
const serializeHardBreak = (state, node, parent, index) => {
|
|
180
|
+
for (let i = index + 1; i < parent.childCount; i += 1) {
|
|
181
|
+
if (parent.child(i).type !== node.type) {
|
|
182
|
+
state.write('\\\n');
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* 文字。**改行の次の行だけは、行頭の記号を自分でエスケープする。**
|
|
189
|
+
*
|
|
190
|
+
* `MarkdownSerializer` が行頭のエスケープを行うのはブロックの先頭だけで、段落内改行の
|
|
191
|
+
* 後ろは行頭であっても対象にならない(ライブラリ側に FIXME として残っている)。
|
|
192
|
+
* エスケープせずに書くと、`\` で改行した次の行が箇条書き・見出し・引用として読み直され、
|
|
193
|
+
* **段落がそこで分かれる**。
|
|
194
|
+
*
|
|
195
|
+
* エスケープの `\` は書式ではないため、文字として書かず `write` で直接出す
|
|
196
|
+
* (`text` を通すと `\` 自体がエスケープされ、`\\` になる)。
|
|
197
|
+
*/
|
|
198
|
+
const serializeText = (state, node, parent, index) => {
|
|
199
|
+
const value = node.text ?? '';
|
|
200
|
+
const afterBreak = index > 0 && parent.child(index - 1).type === n.hard_break;
|
|
201
|
+
// マークが付いていれば、行頭には記号ではなくマークの記法(`**` など)が来る
|
|
202
|
+
const escape = afterBreak && node.marks.length === 0 ? lineStartEscape(value) : null;
|
|
203
|
+
if (escape)
|
|
204
|
+
state.write(escape);
|
|
205
|
+
state.text(escape ? value.slice(escape.length - 1) : value);
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* 行頭で別のブロックの始まりとして読まれる記号をエスケープした、先頭部分。
|
|
209
|
+
* エスケープする必要が無ければ null。
|
|
210
|
+
*
|
|
211
|
+
* `*` や `_` のように行頭以外でも意味を持つ記号は、`MarkdownSerializer` が
|
|
212
|
+
* どこにあってもエスケープするため、ここでは扱わない。
|
|
213
|
+
*/
|
|
214
|
+
function lineStartEscape(text) {
|
|
215
|
+
// 番号付きの `1.` は数字をエスケープできないため、`\` は数字と `.` の間へ入れる
|
|
216
|
+
const ordered = /^\s*\d+(?=\.)/.exec(text);
|
|
217
|
+
if (ordered)
|
|
218
|
+
return `${ordered[0]}\\`;
|
|
219
|
+
const marker = /^(\s*)(?:[->]|\+(?=\s)|#{1,6}(?=\s|$))/.exec(text);
|
|
220
|
+
return marker ? `${marker[1]}\\` : null;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* 階層に加わらない見出し(→ スキーマの `nested_heading`)。レベルは属性から採る。
|
|
224
|
+
*
|
|
225
|
+
* `heading` と違い構造上の深さを見ないのは、この見出しが section の入れ子に
|
|
226
|
+
* 加わらず、**深さを合わせる相手がいない**ためである。中身が空のときに記号の
|
|
227
|
+
* 後ろへ空白を残さないのは、最上位の見出しと同じ扱いである。
|
|
228
|
+
*/
|
|
229
|
+
const serializeNestedHeading = (state, node) => {
|
|
230
|
+
const marker = '#'.repeat(Math.min(Number(node.attrs.level), MAX_HEADING_LEVEL));
|
|
231
|
+
state.write(node.content.size > 0 ? `${marker} ` : marker);
|
|
232
|
+
state.renderInline(node);
|
|
233
|
+
state.closeBlock(node);
|
|
234
|
+
};
|
|
235
|
+
const serializeHorizontalRule = (state, node) => {
|
|
236
|
+
state.write('---');
|
|
237
|
+
state.closeBlock(node);
|
|
238
|
+
};
|
|
239
|
+
const serializeUnknownBlock = (state, node) => {
|
|
240
|
+
// 原文をそのまま出す。エスケープも整形も行わない
|
|
241
|
+
state.write(String(node.attrs.source));
|
|
242
|
+
state.closeBlock(node);
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* 箇条書き。1 ノードが 1 項目にあたるため、字下げ幅は行頭の記号の幅から決まる。
|
|
246
|
+
* 番号は属性ではなく、同一階層で連続する兄弟の並びから振り直す。
|
|
247
|
+
*/
|
|
248
|
+
const serializeList = (state, node, parent, index) => {
|
|
249
|
+
// 項目ごとに 1 ノードのため、既定では項目の間に空行が入る。
|
|
250
|
+
// 直前が項目、または自分が項目の中の配下であれば、改行 1 つに詰める。
|
|
251
|
+
// ただし配下は、詰めた形が読み直せるときに限る(→ `readableAfterText`)。
|
|
252
|
+
const previous = index > 0 ? parent.child(index - 1) : null;
|
|
253
|
+
const tight = previous?.type === n.list ||
|
|
254
|
+
(parent.type === n.list && (!previous?.isTextblock || readableAfterText(node, parent, index)));
|
|
255
|
+
if (tight)
|
|
256
|
+
flushClose(state, 1);
|
|
257
|
+
const { text, width } = listPrefix(node, parent, index);
|
|
258
|
+
state.wrapBlock(' '.repeat(width), text, node, () => state.renderContent(node));
|
|
259
|
+
};
|
|
260
|
+
const serializeTable = (state, node) => {
|
|
261
|
+
const rows = [];
|
|
262
|
+
const aligns = [];
|
|
263
|
+
node.forEach((row, _offset, rowIndex) => {
|
|
264
|
+
const cells = [];
|
|
265
|
+
row.forEach((cell) => {
|
|
266
|
+
cells.push(escapeCell(writeInline(cell.content)));
|
|
267
|
+
if (rowIndex === 0)
|
|
268
|
+
aligns.push(cell.attrs.align);
|
|
269
|
+
});
|
|
270
|
+
rows.push(cells);
|
|
271
|
+
});
|
|
272
|
+
if (rows.length === 0) {
|
|
273
|
+
state.closeBlock(node);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const columnCount = Math.max(...rows.map((cells) => cells.length));
|
|
277
|
+
const line = (cells) => `| ${Array.from({ length: columnCount }, (_, i) => cells[i] ?? '').join(' | ')} |`;
|
|
278
|
+
state.write(line(rows[0]));
|
|
279
|
+
state.ensureNewLine();
|
|
280
|
+
state.write(`| ${Array.from({ length: columnCount }, (_, i) => delimiterFor(aligns[i] ?? null)).join(' | ')} |`);
|
|
281
|
+
for (const cells of rows.slice(1)) {
|
|
282
|
+
state.ensureNewLine();
|
|
283
|
+
state.write(line(cells));
|
|
284
|
+
}
|
|
285
|
+
state.closeBlock(node);
|
|
286
|
+
};
|
|
287
|
+
const blocks = {
|
|
288
|
+
paragraph: serializeParagraph,
|
|
289
|
+
nested_heading: serializeNestedHeading,
|
|
290
|
+
blockquote: serializeBlockquote,
|
|
291
|
+
code_block: serializeCodeBlock,
|
|
292
|
+
mermaid_block: serializeMermaidBlock,
|
|
293
|
+
horizontal_rule: serializeHorizontalRule,
|
|
294
|
+
unknown_block: serializeUnknownBlock,
|
|
295
|
+
list: serializeList,
|
|
296
|
+
table: serializeTable
|
|
297
|
+
};
|
|
298
|
+
const nodes = {
|
|
299
|
+
...Object.fromEntries(Object.entries(blocks).map(([name, serialize]) => [name, afterBlankLine(serialize)])),
|
|
300
|
+
hard_break: serializeHardBreak,
|
|
301
|
+
text: serializeText
|
|
302
|
+
};
|
|
303
|
+
const marks = {
|
|
304
|
+
strong: { open: '**', close: '**', mixable: true, expelEnclosingWhitespace: true },
|
|
305
|
+
em: { open: '*', close: '*', mixable: true, expelEnclosingWhitespace: true },
|
|
306
|
+
strikethrough: { open: '~~', close: '~~', mixable: true, expelEnclosingWhitespace: true },
|
|
307
|
+
code: {
|
|
308
|
+
open: (_state, _mark, parent, index) => backticksFor(parent.child(index), -1),
|
|
309
|
+
close: (_state, _mark, parent, index) => backticksFor(parent.child(index - 1), 1),
|
|
310
|
+
escape: false
|
|
311
|
+
},
|
|
312
|
+
link: {
|
|
313
|
+
open: '[',
|
|
314
|
+
close: (_state, mark) => {
|
|
315
|
+
const title = mark.attrs.title ? ` "${String(mark.attrs.title).replace(/"/g, '\\"')}"` : '';
|
|
316
|
+
return `](${String(mark.attrs.href ?? '')}${title})`;
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
/**
|
|
320
|
+
* 脚注のマーカー(→ スキーマの `footnote`)。**見た目だけのマークなので、何も書かない。**
|
|
321
|
+
* 記法そのもの(`[^1]`)は文字として持っており、エスケープしないのは `verbatim` の
|
|
322
|
+
* 仕事である。
|
|
323
|
+
*/
|
|
324
|
+
footnote: { open: '', close: '', mixable: true },
|
|
325
|
+
/**
|
|
326
|
+
* 受け皿の文字(→ スキーマの `verbatim`)。記法の原文なので、記号を 1 つも足さず
|
|
327
|
+
* 1 つも削らない。**エスケープの規則そのものは変えない**——普通の文章の `[` を
|
|
328
|
+
* エスケープするのは、文字として書かれた `[a](url)` がリンクとして読み直されるのを
|
|
329
|
+
* 防いでおり、正しい振る舞いである。
|
|
330
|
+
*/
|
|
331
|
+
verbatim: { open: '', close: '', escape: false }
|
|
332
|
+
};
|
|
333
|
+
const markdownSerializer = new MarkdownSerializer(nodes, marks);
|
|
334
|
+
// --- ヘルパー ---
|
|
335
|
+
function isBlank(node) {
|
|
336
|
+
if (node.textContent.trim() !== '')
|
|
337
|
+
return false;
|
|
338
|
+
let blank = true;
|
|
339
|
+
node.forEach((child) => {
|
|
340
|
+
if (child.type !== n.paragraph)
|
|
341
|
+
blank = false;
|
|
342
|
+
});
|
|
343
|
+
return blank;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* 記号だけの行に続くブロックを、空行を挟まずに書く。
|
|
347
|
+
*
|
|
348
|
+
* **記号だけの行の次に空行を置くと、その項目は中身を持てなくなる。** 配下は項目の
|
|
349
|
+
* 外へ出て並びの兄弟になり、字下げの深いところではコードブロックとして読まれる。
|
|
350
|
+
* 空の項目を入れ子にした形が、読み直すたびに平らになっていた。
|
|
351
|
+
*
|
|
352
|
+
* 空行を挟むのは、直前の文章を断ち切るためである(→ `readableAfterText`)。
|
|
353
|
+
* 1 行目に文字が無ければ断ち切る相手もいないので、詰めて書いてよい。
|
|
354
|
+
*/
|
|
355
|
+
function afterBlankLine(serialize) {
|
|
356
|
+
return (state, node, parent, index) => {
|
|
357
|
+
if (startsBlankLine(parent, index))
|
|
358
|
+
flushClose(state, 1);
|
|
359
|
+
serialize(state, node, parent, index);
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* その項目の 1 行目が、記号だけの行になるか。
|
|
364
|
+
*
|
|
365
|
+
* チェック項目は記号だけの行にならない。`[ ] ` は記号ではなく本文であり(GFM)、
|
|
366
|
+
* 中身が空でも 1 行目に文字が残る。
|
|
367
|
+
*/
|
|
368
|
+
function startsBlankLine(parent, index) {
|
|
369
|
+
if (parent.type !== n.list || index !== 1 || parent.attrs.kind === 'task')
|
|
370
|
+
return false;
|
|
371
|
+
return parent.firstChild?.content.size === 0;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* 直前のブロックとの間に入る空行を、改行 1 つに詰める。
|
|
375
|
+
* `MarkdownSerializerState` が持つが、型定義に現れないため形を与える。
|
|
376
|
+
*/
|
|
377
|
+
function flushClose(state, size) {
|
|
378
|
+
state.flushClose(size);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* 直前の文章の次の行に、空行を挟まずに置いても項目として読み直せるか。
|
|
382
|
+
*
|
|
383
|
+
* マークダウンでは、**中身が空の項目**と、**1 以外の番号で始まる項目**は、
|
|
384
|
+
* 直前の文章を断ち切れない。詰めて書くと、読み直したときに項目ではなく
|
|
385
|
+
* 文章の続きの文字として扱われ、配下がまるごと消える。空行を挟めば読める。
|
|
386
|
+
*/
|
|
387
|
+
function readableAfterText(node, parent, index) {
|
|
388
|
+
if (node.firstChild?.content.size === 0)
|
|
389
|
+
return false;
|
|
390
|
+
if (node.attrs.kind !== 'ordered')
|
|
391
|
+
return true;
|
|
392
|
+
return orderNumberOf(node, parent, index) === 1;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* 項目の行頭に書く文字列(`text`)と、2 行目以降の字下げ幅(`width`)。
|
|
396
|
+
*
|
|
397
|
+
* **字下げ幅は記号の幅であって、書き出す文字列の長さではない。** 番号付きは桁が増えれば
|
|
398
|
+
* 記号も広がるので幅は一致するが、チェック項目は一致しない。`- [x] ` のうち記号は `- `
|
|
399
|
+
* だけで、`[x] ` は本文の一部(GFM)である。文字列の長さ(6)で下げると、読み直したときに
|
|
400
|
+
* 深すぎる字下げとして扱われ、配下が本文の続きに化ける(`- [x] a` + ` - b`
|
|
401
|
+
* → `- [x] a - b`)。両者を 1 か所で組み立て、記号と本文を混ぜないことで防ぐ。
|
|
402
|
+
*/
|
|
403
|
+
function listPrefix(node, parent, index) {
|
|
404
|
+
const kind = node.attrs.kind;
|
|
405
|
+
const marker = kind === 'ordered' ? `${orderNumberOf(node, parent, index)}. ` : '- ';
|
|
406
|
+
const checkbox = kind === 'task' ? (node.attrs.checked ? '[x] ' : '[ ] ') : '';
|
|
407
|
+
return { text: marker + checkbox, width: marker.length };
|
|
408
|
+
}
|
|
409
|
+
/** 同一階層で連続する番号付き項目の並びから番号を決める */
|
|
410
|
+
function orderNumberOf(node, parent, index) {
|
|
411
|
+
let start = Number(node.attrs.order);
|
|
412
|
+
let offset = 0;
|
|
413
|
+
for (let i = index - 1; i >= 0; i -= 1) {
|
|
414
|
+
const sibling = parent.child(i);
|
|
415
|
+
if (sibling.type !== n.list || sibling.attrs.kind !== 'ordered')
|
|
416
|
+
break;
|
|
417
|
+
offset += 1;
|
|
418
|
+
start = Number(sibling.attrs.order);
|
|
419
|
+
}
|
|
420
|
+
return (Number.isFinite(start) && start > 0 ? start : 1) + offset;
|
|
421
|
+
}
|
|
422
|
+
function delimiterFor(align) {
|
|
423
|
+
if (align === 'left')
|
|
424
|
+
return ':--';
|
|
425
|
+
if (align === 'center')
|
|
426
|
+
return ':-:';
|
|
427
|
+
if (align === 'right')
|
|
428
|
+
return '--:';
|
|
429
|
+
return '---';
|
|
430
|
+
}
|
|
431
|
+
function escapeCell(text) {
|
|
432
|
+
return text.replace(/\|/g, '\\|').replace(/\n/g, ' ');
|
|
433
|
+
}
|
|
434
|
+
function longestFence(text) {
|
|
435
|
+
const runs = text.match(/`{3,}/g) ?? [];
|
|
436
|
+
const longest = runs.reduce((max, run) => Math.max(max, run.length), 2);
|
|
437
|
+
return '`'.repeat(longest + 1);
|
|
438
|
+
}
|
|
439
|
+
function backticksFor(node, side) {
|
|
440
|
+
const ticks = /`+/g;
|
|
441
|
+
let len = 0;
|
|
442
|
+
if (node?.isText && node.text) {
|
|
443
|
+
let match;
|
|
444
|
+
while ((match = ticks.exec(node.text)))
|
|
445
|
+
len = Math.max(len, match[0].length);
|
|
446
|
+
}
|
|
447
|
+
let result = len > 0 && side > 0 ? ' `' : '`';
|
|
448
|
+
for (let i = 0; i < len; i += 1)
|
|
449
|
+
result += '`';
|
|
450
|
+
if (len > 0 && side < 0)
|
|
451
|
+
result += ' ';
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { SectionPath } from '../document/document.types';
|
|
2
|
+
/**
|
|
3
|
+
* 保存された表示状態が指すセクション。
|
|
4
|
+
*
|
|
5
|
+
* パスだけでは足りない。保存と復元のあいだに文書は別の経路で編集されうるため、
|
|
6
|
+
* 同じパスが別のセクションを指しうる(→ `viewStateCodec.ts` の突合キー)。
|
|
7
|
+
*/
|
|
8
|
+
export interface ViewSectionRef {
|
|
9
|
+
readonly path: readonly number[];
|
|
10
|
+
/** 根からの見出しの連なりから導出したハッシュ。元の文字列は復元できない */
|
|
11
|
+
readonly hash: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 利用者が保存・復元できる、JSON 互換の表示状態。
|
|
15
|
+
*
|
|
16
|
+
* 本文(テキスト内容)を含めない。リスト項目の折りたたみも含めない
|
|
17
|
+
* (安定した突合キーを作れないため)。
|
|
18
|
+
*/
|
|
19
|
+
export interface ViewState {
|
|
20
|
+
readonly version: 1;
|
|
21
|
+
readonly folded: readonly ViewSectionRef[];
|
|
22
|
+
readonly focused: ViewSectionRef | null;
|
|
23
|
+
}
|
|
24
|
+
export type ViewStateError = {
|
|
25
|
+
readonly kind: 'invalid-format';
|
|
26
|
+
readonly detail: string;
|
|
27
|
+
} | {
|
|
28
|
+
readonly kind: 'unsupported-version';
|
|
29
|
+
readonly version: number;
|
|
30
|
+
};
|
|
31
|
+
export interface ResolvedViewState {
|
|
32
|
+
readonly foldedPaths: readonly SectionPath[];
|
|
33
|
+
readonly focusedPath: SectionPath | null;
|
|
34
|
+
/** 突合キーが一致せず破棄した項目の数。一致する相手を他の場所へ探しに行かない */
|
|
35
|
+
readonly discardedCount: number;
|
|
36
|
+
}
|
|
37
|
+
export declare function isViewSectionRef(value: unknown): value is ViewSectionRef;
|
|
38
|
+
export declare function isViewState(value: unknown): value is ViewState;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function isViewSectionRef(value) {
|
|
2
|
+
if (typeof value !== 'object' || value === null)
|
|
3
|
+
return false;
|
|
4
|
+
const candidate = value;
|
|
5
|
+
return (Array.isArray(candidate.path) &&
|
|
6
|
+
candidate.path.every((entry) => typeof entry === 'number' && Number.isInteger(entry) && entry >= 0) &&
|
|
7
|
+
typeof candidate.hash === 'string');
|
|
8
|
+
}
|
|
9
|
+
export function isViewState(value) {
|
|
10
|
+
if (typeof value !== 'object' || value === null)
|
|
11
|
+
return false;
|
|
12
|
+
const candidate = value;
|
|
13
|
+
return (candidate.version === 1 &&
|
|
14
|
+
Array.isArray(candidate.folded) &&
|
|
15
|
+
candidate.folded.every(isViewSectionRef) &&
|
|
16
|
+
(candidate.focused === null || isViewSectionRef(candidate.focused)));
|
|
17
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Node } from 'prosemirror-model';
|
|
2
|
+
import type { EditorState } from 'prosemirror-state';
|
|
3
|
+
import type { Result } from '../../utils/result.types';
|
|
4
|
+
import type { SectionPath } from '../document/document.types';
|
|
5
|
+
import { type ResolvedViewState, type ViewState, type ViewStateError } from './viewState.types';
|
|
6
|
+
/** 形式の版。将来の変更で増やし、古い側は「未対応」として安全に無視できるようにする */
|
|
7
|
+
export declare const VIEW_STATE_VERSION = 1;
|
|
8
|
+
/** 何も畳んでおらず、フォーカスもしていない状態 */
|
|
9
|
+
export declare const EMPTY_VIEW_STATE: ViewState;
|
|
10
|
+
export interface ViewStateCodec {
|
|
11
|
+
/**
|
|
12
|
+
* 現在の表示状態を、そのまま JSON にできる値へ変換する。
|
|
13
|
+
*
|
|
14
|
+
* フォーカスの位置は引数で受け取る。フォーカスの真実はプラグインが持っており、
|
|
15
|
+
* ここから覗きに行くと、変換の道具が編集の仕組みに依存してしまう。
|
|
16
|
+
*/
|
|
17
|
+
encode(state: EditorState, focusedPath: SectionPath | null): ViewState;
|
|
18
|
+
/**
|
|
19
|
+
* 外から渡された値を検証し、いまの文書へ当てられる形にする。
|
|
20
|
+
* **失敗を例外にしない。** 利用者のアプリを巻き込んで落とさないためである。
|
|
21
|
+
*/
|
|
22
|
+
decode(value: unknown, doc: Node): Result<ResolvedViewState, ViewStateError>;
|
|
23
|
+
}
|
|
24
|
+
export declare const viewStateCodec: ViewStateCodec;
|
|
25
|
+
/**
|
|
26
|
+
* 2 つの表示状態が同じ内容かを見る。
|
|
27
|
+
*
|
|
28
|
+
* **参照ではなく内容で比べる。** 外から設定された値と現在の状態を突き合わせるとき、
|
|
29
|
+
* 参照で比べると毎回「違う」と判定され、復元処理が止まらなくなる。
|
|
30
|
+
*/
|
|
31
|
+
export declare function isSameViewState(a: ViewState | undefined, b: ViewState | undefined): boolean;
|