rittenhop-dev/versions/5.94.2/node_modules/@tryghost/kg-converters/es/kg-converters.js.map

1 line
47 KiB
Plaintext
Raw Normal View History

2024-09-23 19:40:12 -04:00
{"version":3,"file":"kg-converters.js","sources":["../lib/lexical-to-mobiledoc.js","../lib/mobiledoc-to-lexical.js"],"sourcesContent":["const MOBILEDOC_VERSION = '0.3.1';\nconst GHOST_VERSION = '4.0';\n\nconst BLANK_DOC = {\n version: MOBILEDOC_VERSION,\n ghostVersion: GHOST_VERSION,\n markups: [],\n atoms: [],\n cards: [],\n sections: [\n [1, 'p', [\n [0, [], 0, '']\n ]]\n ]\n};\n\nconst MD_TEXT_SECTION = 1;\nconst MD_LIST_SECTION = 3;\nconst MD_CARD_SECTION = 10;\n\nconst MD_TEXT_MARKER = 0;\nconst MD_ATOM_MARKER = 1;\n\nconst L_IS_BOLD = 1;\nconst L_IS_ITALIC = 1 << 1;\nconst L_IS_STRIKETHROUGH = 1 << 2;\nconst L_IS_UNDERLINE = 1 << 3;\nconst L_IS_CODE = 1 << 4;\nconst L_IS_SUBSCRIPT = 1 << 5;\nconst L_IS_SUPERSCRIPT = 1 << 6;\n\nconst L_FORMAT_MAP = new Map([\n [L_IS_BOLD, 'strong'],\n [L_IS_ITALIC, 'em'],\n [L_IS_STRIKETHROUGH, 's'],\n [L_IS_UNDERLINE, 'u'],\n [L_IS_CODE, 'code'],\n [L_IS_SUBSCRIPT, 'sub'],\n [L_IS_SUPERSCRIPT, 'sup']\n]);\n\nconst HEADING_TYPES = ['heading', 'extended-heading'];\nconst TEXT_TYPES = ['text', 'extended-text'];\n\n// TODO: Feels a little too explicit as it will need updating every time we add a new card.\n//\n// One alternative is to use a list of all built-in Lexical types and assume that anything\n// not listed is a card but that feels more dangerous.\n//\n// Another alternative is to grab the list of cards from kg-default-nodes but that's creating\n// more inter-dependencies that makes development setup tricky.\nconst KNOWN_CARDS = [\n 'audio',\n 'bookmark',\n 'button',\n 'callout',\n 'codeblock',\n 'email-cta',\n 'email',\n 'embed',\n 'file',\n 'gallery',\n 'header',\n 'horizontalrule',\n 'html',\n 'image',\n 'markdown',\n 'paywall',\n 'product',\n 'signup',\n 'toggle',\n 'video'\n];\n\nconst CARD_NAME_MAP = {\n codeblock: 'code',\n horizontalrule: 'hr'\n};\n\nconst CARD_PROPERTY_MAP = {\n embed: {\n embedType: 'type'\n }\n};\n\nexport function lexicalToMobiledoc(serializedLexical) {\n if (serializedLexical === null || serializedLexical === undefined || serializedLexical === '') {\n return JSON.stringify(BLANK_DOC);\n }\n\n const lexical = JSON.parse(serializedLexical);\n\n if (!lexical.root) {\n return JSON.stringify(BLANK_DOC);\n }\n\n const mobiledoc = buildEmptyDoc();\n\n lexical.root.children.forEach(child => addRootChild(child, mobiledoc));\n\n return JSON.stringify(mobiledoc);\n}\n\n/* internal functions ------------------------------------------------------- */\n\nfunction buildEmptyDoc() {\n return {\n version: MOBILEDOC_VERSION,\n ghostVersion: GHOST_VERSION,\n atoms: [],\n cards: [],\n markups: [],\n sections: []\n };\n}\n\nfunction getOrSetMarkupIndex(markup, mobiledoc) {\n let index = mobiledoc.markups.findIndex(m => m[0] === markup);\n\n if (index === -1) {\n mobiledoc.markups.push([markup]);\n index = mobiledoc.markups.length - 1;\n }\n\n return index;\n}\n\nfunction getOrSetAtomIndex(atom, mobiledoc) {\n let index = mobiledoc.atoms.findIndex(m => m[0] === atom);\n\n if (index === -1) {\n mobiledoc.atoms.push(atom);\n index = mobiledoc.atoms.length - 1;\n }\n\n return index;\n}\n\nfunction addRootChild(child, mobiledoc) {\n if (child.type === 'paragraph') {\n addTextSection(child, mobiledoc);\n }\n\n if (HEADING_TYPES.includes(child.type)) {\n addTextSection(child, mobiledoc, child.tag);\n }\n\n if (child.type === 'quote') {\n addTextSection(child, mobiledoc, 'blockquote');\n }\n\n if (child.type === 'aside') {\n addTextSection(child, mobiledoc, 'aside');\n }\n\n if (child.type === 'list') {\n addListSection(child, mobiledoc, child.tag);\n }\n\n if (KNOWN_CARDS.includes(child.type)) {\n addCardSection(child, mobiledoc);\n }\n}\n\nfunction addTextSection(childWithFormats, mobiledoc, tagName = 'p') {\n const