UNPKG

@curvenote/schema

Version:

Schema and markdown parser for @curvenote/editor

82 lines 2.33 kB
import { NodeGroups } from './types'; const LINK_BLOCK_CLASS = 'link-block'; const link_block = { attrs: { url: { default: '' }, title: { default: '' }, description: { default: '' }, }, group: NodeGroups.top, content: `${NodeGroups.text}*`, selectable: true, draggable: true, atom: true, isolating: true, parseDOM: [ { tag: `div.${LINK_BLOCK_CLASS}`, getAttrs(dom) { const attrs = { url: dom.getAttribute('data-url') || null, title: dom.getAttribute('title') || '', description: dom.textContent || '', }; return attrs; }, }, ], toDOM(node) { const { title, description, url } = node.attrs; return [ 'div', { 'data-url': url || undefined, title, class: LINK_BLOCK_CLASS, }, description, ]; }, attrsFromMyst: (token) => { let description = ''; if (token.children.length && token.children[0].type === 'text') { description = token.children[0].value; } return { url: token.url, title: token.title || '', description, }; }, toMyst: (props) => { return { type: 'linkBlock', url: props['data-url'], title: props.title || undefined, children: (props.children || []), }; }, }; export const toMarkdown = (state, node) => { state.ensureNewLine(); state.write(`\`\`\`{link-block} ${node.attrs.url}\n`); if (node.attrs.title) state.write(`:title: ${node.attrs.title}\n`); if (node.attrs.thumbnail) state.write(`:thumbnail: ${node.attrs.thumbnail}\n`); if (node.attrs.description) state.write(`${node.attrs.description}\n`); state.ensureNewLine(); state.write('```'); state.closeBlock(node); }; export const toTex = (state, node) => { if (node.attrs.title) { state.write(`\\href{${node.attrs.url}}{${node.attrs.title}}`); } else { state.write(`\\url{${node.attrs.url}}`); } }; export default link_block; //# sourceMappingURL=linkBlock.js.map