@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.
Files changed (153) hide show
  1. package/README.ja.md +190 -0
  2. package/README.md +190 -0
  3. package/dist/assets/styles/editor.css +1459 -0
  4. package/dist/assets/styles/theme.css +486 -0
  5. package/dist/components/ContextMenu.svelte +279 -0
  6. package/dist/components/ContextMenu.svelte.d.ts +9 -0
  7. package/dist/components/EditorSurface.svelte +99 -0
  8. package/dist/components/EditorSurface.svelte.d.ts +16 -0
  9. package/dist/components/FocusBreadcrumb.svelte +138 -0
  10. package/dist/components/FocusBreadcrumb.svelte.d.ts +16 -0
  11. package/dist/components/MarkdownEditor.svelte +393 -0
  12. package/dist/components/MarkdownEditor.svelte.d.ts +119 -0
  13. package/dist/components/ShortcutHelpDialog.svelte +155 -0
  14. package/dist/components/ShortcutHelpDialog.svelte.d.ts +12 -0
  15. package/dist/components/internal/Dialog.svelte +157 -0
  16. package/dist/components/internal/Dialog.svelte.d.ts +23 -0
  17. package/dist/components/internal/Modal.svelte +181 -0
  18. package/dist/components/internal/Modal.svelte.d.ts +18 -0
  19. package/dist/components/internal/Popup.svelte +248 -0
  20. package/dist/components/internal/Popup.svelte.d.ts +26 -0
  21. package/dist/index.d.ts +32 -0
  22. package/dist/index.js +28 -0
  23. package/dist/markdown.d.ts +13 -0
  24. package/dist/markdown.js +10 -0
  25. package/dist/messages/en.d.ts +2 -0
  26. package/dist/messages/en.js +107 -0
  27. package/dist/messages/index.d.ts +13 -0
  28. package/dist/messages/index.js +31 -0
  29. package/dist/messages/ja.d.ts +2 -0
  30. package/dist/messages/ja.js +112 -0
  31. package/dist/messages/messages.types.d.ts +59 -0
  32. package/dist/messages/messages.types.js +1 -0
  33. package/dist/models/document/bodySelection.d.ts +32 -0
  34. package/dist/models/document/bodySelection.js +43 -0
  35. package/dist/models/document/document.types.d.ts +27 -0
  36. package/dist/models/document/document.types.js +8 -0
  37. package/dist/models/document/foldable.d.ts +18 -0
  38. package/dist/models/document/foldable.js +21 -0
  39. package/dist/models/document/schema.d.ts +2 -0
  40. package/dist/models/document/schema.js +428 -0
  41. package/dist/models/document/sectionTree.d.ts +15 -0
  42. package/dist/models/document/sectionTree.js +123 -0
  43. package/dist/models/markdown/blockOutline.d.ts +9 -0
  44. package/dist/models/markdown/blockOutline.js +82 -0
  45. package/dist/models/markdown/frontMatter.d.ts +98 -0
  46. package/dist/models/markdown/frontMatter.js +300 -0
  47. package/dist/models/markdown/markdown.types.d.ts +60 -0
  48. package/dist/models/markdown/markdown.types.js +1 -0
  49. package/dist/models/markdown/markdownIt.d.ts +1 -0
  50. package/dist/models/markdown/markdownIt.js +92 -0
  51. package/dist/models/markdown/parser.d.ts +4 -0
  52. package/dist/models/markdown/parser.js +505 -0
  53. package/dist/models/markdown/plainText.d.ts +1 -0
  54. package/dist/models/markdown/plainText.js +25 -0
  55. package/dist/models/markdown/serializer.d.ts +17 -0
  56. package/dist/models/markdown/serializer.js +453 -0
  57. package/dist/models/viewState/viewState.types.d.ts +38 -0
  58. package/dist/models/viewState/viewState.types.js +17 -0
  59. package/dist/models/viewState/viewStateCodec.d.ts +31 -0
  60. package/dist/models/viewState/viewStateCodec.js +173 -0
  61. package/dist/operations/autoformat.d.ts +9 -0
  62. package/dist/operations/autoformat.js +86 -0
  63. package/dist/operations/commands/block.d.ts +24 -0
  64. package/dist/operations/commands/block.js +139 -0
  65. package/dist/operations/commands/codeBlock.d.ts +7 -0
  66. package/dist/operations/commands/codeBlock.js +26 -0
  67. package/dist/operations/commands/element.d.ts +67 -0
  68. package/dist/operations/commands/element.js +269 -0
  69. package/dist/operations/commands/frontMatter.d.ts +35 -0
  70. package/dist/operations/commands/frontMatter.js +120 -0
  71. package/dist/operations/commands/lineBreak.d.ts +9 -0
  72. package/dist/operations/commands/lineBreak.js +16 -0
  73. package/dist/operations/commands/link.d.ts +7 -0
  74. package/dist/operations/commands/link.js +21 -0
  75. package/dist/operations/commands/list.d.ts +126 -0
  76. package/dist/operations/commands/list.js +652 -0
  77. package/dist/operations/commands/mermaidBlock.d.ts +9 -0
  78. package/dist/operations/commands/mermaidBlock.js +28 -0
  79. package/dist/operations/commands/section.d.ts +217 -0
  80. package/dist/operations/commands/section.js +865 -0
  81. package/dist/operations/commands/sectionFold.d.ts +36 -0
  82. package/dist/operations/commands/sectionFold.js +219 -0
  83. package/dist/operations/commands/sectionJump.d.ts +8 -0
  84. package/dist/operations/commands/sectionJump.js +144 -0
  85. package/dist/operations/commands/selectedBlock.d.ts +37 -0
  86. package/dist/operations/commands/selectedBlock.js +170 -0
  87. package/dist/operations/commands/table.d.ts +64 -0
  88. package/dist/operations/commands/table.js +437 -0
  89. package/dist/operations/keymap/assemble.d.ts +22 -0
  90. package/dist/operations/keymap/assemble.js +165 -0
  91. package/dist/operations/keymap/coreActions.d.ts +2 -0
  92. package/dist/operations/keymap/coreActions.js +711 -0
  93. package/dist/operations/keymap/menuItems.d.ts +52 -0
  94. package/dist/operations/keymap/menuItems.js +114 -0
  95. package/dist/operations/keymap/shortcutKeys.d.ts +42 -0
  96. package/dist/operations/keymap/shortcutKeys.js +114 -0
  97. package/dist/operations/keymap/viewActions.d.ts +30 -0
  98. package/dist/operations/keymap/viewActions.js +182 -0
  99. package/dist/operations/operations.types.d.ts +121 -0
  100. package/dist/operations/operations.types.js +1 -0
  101. package/dist/operations/plugins/blockHandle.d.ts +25 -0
  102. package/dist/operations/plugins/blockHandle.js +112 -0
  103. package/dist/operations/plugins/contextMenu.d.ts +33 -0
  104. package/dist/operations/plugins/contextMenu.js +204 -0
  105. package/dist/operations/plugins/focus.d.ts +39 -0
  106. package/dist/operations/plugins/focus.js +350 -0
  107. package/dist/operations/plugins/foldClick.d.ts +11 -0
  108. package/dist/operations/plugins/foldClick.js +117 -0
  109. package/dist/operations/plugins/foldGuard.d.ts +15 -0
  110. package/dist/operations/plugins/foldGuard.js +31 -0
  111. package/dist/operations/plugins/frontMatterGuard.d.ts +21 -0
  112. package/dist/operations/plugins/frontMatterGuard.js +150 -0
  113. package/dist/operations/plugins/frontMatterView.d.ts +34 -0
  114. package/dist/operations/plugins/frontMatterView.js +137 -0
  115. package/dist/operations/plugins/linkPaste.d.ts +3 -0
  116. package/dist/operations/plugins/linkPaste.js +20 -0
  117. package/dist/operations/plugins/listStructure.d.ts +13 -0
  118. package/dist/operations/plugins/listStructure.js +18 -0
  119. package/dist/operations/plugins/markdownClipboard.d.ts +8 -0
  120. package/dist/operations/plugins/markdownClipboard.js +208 -0
  121. package/dist/operations/plugins/mermaidView.d.ts +20 -0
  122. package/dist/operations/plugins/mermaidView.js +206 -0
  123. package/dist/operations/plugins/requiredSelectionEntry.d.ts +10 -0
  124. package/dist/operations/plugins/requiredSelectionEntry.js +124 -0
  125. package/dist/operations/plugins/sectionView.d.ts +12 -0
  126. package/dist/operations/plugins/sectionView.js +44 -0
  127. package/dist/operations/plugins/tabEscape.d.ts +21 -0
  128. package/dist/operations/plugins/tabEscape.js +41 -0
  129. package/dist/operations/plugins/tableHandles.d.ts +24 -0
  130. package/dist/operations/plugins/tableHandles.js +89 -0
  131. package/dist/operations/transaction.d.ts +26 -0
  132. package/dist/operations/transaction.js +51 -0
  133. package/dist/operations/transactionOrigin.d.ts +17 -0
  134. package/dist/operations/transactionOrigin.js +9 -0
  135. package/dist/states/contextMenuController.svelte.d.ts +57 -0
  136. package/dist/states/contextMenuController.svelte.js +206 -0
  137. package/dist/states/editorSession.d.ts +12 -0
  138. package/dist/states/editorSession.js +268 -0
  139. package/dist/states/editorSession.types.d.ts +57 -0
  140. package/dist/states/editorSession.types.js +1 -0
  141. package/dist/states/outlineViewController.svelte.d.ts +52 -0
  142. package/dist/states/outlineViewController.svelte.js +134 -0
  143. package/dist/utils/dateText.d.ts +25 -0
  144. package/dist/utils/dateText.js +71 -0
  145. package/dist/utils/geometry.types.d.ts +13 -0
  146. package/dist/utils/geometry.types.js +1 -0
  147. package/dist/utils/json.types.d.ts +4 -0
  148. package/dist/utils/json.types.js +1 -0
  149. package/dist/utils/platform.d.ts +8 -0
  150. package/dist/utils/platform.js +10 -0
  151. package/dist/utils/result.types.d.ts +7 -0
  152. package/dist/utils/result.types.js +1 -0
  153. package/package.json +109 -0
