@portabletext/plugin-markdown-shortcuts 8.0.21 → 8.0.23

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 (2) hide show
  1. package/README.md +53 -25
  2. package/package.json +12 -12
package/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # `@portabletext/plugin-markdown-shortcuts`
2
2
 
3
- > ⬇️ Adds helpful Markdown shortcuts to the editor
3
+ > Add helpful Markdown shortcuts to the editor
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @portabletext/plugin-markdown-shortcuts
9
+ ```
10
+
11
+ ## Usage
4
12
 
5
13
  Import the `MarkdownShortcutsPlugin` React component and place it inside the `EditorProvider` and tell it about your schema:
6
14
 
@@ -14,7 +22,7 @@ import {MarkdownShortcutsPlugin} from '@portabletext/plugin-markdown-shortcuts'
14
22
 
15
23
  const schemaDefinition = defineSchema({
16
24
  blockObjects: [{name: 'break'}],
17
- annotations: [{name: 'link', fields: [{name: 'href', type: 'string'}]}]
25
+ annotations: [{name: 'link', fields: [{name: 'href', type: 'string'}]}],
18
26
  decorators: [
19
27
  {name: 'em'},
20
28
  {name: 'code'},
@@ -43,35 +51,36 @@ function App() {
43
51
  >
44
52
  <PortableTextEditable />
45
53
  <MarkdownShortcutsPlugin
46
- boldDecorator={({schema}) =>
47
- schema.decorators.find((d) => d.name === 'strong')?.name
54
+ boldDecorator={({context}) =>
55
+ context.schema.decorators.find((d) => d.name === 'strong')?.name
48
56
  }
49
- codeDecorator={({schema}) =>
50
- schema.decorators.find((d) => d.name === 'code')?.name
57
+ codeDecorator={({context}) =>
58
+ context.schema.decorators.find((d) => d.name === 'code')?.name
51
59
  }
52
- italicDecorator={({schema}) =>
53
- schema.decorators.find((d) => d.name === 'em')?.name
60
+ italicDecorator={({context}) =>
61
+ context.schema.decorators.find((d) => d.name === 'em')?.name
54
62
  }
55
- strikeThroughDecorator={({schema}) =>
56
- schema.decorators.find((d) => d.name === 'strike-through')?.name
63
+ strikeThroughDecorator={({context}) =>
64
+ context.schema.decorators.find((d) => d.name === 'strike-through')
65
+ ?.name
57
66
  }
58
- defaultStyle={({schema}) =>
59
- schema.styles.find((s) => s.name === 'normal')?.name
67
+ defaultStyle={({context}) =>
68
+ context.schema.styles.find((s) => s.name === 'normal')?.name
60
69
  }
61
- headingStyle={({schema, level}) =>
62
- schema.styles.find((s) => s.name === `h${level}`)?.name
70
+ headingStyle={({context, props}) =>
71
+ context.schema.styles.find((s) => s.name === `h${props.level}`)?.name
63
72
  }
64
- blockquoteStyle={({schema}) =>
65
- schema.styles.find((s) => s.name === 'blockquote')?.name
73
+ blockquoteStyle={({context}) =>
74
+ context.schema.styles.find((s) => s.name === 'blockquote')?.name
66
75
  }
67
- orderedList={({schema}) =>
68
- schema.lists.find((s) => s.name === 'number')?.name
76
+ orderedList={({context}) =>
77
+ context.schema.lists.find((s) => s.name === 'number')?.name
69
78
  }
70
- unorderedList={({schema}) =>
71
- schema.lists.find((s) => s.name === 'bullet')?.name
79
+ unorderedList={({context}) =>
80
+ context.schema.lists.find((s) => s.name === 'bullet')?.name
72
81
  }
73
- horizontalRuleObject={({schema}) => {
74
- const schemaType = schema.blockObjects.find(
82
+ horizontalRuleObject={({context}) => {
83
+ const schemaType = context.schema.blockObjects.find(
75
84
  (object) => object.name === 'break',
76
85
  )
77
86
 
@@ -81,8 +90,8 @@ function App() {
81
90
 
82
91
  return {_type: schemaType.name}
83
92
  }}
84
- linkObject={({schema, href}) => {
85
- const schemaType = schema.annotations.find(
93
+ linkObject={({context, props}) => {
94
+ const schemaType = context.schema.annotations.find(
86
95
  (annotation) => annotation.name === 'link',
87
96
  )
88
97
  const hrefField = schemaType?.fields.find(
@@ -95,7 +104,7 @@ function App() {
95
104
 
96
105
  return {
97
106
  _type: schemaType.name,
98
- [hrefField.name]: href,
107
+ [hrefField.name]: props.href,
99
108
  }
100
109
  }}
101
110
  />
@@ -103,3 +112,22 @@ function App() {
103
112
  )
104
113
  }
105
114
  ```
