Supports bi-directional import/export between MDX <=> Lexical. JSX will be mapped to lexical blocks back and forth. This will allow editing our mdx docs in payload while keeping mdx as the source of truth --------- Co-authored-by: Germán Jabloñski <43938777+GermanJablo@users.noreply.github.com>
37 lines
998 B
TypeScript
37 lines
998 B
TypeScript
import type { BlockJSX } from 'payload'
|
|
|
|
export const codeConverter: BlockJSX = {
|
|
customStartRegex: /^[ \t]*```(\w+)?/,
|
|
customEndRegex: {
|
|
optional: true,
|
|
regExp: /[ \t]*```$/,
|
|
},
|
|
doNotTrimChildren: true,
|
|
import: ({ openMatch, children, closeMatch }) => {
|
|
const language = openMatch[1]
|
|
|
|
const isSingleLineAndComplete =
|
|
!!closeMatch && !children.includes('\n') && openMatch.input?.trim() !== '```' + language
|
|
|
|
if (isSingleLineAndComplete) {
|
|
return {
|
|
language: '',
|
|
code: language + (children?.length ? children : ''), // No need to add space to children as they are not trimmed
|
|
}
|
|
}
|
|
|
|
return {
|
|
language,
|
|
code: children,
|
|
}
|
|
},
|
|
export: ({ fields }) => {
|
|
const isSingleLine = !fields.code.includes('\n') && !fields.language?.length
|
|
if (isSingleLine) {
|
|
return '```' + fields.code + '```'
|
|
}
|
|
|
|
return '```' + (fields.language || '') + (fields.code ? '\n' + fields.code : '') + '\n' + '```'
|
|
},
|
|
}
|