decap-cms-widget-markdown 3.7.0 → 3.9.0

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.
@@ -39,13 +39,24 @@ export default class MarkdownControl extends React.Component {
39
39
  const preferredMode = localStorage.getItem(MODE_STORAGE_KEY) ?? 'rich_text';
40
40
  _getEditorComponents = props.getEditorComponents;
41
41
  this.state = {
42
- mode: this.getAllowedModes().indexOf(preferredMode) !== -1 ? preferredMode : this.getAllowedModes()[0],
42
+ mode:
43
+ // When used inside a container/shortcode editor component, default to
44
+ // raw mode — the widget type already implies the editing surface.
45
+ props.isEditorComponent ? 'raw' : this.getAllowedModes().indexOf(preferredMode) !== -1 ? preferredMode : this.getAllowedModes()[0],
43
46
  pendingFocus: false
44
47
  };
45
48
  }
46
49
  componentDidMount() {
47
50
  // Manually validate PropTypes - React 19 breaking change
48
51
  PropTypes.checkPropTypes(MarkdownControl.propTypes, this.props, 'prop', 'MarkdownControl');
52
+
53
+ // Ensure containerised widgets start in the correct mode even if the
54
+ // constructor ran before the prop was available (e.g. HMR / late prop).
55
+ if (this.props.isEditorComponent && this.state.mode !== 'raw') {
56
+ this.setState({
57
+ mode: 'raw'
58
+ });
59
+ }
49
60
  }
50
61
  handleMode = mode => {
51
62
  this.setState({
@@ -84,7 +95,8 @@ export default class MarkdownControl extends React.Component {
84
95
  mode,
85
96
  pendingFocus
86
97
  } = this.state;
87
- const isShowModeToggle = this.getAllowedModes().length > 1;
98
+ const isEditorComponent = this.props.isEditorComponent;
99
+ const isShowModeToggle = this.getAllowedModes().length > 1 && !isEditorComponent;
88
100
  const visualEditor = ___EmotionJSX("div", {
89
101
  className: "cms-editor-visual",
90
102
  ref: this.processRef
@@ -1,11 +1,34 @@
1
1
  // source: https://github.com/ianstormtaylor/slate/blob/main/site/examples/ts/paste-html.tsx
2
+ import DOMPurify from 'dompurify';
2
3
  import { jsx } from 'slate-hyperscript';
3
4
  import { Transforms } from 'slate';
5
+ function sanitizeElementUrl(url, {
6
+ allowDataImage = false
7
+ } = {}) {
8
+ if (!url) {
9
+ return null;
10
+ }
11
+ const trimmed = url.trim();
12
+ if (!trimmed) {
13
+ return null;
14
+ }
15
+ const normalized = trimmed.replace(/\s+/g, '').toLowerCase();
16
+ if (normalized.startsWith('javascript:') || normalized.startsWith('vbscript:') || normalized.startsWith('file:')) {
17
+ return null;
18
+ }
19
+ if (normalized.startsWith('data:')) {
20
+ return allowDataImage && /^data:image\/(?!svg\+xml)[a-z0-9.+-]+[;,]/i.test(normalized) ? trimmed : null;
21
+ }
22
+ return trimmed;
23
+ }
4
24
  const ELEMENT_TAGS = {
5
- A: el => ({
6
- type: 'link',
7
- url: el.getAttribute('href')
8
- }),
25
+ A: el => {
26
+ const url = sanitizeElementUrl(el.getAttribute('href'));
27
+ return url ? {
28
+ type: 'link',
29
+ url
30
+ } : undefined;
31
+ },
9
32
  BLOCKQUOTE: () => ({
10
33
  type: 'quote'
11
34
  }),
@@ -27,10 +50,15 @@ const ELEMENT_TAGS = {
27
50
  H6: () => ({
28
51
  type: 'heading-six'
29
52
  }),
30
- IMG: el => ({
31
- type: 'image',
32
- url: el.getAttribute('src')
33
- }),
53
+ IMG: el => {
54
+ const url = sanitizeElementUrl(el.getAttribute('src'), {
55
+ allowDataImage: true
56
+ });
57
+ return url ? {
58
+ type: 'image',
59
+ url
60
+ } : null;
61
+ },
34
62
  LI: () => ({
35
63
  type: 'list-item'
36
64
  }),
@@ -82,7 +110,7 @@ const INLINE_STYLES = {
82
110
  };
83
111
  function deserialize(el) {
84
112
  if (el.nodeType === 3) {
85
- return el.textContent;
113
+ return el.textContent.replace(/(\r)?\n/g, '');
86
114
  } else if (el.nodeType !== 1) {
87
115
  return null;
88
116
  } else if (el.nodeName === 'BR') {
@@ -106,6 +134,12 @@ function deserialize(el) {
106
134
  }
107
135
  if (ELEMENT_TAGS[nodeName]) {
108
136
  const attrs = ELEMENT_TAGS[nodeName](el);
137
+ if (attrs === undefined) {
138
+ return children;
139
+ }
140
+ if (attrs === null) {
141
+ return null;
142
+ }
109
143
  return jsx('element', attrs, children);
110
144
  }
111
145
  if (TEXT_TAGS[nodeName]) {
@@ -143,7 +177,8 @@ function withHtml(editor) {
143
177
  editor.insertData = data => {
144
178
  const html = data.getData('text/html');
145
179
  if (html) {
146
- const parsed = new DOMParser().parseFromString(html, 'text/html');
180
+ const sanitizedHtml = DOMPurify.sanitize(html);
181
+ const parsed = new DOMParser().parseFromString(sanitizedHtml, 'text/html');
147
182
  const fragment = deserialize(parsed.body);
148
183
  Transforms.insertFragment(editor, fragment);
149
184
  return;
@@ -1,7 +1,7 @@
1
1
  import { Transforms } from 'slate';
2
2
  import isCursorInEmptyParagraph from './locations/isCursorInEmptyParagraph';
3
3
  function insertShortcode(editor, pluginConfig) {
4
- const defaultValues = pluginConfig.fields.toMap().mapKeys((_, field) => field.get('name')).filter(field => field.has('default')).map(field => field.get('default'));
4
+ const defaultValues = pluginConfig.fields.toMap().mapKeys((_, field) => field.get('name')).map(field => field.get('default', ''));
5
5
  const nodeData = {
6
6
  type: 'shortcode',
7
7
  id: pluginConfig.id,