115
+
116
+ ## Why look up the type in the schema?
117
+
118
+ Each callback returns the type name as found in `context.schema` (for
119
+ example `context.schema.decorators.find((d) => d.name === 'strong')?.name`)
120
+ instead of a hardcoded `'strong'`. That lookup is what makes the plugin
121
+ schema-aware: when the schema does not define the type, the lookup returns
122
+ `undefined` and the shortcut is skipped, so the plugin never inserts a
123
+ decorator, style, or object the schema does not declare. Hardcoding the
124
+ string would fire the shortcut regardless and produce content the schema
125
+ forbids.
126
+
127
+ The same gating follows [containers](../schema/README.md#containers-and-sub-schemas),
128
+ because `context.schema` is the schema resolved at the caret, not always the
129
+ top-level one. Inside a code block whose sub-schema declares no decorators, a
130
+ `boldDecorator` that looks up `strong` returns `undefined`, so the shortcut
131
+ is skipped there too. Point each callback at `context.schema` rather than
132
+ capturing the top-level schema and the shortcuts stay correct at every
133
+ nesting level.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@portabletext/plugin-markdown-shortcuts",
3
- "version": "8.0.21",
4
- "description": "Adds helpful Markdown shortcuts to the editor",
3
+ "version": "8.0.23",
4
+ "description": "Add helpful Markdown shortcuts to the editor",
5
5
  "keywords": [
6
6
  "portabletext",
7
7
  "plugin",
@@ -32,28 +32,28 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@portabletext/plugin-character-pair-decorator": "^8.0.21",
36
- "@portabletext/plugin-input-rule": "^5.0.21"
35
+ "@portabletext/plugin-character-pair-decorator": "^8.0.23",
36
+ "@portabletext/plugin-input-rule": "^5.0.23"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@sanity/tsconfig": "^2.1.0",
40
- "@types/react": "^19.2.14",
40
+ "@types/react": "^19.2.17",
41
41
  "@vitejs/plugin-react": "^5.2.0",
42
- "@vitest/browser": "^4.1.8",
43
- "@vitest/browser-playwright": "^4.1.8",
42
+ "@vitest/browser": "^4.1.9",
43
+ "@vitest/browser-playwright": "^4.1.9",
44
44
  "babel-plugin-react-compiler": "^1.0.0",
45
45
  "eslint": "^9.39.1",
46
46
  "eslint-plugin-react-hooks": "^7.1.1",
47
- "react": "^19.2.5",
47
+ "react": "^19.2.7",
48
48
  "typescript": "5.9.3",
49
49
  "typescript-eslint": "^8.48.0",
50
- "vitest": "^4.1.8",
51
- "@portabletext/editor": "^7.6.2",
52
- "racejar": "2.0.8"
50
+ "vitest": "^4.1.9",
51
+ "@portabletext/editor": "^7.8.0",
52
+ "racejar": "2.0.9"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "react": "^19.2",
56
- "@portabletext/editor": "^7.6.2"
56
+ "@portabletext/editor": "^7.8.0"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">=20.19 <22 || >=22.12"