@@ -0,0 +1,428 @@
1
+ import { Schema } from 'prosemirror-model';
2
+ import { createListSpec, defaultAttributesGetter, listToDOM } from 'prosemirror-flat-list';
3
+ import { tableNodes } from 'prosemirror-tables';
4
+ import { foldStateAttrs } from './foldable';
5
+ import { MAX_HEADING_LEVEL, MIN_HEADING_LEVEL } from './document.types';
6
+ /**
7
+ * `requiredSelectionBlock` は**要選択ブロックのフラグ**である。中にカーソルを置いても
8
+ * 階層操作や `Tab` が中身に対して働いてしまい、ブロック自身を指せないものに付ける
9
+ * (表・引用・コードブロック)。段落・見出し・箇条書きの項目には付けない。
10
+ *
11
+ * 種別の一覧を別のファイルに置かないのは、ノードを増やしたときに直し忘れるためである。
12
+ * ノードの定義と印が同じ場所にあれば、増やすときに必ず目に入る。
13
+ *
14
+ * 印は描画にも読み書きにも現れない。`toDOM` / `parseDOM` とマークダウンの入出力は
15
+ * 印の有無で変わらない。
16
+ */
17
+ /**
18
+ * 見出しどうしが作る階層を入れ子の `section` として表す。
19
+ *
20
+ * - `doc` は先頭にフロントマターを 1 つ持ち、その後ろに必ず 1 つ以上の本文を持つ
21
+ * - 見出しを持たないセクション(`intro_section`)は本文の先頭に 1 つだけ置ける
22
+ * - `section` の中身はブロックが先、子セクションが後(Markdown で書き戻せる並びに限る)
23
+ * - 表のセルはインラインのみを含み、ブロック要素を構造的に排除する
24
+ */
25
+ const structureNodes = {
26
+ /**
27
+ * 文書のいちばん外側には、文字の入らないカーソルの置き場所を作らせない。
28
+ *
29
+ * 既定の判定は「次に来る既定の種別がテキストブロックなら置いてよい」であり、先頭に
30
+ * 来るフロントマターがテキストブロックであるため、**その手前に置き場所ができる**。
31
+ * そこは何も置けない場所(先頭はフロントマターと決まっている)なので、上キーで
32
+ * 抜けたきり何も書けない行き止まりになる。
33
+ *
34
+ * 本文どうしのあいだ(セクションの前後)は、もともと既定の判定で表現を持たない。
35
+ */
36
+ doc: {
37
+ content: 'front_matter (intro_section section* | section+)',
38
+ allowGapCursor: false
39
+ },
40
+ /**
41
+ * 文書の先頭に置かれるフロントマター。中身は解釈しないテキストとして持つ。
42
+ *
43
+ * **省略できない形にする。** 無くなると書き足す入口が失われ、一度メタデータを
44
+ * 持たなくなった文書には二度と書けなくなる。中身が空のときは Markdown へ書き出さない
45
+ * ため、「常にある」ことが出力に現れることはない。
46
+ *
47
+ * **`requiredSelectionBlock` を付けない。** 付けると `targetElement` が拾い、複製が
48
+ * フロントマターを 2 つ持つ文書を作ろうとし、移動が本文の下へ動かせてしまう。
49
+ * 付けなければ `topLevelBlock` が `doc` 直下を対象にしないため、移動・複製・削除・
50
+ * 階層操作がいずれも自然に不発になる。
51
+ *
52
+ * `group: 'block'` にも入れない(本文の中に置ける種別にしない)。
53
+ *
54
+ * parseDOM の規則の競合は並び順(スキーマのノード順)で決まり、`code_block` の
55
+ * 汎用 `pre` 規則が先に並ぶため、`priority` を上げて先に評価させる。
56
+ */
57
+ front_matter: {
58
+ content: 'text*',
59
+ code: true,
60
+ defining: true,
61
+ /**
62
+ * 本文と繋がらない。付けないと、本文の先頭で `Backspace` を押したときに
63
+ * 編集基盤の既定の結合が働き、**本文の 1 行目がメタデータの中へ吸い込まれる**。
64
+ */
65
+ isolating: true,
66
+ marks: '',
67
+ toDOM: () => [
68
+ 'pre',
69
+ { class: 'markdown-editor__front-matter', 'data-front-matter': 'true' },
70
+ ['code', 0]
71
+ ],
72
+ parseDOM: [{ tag: 'pre[data-front-matter]', preserveWhitespace: 'full', priority: 60 }]
73
+ },
74
+ intro_section: {
75
+ content: 'block+',
76
+ defining: true,
77
+ toDOM: () => ['div', { class: 'markdown-editor__intro-section' }, 0],
78
+ parseDOM: [{ tag: 'div.markdown-editor__intro-section' }]
79
+ },
80
+ /**
81
+ * `collapsed` は「今どう見ているか」であって「何が書かれているか」ではない。
82
+ * 属性として持つのは、移動・複製・削除・取り消しにノードごと追随させるためである
83
+ * (書き出しには現れない。シリアライザは属性を読まない)。
84
+ *
85
+ * 見出しを持たないセクションは折りたたみの対象にしないため、属性を持たせない。
86
+ * 箇条書きは `prosemirror-flat-list` が同名の属性を既に持つ。
87
+ */
88
+ section: {
89
+ content: 'heading block* section*',
90
+ defining: true,
91
+ attrs: { collapsed: { default: false } },
92
+ toDOM: (node) => [
93
+ 'section',
94
+ {
95
+ class: 'markdown-editor__section',
96
+ 'data-collapsed': node.attrs.collapsed ? 'true' : undefined,
97
+ ...foldStateAttrs(node)
98
+ },
99
+ 0
100
+ ],
101
+ parseDOM: [
102
+ {
103
+ tag: 'section.markdown-editor__section',
104
+ getAttrs: (element) => ({
105
+ collapsed: element.getAttribute('data-collapsed') === 'true'
106
+ })
107
+ }
108
+ ]
109
+ }
110
+ };
111
+ /**
112
+ * 箇条書きの項目。一覧ライブラリの仕様をそのまま使い、描画にだけ開閉状態を足す。
113
+ *
114
+ * ライブラリが出す `data-list-collapsable` は「畳める」しか表さず、開いているか
115
+ * 閉じているかを伝えない。押せる記号を出しながら状態を伝えなければ、画面読み上げ
116
+ * ソフトの利用者にだけ使えない操作が増える。
117
+ */
118
+ function listSpec() {
119
+ const spec = createListSpec();
120
+ return {
121
+ ...spec,
122
+ toDOM: (node) => listToDOM({
123
+ node,
124
+ getAttributes: (target) => ({
125
+ ...defaultAttributesGetter(target),
126
+ ...foldStateAttrs(target)
127
+ })
128
+ })
129
+ };
130
+ }
131
+ const blockNodes = {
132
+ // `block` グループの先頭に置く(空のコンテナを埋めるときの既定の種別になる)
133
+ paragraph: {
134
+ content: 'inline*',
135
+ group: 'block',
136
+ toDOM: () => ['p', { class: 'markdown-editor__paragraph' }, 0],
137
+ parseDOM: [{ tag: 'p' }]
138
+ },
139
+ /**
140
+ * `block` グループに入れない。見出しは section の先頭にしか置けない。
141
+ *
142
+ * 中身を `text*` に絞るのは、**見出しに段落内改行を置かせない**ためである。
143
+ * ATX 見出し(`# ...`)はマークダウンでは 1 行のブロックで、行末の空白 2 つも `\` も
144
+ * 改行にならない。書ける手段は生の `<br>` だけであり、このエディタはインラインの HTML を
145
+ * 解釈せず文字として残すため、往復で `<br>` という文字列に化ける。
146
+ * 見出しの文字列はパンくず・折りたたみの見出しとしても 1 行で出る(→ `hard_break`)。
147
+ */
148
+ heading: {
149
+ content: 'text*',
150
+ defining: true,
151
+ attrs: { level: { default: MIN_HEADING_LEVEL } },
152
+ toDOM: (node) => [
153
+ `h${node.attrs.level}`,
154
+ { class: `markdown-editor__heading markdown-editor__heading--level-${node.attrs.level}` },
155
+ 0
156
+ ],
157
+ parseDOM: Array.from({ length: MAX_HEADING_LEVEL }, (_, index) => ({
158
+ tag: `h${index + 1}`,
159
+ attrs: { level: index + 1 }
160
+ }))
161
+ },
162
+ /**
163
+ * 引用や箇条書きの項目の中に書かれた見出し。**文書の階層には加わらない。**
164
+ *
165
+ * マークダウンでは、引用や項目の中の見出しは後続の最上位のブロックを従えないため、
166
+ * 見出しどうしが作る階層には入らない。それでも見出しとして書かれている以上、
167
+ * 記号は残らなければならない(記号を落として中身だけを段落にすると、開いて保存する
168
+ * だけで見出しが本文になる)。
169
+ *
170
+ * **`heading` を `group: 'block'` へ入れる形は採らない。** `section` の中身が
171
+ * `heading block* section*` であるため、`block*` の側にも見出しがマッチし、
172
+ * **見出しを 2 つ持つ section** が構造として作れてしまう。階層を section の入れ子で
173
+ * 表すというこのスキーマの中心が緩む。
174
+ *
175
+ * `level` は**原文のレベル**をそのまま持つ。`heading` と違い構造上の深さから
176
+ * 決め直さないのは、加わる階層が無く決め直す相手がいないためである。
177
+ *
178
+ * 中身を `text*` に絞る理由は `heading` と同じ(マークダウンでは 1 行のブロックで、
179
+ * 改行を書く手段が無い)。
180
+ *
181
+ * parseDOM の規則の競合は並び順(スキーマのノード順)で決まり、`heading` の汎用
182
+ * `h1`〜`h6` 規則が先に並ぶため、`priority` を上げて先に評価させる。
183
+ */
184
+ nested_heading: {
185
+ content: 'text*',
186
+ group: 'block',
187
+ defining: true,
188
+ attrs: { level: { default: MIN_HEADING_LEVEL } },
189
+ toDOM: (node) => [
190
+ `h${node.attrs.level}`,
191
+ {
192
+ class: `markdown-editor__nested-heading markdown-editor__nested-heading--level-${node.attrs.level}`,
193
+ 'data-nested-heading': 'true'
194
+ },
195
+ 0
196
+ ],
197
+ parseDOM: Array.from({ length: MAX_HEADING_LEVEL }, (_, index) => ({
198
+ tag: `h${index + 1}[data-nested-heading]`,
199
+ attrs: { level: index + 1 },
200
+ priority: 60
201
+ }))
202
+ },
203
+ blockquote: {
204
+ content: 'block+',
205
+ group: 'block',
206
+ defining: true,
207
+ requiredSelectionBlock: true,
208
+ toDOM: () => ['blockquote', { class: 'markdown-editor__blockquote' }, 0],
209
+ parseDOM: [{ tag: 'blockquote' }]
210
+ },
211
+ code_block: {
212
+ content: 'text*',
213
+ group: 'block',
214
+ code: true,
215
+ defining: true,
216
+ requiredSelectionBlock: true,
217
+ marks: '',
218
+ attrs: { language: { default: '' } },
219
+ toDOM: (node) => [
220
+ 'pre',
221
+ { class: 'markdown-editor__code-block', 'data-language': node.attrs.language || null },
222
+ ['code', 0]
223
+ ],
224
+ parseDOM: [
225
+ {
226
+ tag: 'pre',
227
+ preserveWhitespace: 'full',
228
+ getAttrs: (element) => ({
229
+ language: element.getAttribute('data-language') ?? ''
230
+ })
231
+ }
232
+ ]
233
+ },
234
+ /**
235
+ * Mermaid 記法のソースを中身として持つブロック。図・エラー・読み込み状態は
236
+ * 「今どう見えているか」であり、文書には現れない(内容はソースだけ)。
237
+ *
238
+ * parseDOM の規則の競合は並び順(スキーマのノード順)で決まり、`code_block` の
239
+ * 汎用 `pre` 規則が先に並ぶため、`priority` を上げて先に評価させる。
240
+ */
241
+ mermaid_block: {
242
+ content: 'text*',
243
+ group: 'block',
244
+ code: true,
245
+ defining: true,
246
+ requiredSelectionBlock: true,
247
+ marks: '',
248
+ toDOM: () => [
249
+ 'pre',
250
+ { class: 'markdown-editor__mermaid-block', 'data-mermaid-block': 'true' },
251
+ ['code', 0]
252
+ ],
253
+ parseDOM: [{ tag: 'pre[data-mermaid-block]', preserveWhitespace: 'full', priority: 60 }]
254
+ },
255
+ horizontal_rule: {
256
+ group: 'block',
257
+ toDOM: () => ['hr', { class: 'markdown-editor__horizontal-rule' }],
258
+ parseDOM: [{ tag: 'hr' }]
259
+ },
260
+ /**
261
+ * 対応していない記法を原文のまま保持するコンテナ。
262
+ * 中身を解釈しないため atom とし、原文は文字として描画する(HTML として実行しない)。
263
+ */
264
+ unknown_block: {
265
+ group: 'block',
266
+ atom: true,
267
+ selectable: true,
268
+ attrs: { source: { default: '' } },
269
+ toDOM: (node) => [
270
+ 'pre',
271
+ { class: 'markdown-editor__unknown-block', 'data-unknown-block': 'true' },
272
+ String(node.attrs.source)
273
+ ],
274
+ parseDOM: [
275
+ {
276
+ tag: 'pre[data-unknown-block]',
277
+ preserveWhitespace: 'full',
278
+ // `code_block` の汎用 `pre` 規則が定義順で先に並ぶため、上げないと届かない
279
+ priority: 60,
280
+ getAttrs: (element) => ({ source: element.textContent ?? '' })
281
+ }
282
+ ]
283
+ },
284
+ list: listSpec(),
285
+ /**
286
+ * 段落の中の改行。書き出しは行末の `\` で、読み込みは `hardbreak` から戻る。
287
+ *
288
+ * 行末の空白 2 つを使わないのは、整形ツールやエディタの「行末の空白を削る」設定で
289
+ * 消え、差分にも現れないためである。改行が黙って失われる書き方は選ばない。
290
+ *
291
+ * 置ける場所は**インラインを持つブロックのうち、`content` が `inline*` のもの**、
292
+ * つまり段落だけである。見出しと表のセルは `text*` に絞ってあり、構造として入らない
293
+ * (どちらもマークダウンでは 1 行のブロックで、`<br>` 以外に書く手段が無い)。
294
+ */
295
+ hard_break: {
296
+ inline: true,
297
+ group: 'inline',
298
+ selectable: false,
299
+ // 描き方を変える余地が無いため、印の付いた class を持たせない
300
+ toDOM: () => ['br'],
301
+ parseDOM: [{ tag: 'br' }]
302
+ },
303
+ text: { group: 'inline' }
304
+ };
305
+ const tableNodeSpecs = tableNodes({
306
+ tableGroup: 'block',
307
+ // セル内は文字のみ。ブロック要素も段落内改行もスキーマ段階で拒否する
308
+ // (パイプ表のセルは 1 行で、改行を `<br>` 以外に書けない → `hard_break`)
309
+ cellContent: 'text*',
310
+ cellAttributes: {
311
+ // 列の配置指定。マークダウンの `|:--|--:|` と往復させるために保持する
312
+ align: {
313
+ default: null,
314
+ getFromDOM: (element) => element.style.textAlign || null,
315
+ setDOMAttr: (value, attrs) => {
316
+ if (value)
317
+ attrs.style = `${attrs.style ?? ''}text-align: ${value};`;
318
+ }
319
+ }
320
+ }
321
+ });
322
+ /**
323
+ * 行の中に、文字の入らないカーソルの置き場所を作らせない。
324
+ *
325
+ * セルはインラインだけを入れる場所(テキストブロック)であるため、既定の判定では
326
+ * 「セルとセルの間には文字を打てるコンテナを作れる」と見なされ、行の中に置き場所ができる。
327
+ * その位置を描く要素が `<tr>` の直下へ入ると、ブラウザはそれを名前のないセルとして
328
+ * 数え、その行だけ列がずれて描かれる。
329
+ *
330
+ * 表そのもの・セルの中は既定の判定で既に表現を持たない。**行の中だけが穴だった。**
331
+ * 表の前後(段落を作れる位置)は親が `section` / `intro_section` なので影響しない。
332
+ */
333
+ const tableSpec = {
334
+ ...tableNodeSpecs,
335
+ table: { ...tableNodeSpecs.table, requiredSelectionBlock: true },
336
+ table_row: { ...tableNodeSpecs.table_row, allowGapCursor: false }
337
+ };
338
+ const marks = {
339
+ link: {
340
+ attrs: { href: { default: '' }, title: { default: null } },
341
+ inclusive: false,
342
+ toDOM: (mark) => [
343
+ 'a',
344
+ { class: 'markdown-editor__link', href: mark.attrs.href, title: mark.attrs.title },
345
+ 0
346
+ ],
347
+ parseDOM: [
348
+ {
349
+ tag: 'a[href]',
350
+ getAttrs: (element) => ({
351
+ href: element.getAttribute('href') ?? '',
352
+ title: element.getAttribute('title')
353
+ })
354
+ }
355
+ ]
356
+ },
357
+ strong: {
358
+ toDOM: () => ['strong', 0],
359
+ parseDOM: [
360
+ { tag: 'strong' },
361
+ {
362
+ tag: 'b',
363
+ getAttrs: (element) => element.style.fontWeight !== 'normal' && null
364
+ },
365
+ { style: 'font-weight=400', clearMark: (mark) => mark.type.name === 'strong' },
366
+ {
367
+ style: 'font-weight',
368
+ getAttrs: (value) => /^(bold(er)?|[5-9]\d{2,})$/.test(value) && null
369
+ }
370
+ ]
371
+ },
372
+ em: {
373
+ toDOM: () => ['em', 0],
374
+ parseDOM: [{ tag: 'em' }, { tag: 'i' }, { style: 'font-style=italic' }]
375
+ },
376
+ strikethrough: {
377
+ toDOM: () => ['s', 0],
378
+ parseDOM: [{ tag: 's' }, { tag: 'del' }, { style: 'text-decoration=line-through' }]
379
+ },
380
+ code: {
381
+ code: true,
382
+ excludes: '_',
383
+ toDOM: () => ['code', { class: 'markdown-editor__inline-code' }, 0],
384
+ parseDOM: [{ tag: 'code' }]
385
+ },
386
+ /**
387
+ * 脚注のマーカー(`[^1]`)であることを表す。**見た目だけの札**であり、文字は原文のまま
388
+ * `[^1]` を持つ(書き出しは `verbatim` に任せる)。上付きの小さなマーカーとして描く。
389
+ *
390
+ * **`verbatim` より前に宣言する。** あとに置くと最も内側のマークがこちらになり、
391
+ * 書き出しの「エスケープしない」が効かなくなる(→ `verbatim` の注)。
392
+ *
393
+ * 脚注として組版はしない——注へ飛ぶ・注を文書の末尾へ集める・注を編集することは
394
+ * 別の話である(→ `docs/backlog.md` C13)。
395
+ */
396
+ footnote: {
397
+ inclusive: false,
398
+ toDOM: () => ['sup', { class: 'markdown-editor__footnote-marker' }, 0],
399
+ parseDOM: [{ tag: 'sup.markdown-editor__footnote-marker' }]
400
+ },
401
+ /**
402
+ * その文字が**記法の原文**であり、書き出しで手を加えてはならないというマーク。
403
+ * 文書モデルで表現できない記法(画像・インラインの HTML・番号付きの項目の
404
+ * チェックボックス)は、このマークを付けた文字として取り込む。**ブロックの側の
405
+ * `unknown_block` にあたるものが、インラインの側に無いことが壊れる理由だった。**
406
+ *
407
+ * **必ずマークの一覧のいちばん最後に置く。** マークの並びは宣言順(rank)の
408
+ * 昇順で作られるため、最後に置いたものが最も内側になる。書き出しの
409
+ * 「エスケープしない」(`escape: false`)が効くのは最も内側のマークのときだけであり、
410
+ * **後ろへマークを足すと、エスケープしない性質が黙って失われる**。
411
+ *
412
+ * `excludes` を持たせないのは、リンクの中の画像・強調の中の画像で共存が要るため。
413
+ * `inclusive: false` にするのは、**画像の直後に打った文字へマークが広がらない**ように
414
+ * するためである(広がると、書き手が普通に打った `[foo]` がエスケープされずリンクの
415
+ * 一部として読み直されうる)。同じスキーマの `link` が既にこの形である。
416
+ *
417
+ * 見た目は持たない。書き手からは今までどおりただの文字に見える。
418
+ */
419
+ verbatim: {
420
+ inclusive: false,
421
+ toDOM: () => ['span', { class: 'markdown-editor__verbatim' }, 0],
422
+ parseDOM: [{ tag: 'span.markdown-editor__verbatim' }]
423
+ }
424
+ };
425
+ export const outlineSchema = new Schema({
426
+ nodes: { ...structureNodes, ...blockNodes, ...tableSpec },
427
+ marks
428
+ });
@@ -0,0 +1,15 @@
1
+ import type { Node } from 'prosemirror-model';
2
+ import type { Transaction } from 'prosemirror-state';
3
+ import { type SectionPath, type SectionRef } from './document.types';
4
+ export interface SectionTree {
5
+ /** 位置がもっとも内側で属するセクションを返す */
6
+ resolveSection(doc: Node, pos: number): SectionRef | null;
7
+ /** 位置が属するセクションの祖先を、外側から順に返す(自身は含まない) */
8
+ resolveAncestors(doc: Node, pos: number): readonly SectionRef[];
9
+ resolveByPath(doc: Node, path: SectionPath): SectionRef | null;
10
+ /** 文書順のすべてのセクション。見出しを持たない先頭セクションを含む(`path` は `null`) */
11
+ listSections(doc: Node): readonly SectionRef[];
12
+ /** 対象とその配下の見出しレベルを、構造上の深さへ合わせ直す */
13
+ syncSectionLevels(tr: Transaction, target: SectionRef): Transaction;
14
+ }
15
+ export declare const sectionTree: SectionTree;
@@ -0,0 +1,123 @@
1
+ import { MAX_HEADING_LEVEL } from './document.types';
2
+ import { outlineSchema } from './schema';
3
+ const SECTION = outlineSchema.nodes.section;
4
+ const INTRO_SECTION = outlineSchema.nodes.intro_section;
5
+ export const sectionTree = {
6
+ resolveSection,
7
+ resolveAncestors,
8
+ resolveByPath,
9
+ listSections,
10
+ syncSectionLevels
11
+ };
12
+ function resolveSection(doc, pos) {
13
+ const chain = sectionChain(doc, pos);
14
+ return chain.length > 0 ? chain[chain.length - 1] : null;
15
+ }
16
+ function resolveAncestors(doc, pos) {
17
+ return sectionChain(doc, pos).slice(0, -1);
18
+ }
19
+ function resolveByPath(doc, path) {
20
+ if (path.length === 0)
21
+ return null;
22
+ let parent = doc;
23
+ let parentPos = -1;
24
+ let found = null;
25
+ for (const index of path) {
26
+ const child = nthSection(parent, index, parentPos);
27
+ if (!child)
28
+ return null;
29
+ found = child;
30
+ parent = child.node;
31
+ parentPos = child.pos;
32
+ }
33
+ return found ? toRef(doc, found.node, found.pos) : null;
34
+ }
35
+ function listSections(doc) {
36
+ const refs = [];
37
+ doc.descendants((node, pos) => {
38
+ if (node.type === INTRO_SECTION) {
39
+ refs.push(toRef(doc, node, pos));
40
+ return false;
41
+ }
42
+ if (node.type !== SECTION)
43
+ return false;
44
+ refs.push(toRef(doc, node, pos));
45
+ return true;
46
+ });
47
+ return refs;
48
+ }
49
+ function syncSectionLevels(tr, target) {
50
+ if (target.level === null)
51
+ return tr;
52
+ const start = target.pos;
53
+ const end = start + target.node.nodeSize;
54
+ tr.doc.nodesBetween(start, end, (node, pos) => {
55
+ if (node.type !== outlineSchema.nodes.heading)
56
+ return true;
57
+ // 見出しは section の直下にしかないため、位置の深さがそのまま構造上の深さになる
58
+ const level = clampLevel(tr.doc.resolve(pos).depth);
59
+ if (node.attrs.level !== level) {
60
+ tr.setNodeAttribute(pos, 'level', level);
61
+ }
62
+ return false;
63
+ });
64
+ return tr;
65
+ }
66
+ // --- ヘルパー ---
67
+ /** 位置を包むセクションを、外側から順に返す */
68
+ function sectionChain(doc, pos) {
69
+ const $pos = doc.resolve(Math.max(0, Math.min(pos, doc.content.size)));
70
+ const chain = [];
71
+ for (let depth = 1; depth <= $pos.depth; depth += 1) {
72
+ const node = $pos.node(depth);
73
+ if (node.type === SECTION || node.type === INTRO_SECTION) {
74
+ chain.push(toRef(doc, node, $pos.before(depth)));
75
+ }
76
+ }
77
+ return chain;
78
+ }
79
+ function toRef(doc, node, pos) {
80
+ const isIntro = node.type === INTRO_SECTION;
81
+ return {
82
+ pos,
83
+ node,
84
+ path: isIntro ? null : pathOf(doc, pos),
85
+ level: isIntro ? null : clampLevel(doc.resolve(pos).depth + 1),
86
+ // 見出し以外の中身を持つか(見出しを持たないセクションは中身が必ずある)
87
+ hasChildren: isIntro ? node.childCount > 0 : node.childCount > 1
88
+ };
89
+ }
90
+ /** セクションだけを数えたインデックス列。見出しを持たない先頭セクションは数に入れない */
91
+ function pathOf(doc, pos) {
92
+ const $pos = doc.resolve(pos);
93
+ const path = [];
94
+ for (let depth = 0; depth <= $pos.depth; depth += 1) {
95
+ const parent = $pos.node(depth);
96
+ const indexInParent = $pos.index(depth);
97
+ let sectionIndex = 0;
98
+ for (let i = 0; i < indexInParent; i += 1) {
99
+ if (parent.child(i).type === SECTION)
100
+ sectionIndex += 1;
101
+ }
102
+ path.push(sectionIndex);
103
+ }
104
+ return path;
105
+ }
106
+ function nthSection(parent, index, parentPos) {
107
+ // parentPos が -1 のときは doc。それ以外は section なので中身は +1 から始まる
108
+ let pos = parentPos + 1;
109
+ let seen = 0;
110
+ for (let i = 0; i < parent.childCount; i += 1) {
111
+ const child = parent.child(i);
112
+ if (child.type === SECTION) {
113
+ if (seen === index)
114
+ return { node: child, pos };
115
+ seen += 1;
116
+ }
117
+ pos += child.nodeSize;
118
+ }
119
+ return null;
120
+ }
121
+ function clampLevel(depth) {
122
+ return Math.min(Math.max(depth, 1), MAX_HEADING_LEVEL);
123
+ }
@@ -0,0 +1,9 @@
1
+ import type { MarkdownBlock } from './markdown.types';
2
+ /**
3
+ * 本文の文字列を受け取り、最上位のブロックを文書順に返す(公開 API)。
4
+ *
5
+ * 引数は `readBody` が返した**本文**である。マークダウン全体を渡すと、区切りが
6
+ * 水平線、項目の行が段落として読まれる。**引数がそのまま位置の基準になる**形にして
7
+ * あり、渡したものと返る位置の基準が食い違わない。
8
+ */
9
+ export declare function readBodyBlocks(body: string): readonly MarkdownBlock[];
@@ -0,0 +1,82 @@
1
+ import { markdownIt } from './markdownIt.js';
2
+ import { readPlainText } from './plainText.js';
3
+ /**
4
+ * 本文の文字列を受け取り、最上位のブロックを文書順に返す(公開 API)。
5
+ *
6
+ * 引数は `readBody` が返した**本文**である。マークダウン全体を渡すと、区切りが
7
+ * 水平線、項目の行が段落として読まれる。**引数がそのまま位置の基準になる**形にして
8
+ * あり、渡したものと返る位置の基準が食い違わない。
9
+ */
10
+ export function readBodyBlocks(body) {
11
+ const lines = lineSpans(body);
12
+ const tokens = markdownIt.parse(body, {});
13
+ const blocks = [];
14
+ const open = [];
15
+ tokens.forEach((token, index) => {
16
+ if (!isTopLevelBlock(token) || !token.map)
17
+ return;
18
+ const kind = kindOf(token);
19
+ // 見出しは、同じか浅いものに出会った分だけ戻してから経路を採る(自身を含めない)
20
+ if (kind === 'heading')
21
+ closeTo(open, levelOf(token));
22
+ blocks.push({
23
+ ...rangeOf(body, lines, token.map),
24
+ kind,
25
+ headingPath: open.map((heading) => heading.text)
26
+ });
27
+ if (kind === 'heading') {
28
+ open.push({ level: levelOf(token), text: readPlainText(tokens[index + 1]?.content ?? '') });
29
+ }
30
+ });
31
+ return blocks;
32
+ }
33
+ function closeTo(open, level) {
34
+ while (open.length > 0 && open[open.length - 1].level >= level)
35
+ open.pop();
36
+ }
37
+ function levelOf(token) {
38
+ return Number(token.tag.slice(1));
39
+ }
40
+ /** 最上位の開始トークン。閉じトークン(`nesting === -1`)は数えない */
41
+ function isTopLevelBlock(token) {
42
+ return token.level === 0 && token.nesting !== -1;
43
+ }
44
+ const KINDS = new Map([
45
+ ['heading_open', 'heading'],
46
+ ['paragraph_open', 'paragraph'],
47
+ ['fence', 'code'],
48
+ ['code_block', 'code'],
49
+ ['table_open', 'table'],
50
+ ['bullet_list_open', 'list'],
51
+ ['ordered_list_open', 'list'],
52
+ ['blockquote_open', 'blockquote'],
53
+ ['hr', 'horizontalRule']
54
+ ]);
55
+ /** 一覧に無いものは原文のブロックとして返す。**落とすと、範囲を繋いでも元の本文にならない** */
56
+ function kindOf(token) {
57
+ return KINDS.get(token.type) ?? 'raw';
58
+ }
59
+ function rangeOf(body, lines, map) {
60
+ const first = map[0];
61
+ let last = Math.min(map[1], lines.length) - 1;
62
+ while (last > first && !body.slice(lines[last].start, lines[last].end).trim())
63
+ last -= 1;
64
+ return { start: lines[first].start, end: lines[last].end };
65
+ }
66
+ /**
67
+ * 行の開始位置と終了位置の表を、**原文の側で**作る。
68
+ *
69
+ * パーサは改行を `\n` へ正規化するが行の対応は 1 対 1 に保たれるため、原文で数えれば
70
+ * `\r\n` の本文でも位置が合う。`split('\n')` で作ると `\r\n` の本文で 1 文字ずつずれる。
71
+ */
72
+ function lineSpans(body) {
73
+ const spans = [];
74
+ const breaks = /\r\n|\r|\n/g;
75
+ let start = 0;
76
+ for (let found = breaks.exec(body); found; found = breaks.exec(body)) {
77
+ spans.push({ start, end: found.index });
78
+ start = found.index + found[0].length;
79
+ }
80
+ spans.push({ start, end: body.length });
81
+ return spans;
82
+ }