@prosekit/extensions 0.11.4 → 0.11.5
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/commit/style.css +2 -0
- package/dist/commit/style.css.map +1 -0
- package/dist/commit/style.js +1 -0
- package/dist/{drop-indicator-dB9rZn8e.js → drop-indicator-D1eHOhSi.js} +4 -4
- package/dist/drop-indicator-D1eHOhSi.js.map +1 -0
- package/dist/enter-rule-RdhEA900.js +2 -1
- package/dist/enter-rule-RdhEA900.js.map +1 -0
- package/dist/gap-cursor/style.css +2 -0
- package/dist/gap-cursor/style.css.map +1 -0
- package/dist/gap-cursor/style.js +1 -0
- package/dist/{input-rule-Gji4N7Oe.js → input-rule-B17tpW4m.js} +3 -3
- package/dist/input-rule-B17tpW4m.js.map +1 -0
- package/dist/list/style.css +2 -0
- package/dist/list/style.css.map +1 -0
- package/dist/list/style.js +1 -0
- package/dist/loro/style.css +2 -0
- package/dist/loro/style.css.map +1 -0
- package/dist/loro/style.js +1 -0
- package/dist/{mark-rule-D7zaa32n.js → mark-rule-BCqIZMDu.js} +3 -3
- package/dist/mark-rule-BCqIZMDu.js.map +1 -0
- package/dist/{paste-rule-Cca3n5TA.js → paste-rule-DIEJKIje.js} +5 -15
- package/dist/paste-rule-DIEJKIje.js.map +1 -0
- package/dist/placeholder/style.css +2 -0
- package/dist/placeholder/style.css.map +1 -0
- package/dist/placeholder/style.js +1 -0
- package/dist/prosekit-extensions-autocomplete.d.ts +2 -1
- package/dist/prosekit-extensions-autocomplete.d.ts.map +1 -0
- package/dist/prosekit-extensions-autocomplete.js +2 -9
- package/dist/prosekit-extensions-autocomplete.js.map +1 -0
- package/dist/prosekit-extensions-blockquote.d.ts +2 -1
- package/dist/prosekit-extensions-blockquote.d.ts.map +1 -0
- package/dist/prosekit-extensions-blockquote.js +4 -4
- package/dist/prosekit-extensions-blockquote.js.map +1 -0
- package/dist/prosekit-extensions-bold.d.ts +2 -1
- package/dist/prosekit-extensions-bold.d.ts.map +1 -0
- package/dist/prosekit-extensions-bold.js +3 -2
- package/dist/prosekit-extensions-bold.js.map +1 -0
- package/dist/prosekit-extensions-code-block.d.ts +2 -1
- package/dist/prosekit-extensions-code-block.d.ts.map +1 -0
- package/dist/prosekit-extensions-code-block.js +5 -6
- package/dist/prosekit-extensions-code-block.js.map +1 -0
- package/dist/prosekit-extensions-code.d.ts +2 -1
- package/dist/prosekit-extensions-code.d.ts.map +1 -0
- package/dist/prosekit-extensions-code.js +3 -2
- package/dist/prosekit-extensions-code.js.map +1 -0
- package/dist/prosekit-extensions-commit.d.ts +2 -1
- package/dist/prosekit-extensions-commit.d.ts.map +1 -0
- package/dist/prosekit-extensions-commit.js +11 -13
- package/dist/prosekit-extensions-commit.js.map +1 -0
- package/dist/prosekit-extensions-doc.d.ts +2 -1
- package/dist/prosekit-extensions-doc.d.ts.map +1 -0
- package/dist/prosekit-extensions-doc.js +2 -1
- package/dist/prosekit-extensions-doc.js.map +1 -0
- package/dist/prosekit-extensions-drop-cursor.d.ts +2 -1
- package/dist/prosekit-extensions-drop-cursor.d.ts.map +1 -0
- package/dist/prosekit-extensions-drop-cursor.js +2 -1
- package/dist/prosekit-extensions-drop-cursor.js.map +1 -0
- package/dist/prosekit-extensions-drop-indicator.d.ts +2 -1
- package/dist/prosekit-extensions-drop-indicator.d.ts.map +1 -0
- package/dist/prosekit-extensions-drop-indicator.js +1 -1
- package/dist/prosekit-extensions-enter-rule.d.ts +2 -1
- package/dist/prosekit-extensions-enter-rule.d.ts.map +1 -0
- package/dist/prosekit-extensions-file.d.ts +2 -1
- package/dist/prosekit-extensions-file.d.ts.map +1 -0
- package/dist/prosekit-extensions-file.js +4 -16
- package/dist/prosekit-extensions-file.js.map +1 -0
- package/dist/prosekit-extensions-gap-cursor.d.ts +2 -2
- package/dist/prosekit-extensions-gap-cursor.d.ts.map +1 -0
- package/dist/prosekit-extensions-gap-cursor.js +2 -1
- package/dist/prosekit-extensions-gap-cursor.js.map +1 -0
- package/dist/prosekit-extensions-hard-break.d.ts +2 -1
- package/dist/prosekit-extensions-hard-break.d.ts.map +1 -0
- package/dist/prosekit-extensions-hard-break.js +2 -1
- package/dist/prosekit-extensions-hard-break.js.map +1 -0
- package/dist/prosekit-extensions-heading.d.ts +2 -1
- package/dist/prosekit-extensions-heading.d.ts.map +1 -0
- package/dist/prosekit-extensions-heading.js +5 -6
- package/dist/prosekit-extensions-heading.js.map +1 -0
- package/dist/prosekit-extensions-horizontal-rule.d.ts +2 -1
- package/dist/prosekit-extensions-horizontal-rule.d.ts.map +1 -0
- package/dist/prosekit-extensions-horizontal-rule.js +5 -6
- package/dist/prosekit-extensions-horizontal-rule.js.map +1 -0
- package/dist/prosekit-extensions-image.d.ts +2 -1
- package/dist/prosekit-extensions-image.d.ts.map +1 -0
- package/dist/prosekit-extensions-image.js +3 -3
- package/dist/prosekit-extensions-image.js.map +1 -0
- package/dist/prosekit-extensions-input-rule.d.ts +2 -1
- package/dist/prosekit-extensions-input-rule.d.ts.map +1 -0
- package/dist/prosekit-extensions-input-rule.js +1 -1
- package/dist/prosekit-extensions-italic.d.ts +2 -1
- package/dist/prosekit-extensions-italic.d.ts.map +1 -0
- package/dist/prosekit-extensions-italic.js +3 -2
- package/dist/prosekit-extensions-italic.js.map +1 -0
- package/dist/prosekit-extensions-link.d.ts +2 -1
- package/dist/prosekit-extensions-link.d.ts.map +1 -0
- package/dist/prosekit-extensions-link.js +6 -5
- package/dist/prosekit-extensions-link.js.map +1 -0
- package/dist/prosekit-extensions-list.d.ts +22 -21
- package/dist/prosekit-extensions-list.d.ts.map +1 -0
- package/dist/prosekit-extensions-list.js +6 -7
- package/dist/prosekit-extensions-list.js.map +1 -0
- package/dist/prosekit-extensions-loro.d.ts +14 -13
- package/dist/prosekit-extensions-loro.d.ts.map +1 -0
- package/dist/prosekit-extensions-loro.js +2 -1
- package/dist/prosekit-extensions-loro.js.map +1 -0
- package/dist/prosekit-extensions-mark-rule.d.ts +2 -1
- package/dist/prosekit-extensions-mark-rule.d.ts.map +1 -0
- package/dist/prosekit-extensions-mark-rule.js +1 -1
- package/dist/prosekit-extensions-mention.d.ts +2 -1
- package/dist/prosekit-extensions-mention.d.ts.map +1 -0
- package/dist/prosekit-extensions-mention.js +2 -1
- package/dist/prosekit-extensions-mention.js.map +1 -0
- package/dist/prosekit-extensions-mod-click-prevention.d.ts +2 -1
- package/dist/prosekit-extensions-mod-click-prevention.d.ts.map +1 -0
- package/dist/prosekit-extensions-mod-click-prevention.js +2 -1
- package/dist/prosekit-extensions-mod-click-prevention.js.map +1 -0
- package/dist/prosekit-extensions-paragraph.d.ts +2 -1
- package/dist/prosekit-extensions-paragraph.d.ts.map +1 -0
- package/dist/prosekit-extensions-paragraph.js +2 -1
- package/dist/prosekit-extensions-paragraph.js.map +1 -0
- package/dist/prosekit-extensions-paste-rule.d.ts +2 -1
- package/dist/prosekit-extensions-paste-rule.d.ts.map +1 -0
- package/dist/prosekit-extensions-paste-rule.js +1 -1
- package/dist/prosekit-extensions-placeholder.d.ts +2 -1
- package/dist/prosekit-extensions-placeholder.d.ts.map +1 -0
- package/dist/prosekit-extensions-placeholder.js +5 -5
- package/dist/prosekit-extensions-placeholder.js.map +1 -0
- package/dist/prosekit-extensions-readonly.d.ts +2 -1
- package/dist/prosekit-extensions-readonly.d.ts.map +1 -0
- package/dist/prosekit-extensions-readonly.js +2 -1
- package/dist/prosekit-extensions-readonly.js.map +1 -0
- package/dist/prosekit-extensions-search.d.ts +2 -1
- package/dist/prosekit-extensions-search.d.ts.map +1 -0
- package/dist/prosekit-extensions-search.js +3 -3
- package/dist/prosekit-extensions-search.js.map +1 -0
- package/dist/prosekit-extensions-strike.d.ts +2 -1
- package/dist/prosekit-extensions-strike.d.ts.map +1 -0
- package/dist/prosekit-extensions-strike.js +3 -2
- package/dist/prosekit-extensions-strike.js.map +1 -0
- package/dist/prosekit-extensions-table.d.ts +47 -114
- package/dist/prosekit-extensions-table.d.ts.map +1 -0
- package/dist/prosekit-extensions-table.js +2 -2
- package/dist/prosekit-extensions-text-align.d.ts +2 -1
- package/dist/prosekit-extensions-text-align.d.ts.map +1 -0
- package/dist/prosekit-extensions-text-align.js +2 -1
- package/dist/prosekit-extensions-text-align.js.map +1 -0
- package/dist/prosekit-extensions-text.d.ts +2 -1
- package/dist/prosekit-extensions-text.d.ts.map +1 -0
- package/dist/prosekit-extensions-text.js +2 -1
- package/dist/prosekit-extensions-text.js.map +1 -0
- package/dist/prosekit-extensions-underline.d.ts +2 -1
- package/dist/prosekit-extensions-underline.d.ts.map +1 -0
- package/dist/prosekit-extensions-underline.js +2 -1
- package/dist/prosekit-extensions-underline.js.map +1 -0
- package/dist/prosekit-extensions-virtual-selection.d.ts +2 -1
- package/dist/prosekit-extensions-virtual-selection.d.ts.map +1 -0
- package/dist/prosekit-extensions-virtual-selection.js +3 -3
- package/dist/prosekit-extensions-virtual-selection.js.map +1 -0
- package/dist/prosekit-extensions-yjs.d.ts +2 -1
- package/dist/prosekit-extensions-yjs.d.ts.map +1 -0
- package/dist/prosekit-extensions-yjs.js +2 -1
- package/dist/prosekit-extensions-yjs.js.map +1 -0
- package/dist/prosekit-extensions.d.ts +1 -1
- package/dist/prosekit-extensions.js +1 -0
- package/dist/search/style.css +2 -0
- package/dist/search/style.css.map +1 -0
- package/dist/search/style.js +1 -0
- package/dist/shiki-highlighter-chunk-DSPM0T27.d.ts +2 -1
- package/dist/shiki-highlighter-chunk-DSPM0T27.d.ts.map +1 -0
- package/dist/shiki-highlighter-chunk.js +3 -5
- package/dist/shiki-highlighter-chunk.js.map +1 -0
- package/dist/table/style.css +2 -0
- package/dist/table/style.css.map +1 -0
- package/dist/table/style.js +1 -0
- package/dist/table-Bi7WsMI3.js +297 -0
- package/dist/table-Bi7WsMI3.js.map +1 -0
- package/dist/virtual-selection/style.css +2 -0
- package/dist/virtual-selection/style.css.map +1 -0
- package/dist/virtual-selection/style.js +1 -0
- package/dist/yjs/style.css +2 -0
- package/dist/yjs/style.css.map +1 -0
- package/dist/yjs/style.js +1 -0
- package/package.json +10 -9
- package/src/autocomplete/autocomplete-helpers.ts +74 -0
- package/src/autocomplete/autocomplete-plugin.ts +186 -0
- package/src/autocomplete/autocomplete-rule.ts +117 -0
- package/src/autocomplete/autocomplete.spec.ts +132 -0
- package/src/autocomplete/autocomplete.ts +29 -0
- package/src/autocomplete/index.ts +9 -0
- package/src/blockquote/blockquote-commands.ts +32 -0
- package/src/blockquote/blockquote-input-rule.ts +14 -0
- package/src/blockquote/blockquote-keymap.spec.ts +45 -0
- package/src/blockquote/blockquote-keymap.ts +31 -0
- package/src/blockquote/blockquote-spec.ts +24 -0
- package/src/blockquote/blockquote.ts +34 -0
- package/src/blockquote/index.ts +14 -0
- package/src/bold/bold-commands.ts +23 -0
- package/src/bold/bold-input-rule.spec.ts +51 -0
- package/src/bold/bold-input-rule.ts +18 -0
- package/src/bold/bold-keymap.ts +14 -0
- package/src/bold/bold-spec.ts +53 -0
- package/src/bold/bold.ts +32 -0
- package/src/bold/index.ts +14 -0
- package/src/code/code-commands.ts +23 -0
- package/src/code/code-input-rule.ts +18 -0
- package/src/code/code-keymap.ts +14 -0
- package/src/code/code-spec.ts +28 -0
- package/src/code/code.ts +32 -0
- package/src/code/index.ts +14 -0
- package/src/code-block/code-block-commands.ts +44 -0
- package/src/code-block/code-block-highlight.ts +40 -0
- package/src/code-block/code-block-input-rule.ts +36 -0
- package/src/code-block/code-block-keymap.ts +61 -0
- package/src/code-block/code-block-shiki.ts +58 -0
- package/src/code-block/code-block-spec.spec.ts +164 -0
- package/src/code-block/code-block-spec.ts +71 -0
- package/src/code-block/code-block-types.ts +8 -0
- package/src/code-block/code-block.ts +46 -0
- package/src/code-block/index.ts +32 -0
- package/src/code-block/shiki-bundle.ts +8 -0
- package/src/code-block/shiki-highlighter-chunk.ts +84 -0
- package/src/code-block/shiki-highlighter.ts +22 -0
- package/src/code-block/shiki-parser.ts +36 -0
- package/src/commit/index.ts +330 -0
- package/src/commit/style.css +7 -0
- package/src/doc/index.ts +21 -0
- package/src/drop-cursor/drop-cursor.ts +46 -0
- package/src/drop-cursor/index.ts +5 -0
- package/src/drop-indicator/drop-indicator-facet.ts +84 -0
- package/src/drop-indicator/drop-indicator-plugin.ts +147 -0
- package/src/drop-indicator/drop-indicator.ts +37 -0
- package/src/drop-indicator/drop-target.ts +168 -0
- package/src/drop-indicator/index.ts +14 -0
- package/src/drop-indicator/types.ts +90 -0
- package/src/enter-rule/index.ts +241 -0
- package/src/file/file-drop-handler.ts +75 -0
- package/src/file/file-paste-handler.spec.ts +95 -0
- package/src/file/file-paste-handler.ts +59 -0
- package/src/file/file-upload.ts +119 -0
- package/src/file/helpers.ts +39 -0
- package/src/file/index.ts +16 -0
- package/src/gap-cursor/gap-cursor.ts +28 -0
- package/src/gap-cursor/index.ts +4 -0
- package/src/gap-cursor/style.css +25 -0
- package/src/hard-break/hard-break-commands.ts +31 -0
- package/src/hard-break/hard-break-keymap.spec.ts +45 -0
- package/src/hard-break/hard-break-keymap.ts +16 -0
- package/src/hard-break/hard-break-spec.ts +31 -0
- package/src/hard-break/hard-break.ts +32 -0
- package/src/hard-break/index.ts +13 -0
- package/src/heading/heading-commands.ts +37 -0
- package/src/heading/heading-input-rule.ts +22 -0
- package/src/heading/heading-keymap.spec.ts +53 -0
- package/src/heading/heading-keymap.ts +40 -0
- package/src/heading/heading-spec.ts +39 -0
- package/src/heading/heading-types.ts +3 -0
- package/src/heading/heading.ts +34 -0
- package/src/heading/index.ts +15 -0
- package/src/horizontal-rule/horizontal-rule-commands.spec.ts +61 -0
- package/src/horizontal-rule/horizontal-rule-commands.ts +37 -0
- package/src/horizontal-rule/horizontal-rule-input-rule.spec.ts +61 -0
- package/src/horizontal-rule/horizontal-rule-input-rule.ts +26 -0
- package/src/horizontal-rule/horizontal-rule-spec.ts +21 -0
- package/src/horizontal-rule/horizontal-rule.ts +29 -0
- package/src/horizontal-rule/index.ts +14 -0
- package/src/image/image-commands.ts +27 -0
- package/src/image/image-spec.ts +72 -0
- package/src/image/image.ts +25 -0
- package/src/image/index.ts +13 -0
- package/src/index.ts +1 -0
- package/src/input-rule/index.ts +237 -0
- package/src/italic/index.ts +14 -0
- package/src/italic/italic-commands.spec.ts +75 -0
- package/src/italic/italic-commands.ts +23 -0
- package/src/italic/italic-input-rule.spec.ts +25 -0
- package/src/italic/italic-input-rule.ts +18 -0
- package/src/italic/italic-keymap.ts +14 -0
- package/src/italic/italic-spec.ts +35 -0
- package/src/italic/italic.ts +34 -0
- package/src/link/index.spec.ts +88 -0
- package/src/link/index.ts +156 -0
- package/src/link/link-paste-rule.spec.ts +194 -0
- package/src/link/link-paste-rule.ts +22 -0
- package/src/link/link-regex.spec.ts +82 -0
- package/src/link/link-regex.ts +79 -0
- package/src/link/link-types.ts +8 -0
- package/src/list/index.ts +25 -0
- package/src/list/list-commands.ts +61 -0
- package/src/list/list-drop-indicator.ts +37 -0
- package/src/list/list-input-rules.ts +14 -0
- package/src/list/list-keymap.spec.ts +39 -0
- package/src/list/list-keymap.ts +48 -0
- package/src/list/list-plugins.ts +35 -0
- package/src/list/list-serializer.ts +38 -0
- package/src/list/list-spec.ts +60 -0
- package/src/list/list-types.spec.ts +10 -0
- package/src/list/list-types.ts +23 -0
- package/src/list/list.spec.ts +134 -0
- package/src/list/list.ts +38 -0
- package/src/list/style.css +128 -0
- package/src/loro/index.ts +17 -0
- package/src/loro/loro-commands.ts +27 -0
- package/src/loro/loro-cursor-plugin.ts +28 -0
- package/src/loro/loro-keymap.ts +23 -0
- package/src/loro/loro-sync-plugin.ts +14 -0
- package/src/loro/loro-undo-plugin.ts +12 -0
- package/src/loro/loro.ts +75 -0
- package/src/loro/style.css +33 -0
- package/src/mark-rule/apply.ts +129 -0
- package/src/mark-rule/index.ts +2 -0
- package/src/mark-rule/mark-rule.spec.ts +123 -0
- package/src/mark-rule/mark-rule.ts +48 -0
- package/src/mark-rule/range.ts +107 -0
- package/src/mark-rule/types.ts +30 -0
- package/src/mention/index.ts +90 -0
- package/src/mod-click-prevention/index.ts +35 -0
- package/src/paragraph/index.ts +7 -0
- package/src/paragraph/paragraph-commands.ts +29 -0
- package/src/paragraph/paragraph-keymap.ts +15 -0
- package/src/paragraph/paragraph-spec.ts +31 -0
- package/src/paragraph/paragraph.ts +37 -0
- package/src/paste-rule/index.ts +10 -0
- package/src/paste-rule/mark-paste-rule.spec.ts +112 -0
- package/src/paste-rule/mark-paste-rule.ts +194 -0
- package/src/paste-rule/paste-rule-plugin.ts +53 -0
- package/src/paste-rule/paste-rule.spec.ts +96 -0
- package/src/paste-rule/paste-rule.ts +60 -0
- package/src/paste-rule/split-text-by-regex.spec.ts +97 -0
- package/src/paste-rule/split-text-by-regex.ts +44 -0
- package/src/placeholder/index.ts +113 -0
- package/src/placeholder/style.css +7 -0
- package/src/readonly/index.ts +22 -0
- package/src/search/index.ts +140 -0
- package/src/search/style.css +13 -0
- package/src/strike/index.ts +101 -0
- package/src/table/index.ts +53 -0
- package/src/table/style.css +42 -0
- package/src/table/table-commands/delete-cell-selection.spec.ts +41 -0
- package/src/table/table-commands/delete-cell-selection.ts +1 -0
- package/src/table/table-commands/exit-table.spec.ts +45 -0
- package/src/table/table-commands/exit-table.ts +49 -0
- package/src/table/table-commands/insert-table.spec.ts +39 -0
- package/src/table/table-commands/insert-table.ts +80 -0
- package/src/table/table-commands/move-table-column.spec.ts +618 -0
- package/src/table/table-commands/move-table-column.ts +4 -0
- package/src/table/table-commands/move-table-row.spec.ts +380 -0
- package/src/table/table-commands/move-table-row.ts +4 -0
- package/src/table/table-commands/select-table-cell.spec.ts +34 -0
- package/src/table/table-commands/select-table-cell.ts +35 -0
- package/src/table/table-commands/select-table-column.spec.ts +33 -0
- package/src/table/table-commands/select-table-column.ts +39 -0
- package/src/table/table-commands/select-table-row.spec.ts +32 -0
- package/src/table/table-commands/select-table-row.ts +39 -0
- package/src/table/table-commands/select-table.spec.ts +36 -0
- package/src/table/table-commands/select-table.ts +50 -0
- package/src/table/table-commands.ts +110 -0
- package/src/table/table-drop-indicator.ts +40 -0
- package/src/table/table-plugins.ts +15 -0
- package/src/table/table-spec.spec.ts +113 -0
- package/src/table/table-spec.ts +109 -0
- package/src/table/table-utils.ts +16 -0
- package/src/table/table.ts +49 -0
- package/src/table/test-utils.ts +28 -0
- package/src/testing/clipboard.ts +58 -0
- package/src/testing/format-html.ts +5 -0
- package/src/testing/index.ts +161 -0
- package/src/testing/keyboard.ts +36 -0
- package/src/testing/markdown.ts +23 -0
- package/src/text/index.ts +24 -0
- package/src/text-align/index.ts +133 -0
- package/src/types/assert-type-equal.ts +8 -0
- package/src/underline/index.ts +83 -0
- package/src/virtual-selection/index.ts +100 -0
- package/src/virtual-selection/style.css +5 -0
- package/src/yjs/index.ts +22 -0
- package/src/yjs/style.css +31 -0
- package/src/yjs/yjs-commands.ts +27 -0
- package/src/yjs/yjs-cursor-plugin.ts +25 -0
- package/src/yjs/yjs-keymap.ts +23 -0
- package/src/yjs/yjs-sync-plugin.ts +23 -0
- package/src/yjs/yjs-undo-plugin.ts +87 -0
- package/src/yjs/yjs.ts +84 -0
- package/dist/table-CPI9ZxbK.js +0 -760
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {
|
|
2
|
+
defineBold,
|
|
3
|
+
type BoldExtension,
|
|
4
|
+
} from './bold'
|
|
5
|
+
export {
|
|
6
|
+
defineBoldCommands,
|
|
7
|
+
type BoldCommandsExtension,
|
|
8
|
+
} from './bold-commands'
|
|
9
|
+
export { defineBoldInputRule } from './bold-input-rule'
|
|
10
|
+
export { defineBoldKeymap } from './bold-keymap'
|
|
11
|
+
export {
|
|
12
|
+
defineBoldSpec,
|
|
13
|
+
type BoldSpecExtension,
|
|
14
|
+
} from './bold-spec'
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineCommands,
|
|
3
|
+
toggleMark,
|
|
4
|
+
type Extension,
|
|
5
|
+
} from '@prosekit/core'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export type CodeCommandsExtension = Extension<{
|
|
11
|
+
Commands: {
|
|
12
|
+
toggleCode: []
|
|
13
|
+
}
|
|
14
|
+
}>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export function defineCodeCommands(): CodeCommandsExtension {
|
|
20
|
+
return defineCommands({
|
|
21
|
+
toggleCode: () => toggleMark({ type: 'code' }),
|
|
22
|
+
})
|
|
23
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canUseRegexLookbehind,
|
|
3
|
+
type PlainExtension,
|
|
4
|
+
} from '@prosekit/core'
|
|
5
|
+
|
|
6
|
+
import { defineMarkInputRule } from '../input-rule'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export function defineCodeInputRule(): PlainExtension {
|
|
12
|
+
return defineMarkInputRule({
|
|
13
|
+
regex: canUseRegexLookbehind()
|
|
14
|
+
? /(?<=\s|^)`([^\s`]|[^\s`][^`]*[^\s`])`$/
|
|
15
|
+
: /`([^\s`]|[^\s`][^`]*[^\s`])`$/,
|
|
16
|
+
type: 'code',
|
|
17
|
+
})
|
|
18
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineMarkSpec,
|
|
3
|
+
type Extension,
|
|
4
|
+
} from '@prosekit/core'
|
|
5
|
+
import type { Attrs } from '@prosekit/pm/model'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export type CodeSpecExtension = Extension<{
|
|
11
|
+
Marks: {
|
|
12
|
+
code: Attrs
|
|
13
|
+
}
|
|
14
|
+
}>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export function defineCodeSpec(): CodeSpecExtension {
|
|
20
|
+
return defineMarkSpec({
|
|
21
|
+
name: 'code',
|
|
22
|
+
parseDOM: [{ tag: 'code' }],
|
|
23
|
+
code: true,
|
|
24
|
+
toDOM() {
|
|
25
|
+
return ['code', 0]
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
}
|
package/src/code/code.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
union,
|
|
3
|
+
type Union,
|
|
4
|
+
} from '@prosekit/core'
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
defineCodeCommands,
|
|
8
|
+
type CodeCommandsExtension,
|
|
9
|
+
} from './code-commands'
|
|
10
|
+
import { defineCodeInputRule } from './code-input-rule'
|
|
11
|
+
import { defineCodeKeymap } from './code-keymap'
|
|
12
|
+
import {
|
|
13
|
+
defineCodeSpec,
|
|
14
|
+
type CodeSpecExtension,
|
|
15
|
+
} from './code-spec'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export type CodeExtension = Union<[CodeSpecExtension, CodeCommandsExtension]>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export function defineCode(): CodeExtension {
|
|
26
|
+
return union(
|
|
27
|
+
defineCodeSpec(),
|
|
28
|
+
defineCodeCommands(),
|
|
29
|
+
defineCodeKeymap(),
|
|
30
|
+
defineCodeInputRule(),
|
|
31
|
+
)
|
|
32
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {
|
|
2
|
+
defineCode,
|
|
3
|
+
type CodeExtension,
|
|
4
|
+
} from './code'
|
|
5
|
+
export {
|
|
6
|
+
defineCodeCommands,
|
|
7
|
+
type CodeCommandsExtension,
|
|
8
|
+
} from './code-commands'
|
|
9
|
+
export { defineCodeInputRule } from './code-input-rule'
|
|
10
|
+
export { defineCodeKeymap } from './code-keymap'
|
|
11
|
+
export {
|
|
12
|
+
defineCodeSpec,
|
|
13
|
+
type CodeSpecExtension,
|
|
14
|
+
} from './code-spec'
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineCommands,
|
|
3
|
+
insertNode,
|
|
4
|
+
setBlockType,
|
|
5
|
+
setNodeAttrs,
|
|
6
|
+
toggleNode,
|
|
7
|
+
type Extension,
|
|
8
|
+
} from '@prosekit/core'
|
|
9
|
+
|
|
10
|
+
import type { CodeBlockAttrs } from './code-block-types'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export type CodeBlockCommandsExtension = Extension<{
|
|
16
|
+
Commands: {
|
|
17
|
+
setCodeBlock: [attrs?: CodeBlockAttrs]
|
|
18
|
+
insertCodeBlock: [attrs?: CodeBlockAttrs]
|
|
19
|
+
toggleCodeBlock: [attrs?: CodeBlockAttrs]
|
|
20
|
+
setCodeBlockAttrs: [attrs: CodeBlockAttrs]
|
|
21
|
+
}
|
|
22
|
+
}>
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Adds commands for working with `codeBlock` nodes.
|
|
26
|
+
*
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export function defineCodeBlockCommands(): CodeBlockCommandsExtension {
|
|
30
|
+
return defineCommands({
|
|
31
|
+
setCodeBlock: (attrs?: CodeBlockAttrs) => {
|
|
32
|
+
return setBlockType({ type: 'codeBlock', attrs })
|
|
33
|
+
},
|
|
34
|
+
insertCodeBlock: (attrs?: CodeBlockAttrs) => {
|
|
35
|
+
return insertNode({ type: 'codeBlock', attrs })
|
|
36
|
+
},
|
|
37
|
+
toggleCodeBlock: (attrs?: CodeBlockAttrs) => {
|
|
38
|
+
return toggleNode({ type: 'codeBlock', attrs })
|
|
39
|
+
},
|
|
40
|
+
setCodeBlockAttrs: (attrs: CodeBlockAttrs) => {
|
|
41
|
+
return setNodeAttrs({ type: 'codeBlock', attrs })
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
definePlugin,
|
|
3
|
+
type Extension,
|
|
4
|
+
} from '@prosekit/core'
|
|
5
|
+
import {
|
|
6
|
+
createHighlightPlugin,
|
|
7
|
+
type Parser,
|
|
8
|
+
} from 'prosemirror-highlight'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*
|
|
13
|
+
* An alias for the `Parser` type from the `prosemirror-highlight` package.
|
|
14
|
+
*/
|
|
15
|
+
export type HighlightParser = Parser
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export type CodeBlockHighlightOptions = {
|
|
21
|
+
parser: HighlightParser
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Adds syntax highlighting to code blocks. This function requires a `Parser`
|
|
26
|
+
* instance from the `prosemirror-highlight` package. See the
|
|
27
|
+
* [documentation](https://github.com/ocavue/prosemirror-highlight) for more
|
|
28
|
+
* information.
|
|
29
|
+
*
|
|
30
|
+
* @param options
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export function defineCodeBlockHighlight({
|
|
35
|
+
parser,
|
|
36
|
+
}: CodeBlockHighlightOptions): Extension {
|
|
37
|
+
return definePlugin(
|
|
38
|
+
createHighlightPlugin({ parser }),
|
|
39
|
+
)
|
|
40
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PlainExtension } from '@prosekit/core'
|
|
2
|
+
|
|
3
|
+
import { defineTextBlockEnterRule } from '../enter-rule'
|
|
4
|
+
import { defineTextBlockInputRule } from '../input-rule'
|
|
5
|
+
|
|
6
|
+
import type { CodeBlockAttrs } from './code-block-types'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Adds input rules for `codeBlock` nodes.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export function defineCodeBlockInputRule(): PlainExtension {
|
|
14
|
+
return defineTextBlockInputRule({
|
|
15
|
+
regex: /^```(\S*)\s$/,
|
|
16
|
+
type: 'codeBlock',
|
|
17
|
+
attrs: getAttrs,
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Adds enter rules for `codeBlock` nodes.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export function defineCodeBlockEnterRule(): PlainExtension {
|
|
27
|
+
return defineTextBlockEnterRule({
|
|
28
|
+
regex: /^```(\S*)$/,
|
|
29
|
+
type: 'codeBlock',
|
|
30
|
+
attrs: getAttrs,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getAttrs(match: RegExpMatchArray): CodeBlockAttrs {
|
|
35
|
+
return { language: match[1] || '' }
|
|
36
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defaultBlockAt,
|
|
3
|
+
defineKeymap,
|
|
4
|
+
type PlainExtension,
|
|
5
|
+
} from '@prosekit/core'
|
|
6
|
+
import {
|
|
7
|
+
TextSelection,
|
|
8
|
+
type Command,
|
|
9
|
+
} from '@prosekit/pm/state'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Defines the keymap for code blocks.
|
|
13
|
+
*/
|
|
14
|
+
export function defineCodeBlockKeymap(): PlainExtension {
|
|
15
|
+
return defineKeymap({
|
|
16
|
+
Enter: existCodeBlock,
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Exit a code block and insert a default block below if the cursor is at the
|
|
22
|
+
* end of the code block and the code block is ended with two new lines.
|
|
23
|
+
*/
|
|
24
|
+
const existCodeBlock: Command = (state, dispatch) => {
|
|
25
|
+
if (!state.selection.empty) {
|
|
26
|
+
return false
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const { $head } = state.selection
|
|
30
|
+
const parent = $head.parent
|
|
31
|
+
if (
|
|
32
|
+
parent.isTextblock
|
|
33
|
+
&& parent.type.spec.code
|
|
34
|
+
&& $head.parentOffset === parent.content.size
|
|
35
|
+
&& parent.textContent.endsWith('\n\n')
|
|
36
|
+
) {
|
|
37
|
+
const grandParent = $head.node(-1)
|
|
38
|
+
const insertIndex = $head.indexAfter(-1)
|
|
39
|
+
const type = defaultBlockAt(grandParent.contentMatchAt(insertIndex))
|
|
40
|
+
|
|
41
|
+
if (!type || !grandParent.canReplaceWith(insertIndex, insertIndex, type)) {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (dispatch) {
|
|
46
|
+
const { tr } = state
|
|
47
|
+
tr.delete($head.pos - 2, $head.pos)
|
|
48
|
+
const pos = tr.selection.$head.after()
|
|
49
|
+
const node = type.createAndFill()
|
|
50
|
+
if (node) {
|
|
51
|
+
tr.replaceWith(pos, pos, node)
|
|
52
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(pos), 1))
|
|
53
|
+
dispatch(tr.scrollIntoView())
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return true
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return false
|
|
61
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Extension } from '@prosekit/core'
|
|
2
|
+
import type { SpecialLanguage } from 'shiki'
|
|
3
|
+
|
|
4
|
+
import { defineCodeBlockHighlight } from './code-block-highlight'
|
|
5
|
+
import type {
|
|
6
|
+
ShikiBundledLanguage,
|
|
7
|
+
ShikiBundledTheme,
|
|
8
|
+
} from './shiki-bundle'
|
|
9
|
+
import type { ShikiHighlighterOptions } from './shiki-highlighter-chunk'
|
|
10
|
+
import { createLazyParser } from './shiki-parser'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The options to configure the Shiki highlighter.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export interface CodeBlockShikiOptions extends Omit<ShikiHighlighterOptions, 'themes' | 'langs' | 'engine'> {
|
|
18
|
+
/**
|
|
19
|
+
* A list of Shiki themes to pre-load. The first theme in the list will be
|
|
20
|
+
* used to render the code block.
|
|
21
|
+
*
|
|
22
|
+
* @default ['one-dark-pro']
|
|
23
|
+
*/
|
|
24
|
+
themes?: ShikiBundledTheme[]
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A list of Shiki languages to pre-load.
|
|
28
|
+
*
|
|
29
|
+
* @default ['text']
|
|
30
|
+
*/
|
|
31
|
+
langs?: (ShikiBundledLanguage | SpecialLanguage)[]
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The RegExp engine to use. By default, the JavaScript engine is used.
|
|
35
|
+
*/
|
|
36
|
+
engine?: ShikiHighlighterOptions['engine']
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Adds syntax highlighting to code blocks using the [Shiki](https://github.com/shikijs/shiki) package.
|
|
41
|
+
*
|
|
42
|
+
* It will set two CSS variables on the code block elements:
|
|
43
|
+
*
|
|
44
|
+
* - `--prosemirror-highlight`: sets text color
|
|
45
|
+
* - `--prosemirror-highlight-bg`: sets background color
|
|
46
|
+
*
|
|
47
|
+
* @param options - The options to configure the Shiki highlighter.
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export function defineCodeBlockShiki({
|
|
52
|
+
themes = ['one-dark-pro'],
|
|
53
|
+
langs = ['text'],
|
|
54
|
+
...rest
|
|
55
|
+
}: CodeBlockShikiOptions = {}): Extension {
|
|
56
|
+
const parser = createLazyParser({ themes, langs, ...rest })
|
|
57
|
+
return defineCodeBlockHighlight({ parser })
|
|
58
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { jsonFromHTML } from '@prosekit/core'
|
|
2
|
+
import {
|
|
3
|
+
describe,
|
|
4
|
+
expect,
|
|
5
|
+
it,
|
|
6
|
+
} from 'vitest'
|
|
7
|
+
|
|
8
|
+
import { setupTest } from '../testing'
|
|
9
|
+
import { formatHTML } from '../testing/format-html'
|
|
10
|
+
import {
|
|
11
|
+
htmlFromMarkdown,
|
|
12
|
+
markdownFromHTML,
|
|
13
|
+
} from '../testing/markdown'
|
|
14
|
+
|
|
15
|
+
describe('defineCodeBlockSpec', () => {
|
|
16
|
+
it('can parse and serialize code blocks', () => {
|
|
17
|
+
const { editor, n } = setupTest()
|
|
18
|
+
const doc = n.doc(
|
|
19
|
+
n.codeBlock({ language: 'javascript' }, 'console.log("Hello, javascript!");'),
|
|
20
|
+
n.codeBlock({ language: 'JS' }, 'print("Hello, JS!");'),
|
|
21
|
+
n.codeBlock({ language: 'UNKNOWN_LANG' }, 'print hello world'),
|
|
22
|
+
n.codeBlock('hello world'),
|
|
23
|
+
)
|
|
24
|
+
editor.set(doc)
|
|
25
|
+
const html = editor.getDocHTML()
|
|
26
|
+
expect(formatHTML(html)).toMatchInlineSnapshot(
|
|
27
|
+
`
|
|
28
|
+
"
|
|
29
|
+
<div>
|
|
30
|
+
<pre data-language="javascript">
|
|
31
|
+
<code class="language-javascript">
|
|
32
|
+
console.log("Hello, javascript!");
|
|
33
|
+
</code>
|
|
34
|
+
</pre>
|
|
35
|
+
<pre data-language="JS">
|
|
36
|
+
<code class="language-JS">
|
|
37
|
+
print("Hello, JS!");
|
|
38
|
+
</code>
|
|
39
|
+
</pre>
|
|
40
|
+
<pre data-language="UNKNOWN_LANG">
|
|
41
|
+
<code class="language-UNKNOWN_LANG">
|
|
42
|
+
print hello world
|
|
43
|
+
</code>
|
|
44
|
+
</pre>
|
|
45
|
+
<pre>
|
|
46
|
+
<code>
|
|
47
|
+
hello world
|
|
48
|
+
</code>
|
|
49
|
+
</pre>
|
|
50
|
+
</div>
|
|
51
|
+
"
|
|
52
|
+
`,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
const json = editor.getDocJSON()
|
|
56
|
+
expect(json).toMatchInlineSnapshot(`
|
|
57
|
+
{
|
|
58
|
+
"content": [
|
|
59
|
+
{
|
|
60
|
+
"attrs": {
|
|
61
|
+
"language": "javascript",
|
|
62
|
+
},
|
|
63
|
+
"content": [
|
|
64
|
+
{
|
|
65
|
+
"text": "console.log("Hello, javascript!");",
|
|
66
|
+
"type": "text",
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
"type": "codeBlock",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"attrs": {
|
|
73
|
+
"language": "JS",
|
|
74
|
+
},
|
|
75
|
+
"content": [
|
|
76
|
+
{
|
|
77
|
+
"text": "print("Hello, JS!");",
|
|
78
|
+
"type": "text",
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
"type": "codeBlock",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
"attrs": {
|
|
85
|
+
"language": "UNKNOWN_LANG",
|
|
86
|
+
},
|
|
87
|
+
"content": [
|
|
88
|
+
{
|
|
89
|
+
"text": "print hello world",
|
|
90
|
+
"type": "text",
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
"type": "codeBlock",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"attrs": {
|
|
97
|
+
"language": "",
|
|
98
|
+
},
|
|
99
|
+
"content": [
|
|
100
|
+
{
|
|
101
|
+
"text": "hello world",
|
|
102
|
+
"type": "text",
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
"type": "codeBlock",
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
"type": "doc",
|
|
109
|
+
}
|
|
110
|
+
`)
|
|
111
|
+
|
|
112
|
+
const json2 = jsonFromHTML(html, { schema: editor.schema })
|
|
113
|
+
expect(json2).toEqual(json)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('can parse html generated by remark', () => {
|
|
117
|
+
const { editor } = setupTest()
|
|
118
|
+
|
|
119
|
+
const markdown = ['```javascript', '1234', '```'].join('\n')
|
|
120
|
+
const html = htmlFromMarkdown(markdown)
|
|
121
|
+
|
|
122
|
+
expect(html).toMatchInlineSnapshot(`
|
|
123
|
+
"<pre><code class="language-javascript">1234
|
|
124
|
+
</code></pre>
|
|
125
|
+
"
|
|
126
|
+
`)
|
|
127
|
+
const json = jsonFromHTML(html, { schema: editor.schema })
|
|
128
|
+
|
|
129
|
+
expect(json).toMatchObject(
|
|
130
|
+
{
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
attrs: {
|
|
134
|
+
language: 'javascript',
|
|
135
|
+
},
|
|
136
|
+
content: [
|
|
137
|
+
{
|
|
138
|
+
text: '1234' + '\n',
|
|
139
|
+
type: 'text',
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
type: 'codeBlock',
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
type: 'doc',
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('can generate html that can be parsed by remark', () => {
|
|
151
|
+
const { editor, n } = setupTest()
|
|
152
|
+
editor.set(n.doc(n.codeBlock({ language: 'javascript' }, '123')))
|
|
153
|
+
const html = editor.getDocHTML()
|
|
154
|
+
expect(html).toMatchInlineSnapshot(`"<div><pre data-language="javascript"><code class="language-javascript">123</code></pre></div>"`)
|
|
155
|
+
let markdown = markdownFromHTML(html)
|
|
156
|
+
expect(markdown).toMatchInlineSnapshot(`
|
|
157
|
+
"\`\`\`javascript
|
|
158
|
+
123
|
|
159
|
+
\`\`\`
|
|
160
|
+
"
|
|
161
|
+
`)
|
|
162
|
+
expect(markdown).toContain('```javascript')
|
|
163
|
+
})
|
|
164
|
+
})
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineNodeSpec,
|
|
3
|
+
type Extension,
|
|
4
|
+
} from '@prosekit/core'
|
|
5
|
+
|
|
6
|
+
import type { CodeBlockAttrs } from './code-block-types'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export type CodeBlockSpecExtension = Extension<{
|
|
12
|
+
Nodes: {
|
|
13
|
+
codeBlock: CodeBlockAttrs
|
|
14
|
+
}
|
|
15
|
+
}>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Defines the `codeBlock` node spec.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export function defineCodeBlockSpec(): CodeBlockSpecExtension {
|
|
23
|
+
return defineNodeSpec({
|
|
24
|
+
name: 'codeBlock',
|
|
25
|
+
content: 'text*',
|
|
26
|
+
group: 'block',
|
|
27
|
+
code: true,
|
|
28
|
+
defining: true,
|
|
29
|
+
marks: '',
|
|
30
|
+
attrs: { language: { default: '', validate: 'string' } },
|
|
31
|
+
parseDOM: [
|
|
32
|
+
{
|
|
33
|
+
tag: 'pre',
|
|
34
|
+
preserveWhitespace: 'full',
|
|
35
|
+
getAttrs: (node): CodeBlockAttrs => {
|
|
36
|
+
const language = extractLanguageFromElement(node)
|
|
37
|
+
|| extractLanguageFromElement(node.querySelector('code'))
|
|
38
|
+
return { language }
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
toDOM(node) {
|
|
43
|
+
const { language } = node.attrs as CodeBlockAttrs
|
|
44
|
+
return [
|
|
45
|
+
'pre',
|
|
46
|
+
{ 'data-language': language || undefined },
|
|
47
|
+
// `class: language-${language}` is used by remark-rehype to highlight the code block
|
|
48
|
+
['code', { class: language ? `language-${language}` : undefined }, 0],
|
|
49
|
+
]
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function extractLanguageFromElement(element: HTMLElement | null | undefined): string {
|
|
55
|
+
if (!element) {
|
|
56
|
+
return ''
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const attr = element.getAttribute('data-language')
|
|
60
|
+
if (attr) {
|
|
61
|
+
return attr
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const className = element.className
|
|
65
|
+
const match = className.match(/language-(\w+)/)
|
|
66
|
+
if (match) {
|
|
67
|
+
return match[1]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return ''
|
|
71
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
union,
|
|
3
|
+
type Union,
|
|
4
|
+
} from '@prosekit/core'
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
defineCodeBlockCommands,
|
|
8
|
+
type CodeBlockCommandsExtension,
|
|
9
|
+
} from './code-block-commands'
|
|
10
|
+
import {
|
|
11
|
+
defineCodeBlockEnterRule,
|
|
12
|
+
defineCodeBlockInputRule,
|
|
13
|
+
} from './code-block-input-rule'
|
|
14
|
+
import { defineCodeBlockKeymap } from './code-block-keymap'
|
|
15
|
+
import {
|
|
16
|
+
defineCodeBlockSpec,
|
|
17
|
+
type CodeBlockSpecExtension,
|
|
18
|
+
} from './code-block-spec'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export type CodeBlockExtension = Union<
|
|
24
|
+
[CodeBlockSpecExtension, CodeBlockCommandsExtension]
|
|
25
|
+
>
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Adds `codeBlock` nodes to the editor. This includes the following extensions:
|
|
29
|
+
*
|
|
30
|
+
* - {@link defineCodeBlockSpec}
|
|
31
|
+
* - {@link defineCodeBlockInputRule}
|
|
32
|
+
* - {@link defineCodeBlockEnterRule}
|
|
33
|
+
* - {@link defineCodeBlockKeymap}
|
|
34
|
+
* - {@link defineCodeBlockCommands}.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export function defineCodeBlock(): CodeBlockExtension {
|
|
39
|
+
return union(
|
|
40
|
+
defineCodeBlockSpec(),
|
|
41
|
+
defineCodeBlockInputRule(),
|
|
42
|
+
defineCodeBlockEnterRule(),
|
|
43
|
+
defineCodeBlockKeymap(),
|
|
44
|
+
defineCodeBlockCommands(),
|
|
45
|
+
)
|
|
46
|
+
}
|