Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Shortcodes aliasing native .md syntax are only matched in preview #1098

Open
bojidar-bg opened this issue Mar 27, 2024 · 0 comments
Open

Shortcodes aliasing native .md syntax are only matched in preview #1098

bojidar-bg opened this issue Mar 27, 2024 · 0 comments
Labels
bug Something isn't working markdown widgets
Milestone

Comments

@bojidar-bg
Copy link

Not sure if this is a bug or just a massive misuse of the feature on my part, but if I define a shortcode like so:

CMS.registerShortcode('shortcodename', {
  label: 'My shortcode',
  openTag: '<', // <--- !
  closeTag: '>',
  separator: ' ',
  toProps: () => ({}),
  toArgs: () => ['/'],
  control: () => {
    return h('div', {className: 'red-rectangle'}, [])
  },
  preview: () => {
    return h('div', {className: 'red-rectangle'}, [])
  }
  // ...
});

And use it on a document like so:

<shortcodename />

Then the shortcode will match in the preview, as evidenced by the preview component being visible in the result, but will remain visible as raw HTML in the widget.

As far as I can tell, the dissonance comes from the fact that the control first parses the value as markdown before applying shortcodes:

unified()
.use(markdown)
.use(gfm)
// eslint-disable-next-line @typescript-eslint/no-empty-function
.use(useMdx ? mdx : () => {})
.use(toSlatePlugin({ shortcodeConfigs: shortcodeConfigs ?? getShortcodes(), useMdx }))
.process(markdownValue, callback);

While the preview first replaces shortcodes (into MDX) and then parses markdown:
const [state, setValue] = useMdx(`editor-${id}.mdx`, value ?? '');
const [prevValue, setPrevValue] = useState<string | null>(null);
useEffect(() => {
if (prevValue !== value) {
const parsedValue = processShortcodeConfigToMdx(getShortcodes(), value ?? '');
setPrevValue(parsedValue);
setValue(parsedValue);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
return (
<div key="markdown-preview" className={classes.root}>
<MDXProvider components={components}>
<MdxComponent state={state} />{' '}

(Though I suppose for my particular issue, a call to processShortcodeConfigsToSlate around here would suffice:)

case 'html':
if (node.value?.includes('<br>')) {
return {
break: true,
type: NodeTypes.paragraph,
children: [{ text: node.value?.replace(/<br>/g, '') || '' }],
} as ParagraphNode;
}
return { type: 'p', children: [{ text: node.value ?? '' }] };

@KaneFreeman KaneFreeman added this to the 4.x milestone Apr 26, 2024
@KaneFreeman KaneFreeman added bug Something isn't working widgets markdown labels Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working markdown widgets
Projects
None yet
Development

No branches or pull requests

2 participants