@curvenote/schema
Version: 
Schema and markdown parser for @curvenote/editor
88 lines • 2.51 kB
JavaScript
import { MarkdownSerializer } from 'prosemirror-markdown';
import * as nodes from '../../nodes';
import { cleanWhitespaceChars } from '../clean';
function simpleInlineRender(state, node) {
    state.renderInline(node);
}
function simpleBlockRender(state, node) {
    state.renderInline(node);
    state.closeBlock(node);
}
function newLine(state) {
    state.ensureNewLine();
}
function nothing() {
    // pass
}
const noMarks = {
    open: '',
    close: '',
    mixable: true,
};
export const textSerializer = new MarkdownSerializer({
    text(state, node) {
        var _a;
        state.text(cleanWhitespaceChars((_a = node.text) !== null && _a !== void 0 ? _a : ''), false);
    },
    paragraph: simpleBlockRender,
    heading: simpleBlockRender,
    blockquote: simpleBlockRender,
    code_block: simpleBlockRender,
    link_block: simpleBlockRender,
    footnote: simpleBlockRender,
    horizontal_rule: newLine,
    hard_break: newLine,
    ordered_list(state, node) {
        const start = node.attrs.order || 1;
        const maxW = String(start + node.childCount - 1).length;
        const space = state.repeat(' ', maxW + 2);
        state.renderList(node, space, (i) => {
            const nStr = String(start + i);
            return `${state.repeat(' ', maxW - nStr.length) + nStr}. `;
        });
    },
    bullet_list(state, node) {
        state.renderList(node, '  ', () => `${node.attrs.bullet || '*'} `);
    },
    list_item(state, node) {
        state.renderInline(node);
    },
    // Presentational
    image: newLine,
    iframe: newLine,
    figure: simpleBlockRender,
    figcaption: simpleBlockRender,
    time: nodes.Time.toMarkdown,
    callout: simpleBlockRender,
    aside: simpleBlockRender,
    // Technical
    math: simpleInlineRender,
    equation: simpleBlockRender,
    cite: nodes.Cite.toMarkdown,
    cite_group: nodes.CiteGroup.toMarkdown,
    mention: nodes.Mention.toMarkdown,
    // Tables
    table: nodes.Table.toGFMMarkdownTable,
    // Dynamic
    variable: newLine,
    display: nodes.Display.toMarkdown,
    dynamic: nodes.Dynamic.toMarkdown,
    range: nothing,
    switch: nothing,
    button: nothing,
}, {
    em: noMarks,
    strong: noMarks,
    link: noMarks,
    code: noMarks,
    abbr: noMarks,
    subscript: noMarks,
    superscript: noMarks,
    strikethrough: noMarks,
    underline: noMarks,
});
export function toText(doc) {
    const md = textSerializer.serialize(doc, { tightLists: true });
    return md;
}
//# sourceMappingURL=index.js.map