@curvenote/schema
Version:
Schema and markdown parser for @curvenote/editor
170 lines • 4.48 kB
JavaScript
import { MarkGroups } from './nodes/types';
export const link = {
attrs: {
href: {},
title: { default: null },
kind: { default: 'external' },
},
inclusive: false,
parseDOM: [
{
tag: 'a[href]',
getAttrs(dom) {
return {
href: dom.getAttribute('href'),
title: dom.getAttribute('title'),
kind: dom.getAttribute('kind') || 'external',
};
},
},
],
toDOM(node) {
const { href, title, kind } = node.attrs;
return ['a', { href, title, kind }, 0];
},
attrsFromMyst(token) {
return { href: token.url, title: token.title };
},
toMyst: (props, opts) => {
var _a, _b;
return ({
type: 'link',
url: (_b = (_a = opts.localizeLink) === null || _a === void 0 ? void 0 : _a.call(opts, props.href)) !== null && _b !== void 0 ? _b : props.href,
title: props.title || undefined,
children: (props.children || []),
});
},
};
export const code = {
attrs: {},
parseDOM: [{ tag: 'code' }],
toDOM() {
return ['code', 0];
},
excludes: MarkGroups.format,
attrsFromMyst: () => ({}),
toMyst: (props) => {
var _a;
if (((_a = props.children) === null || _a === void 0 ? void 0 : _a.length) === 1 && props.children[0].type === 'text') {
return { type: 'inlineCode', value: props.children[0].value || '' };
}
throw new Error(`Code node does not have one child`);
},
};
export const em = {
attrs: {},
parseDOM: [{ tag: 'i' }, { tag: 'em' }, { style: 'font-style=italic' }],
toDOM() {
return ['em', 0];
},
group: MarkGroups.format,
attrsFromMyst: () => ({}),
toMyst: (props) => ({
type: 'emphasis',
children: (props.children || []),
}),
};
export const strong = {
attrs: {},
parseDOM: [
{ tag: 'strong' },
// This works around a Google Docs misbehavior where
// pasted content will be inexplicably wrapped in `<b>`
// tags with a font-weight normal.
{ tag: 'b', getAttrs: (node) => node.style.fontWeight !== 'normal' && null },
{
style: 'font-weight',
getAttrs: (value) => /^(bold(er)?|[5-9]\d{2,})$/.test(value) && null,
},
],
toDOM() {
return ['strong', 0];
},
group: MarkGroups.format,
attrsFromMyst: () => ({}),
toMyst: (props) => ({
type: 'strong',
children: (props.children || []),
}),
};
export const superscript = {
attrs: {},
toDOM() {
return ['sup', 0];
},
parseDOM: [{ tag: 'sup' }],
excludes: 'subscript',
group: MarkGroups.format,
attrsFromMyst: () => ({}),
toMyst: (props) => ({
type: 'superscript',
children: (props.children || []),
}),
};
export const subscript = {
attrs: {},
toDOM() {
return ['sub', 0];
},
parseDOM: [{ tag: 'sub' }],
excludes: 'superscript',
group: MarkGroups.format,
attrsFromMyst: () => ({}),
toMyst: (props) => ({
type: 'subscript',
children: (props.children || []),
}),
};
export const strikethrough = {
attrs: {},
toDOM() {
return ['s', 0];
},
parseDOM: [{ tag: 's' }],
group: MarkGroups.format,
attrsFromMyst: () => ({}),
toMyst: (props) => ({
type: 'delete',
children: (props.children || []),
}),
};
export const underline = {
attrs: {},
toDOM() {
return ['u', 0];
},
parseDOM: [{ tag: 'u' }],
group: MarkGroups.format,
attrsFromMyst: () => ({}),
toMyst: (props) => ({
type: 'underline',
children: (props.children || []),
}),
};
export const abbr = {
attrs: {
title: { default: '' },
},
inclusive: false,
parseDOM: [
{
tag: 'abbr',
getAttrs(dom) {
return { title: dom.getAttribute('title') };
},
},
],
toDOM(node) {
const { title } = node.attrs;
return ['abbr', { title }, 0];
},
attrsFromMyst(token) {
return { title: token.title };
},
toMyst: (props) => ({
type: 'abbreviation',
title: props.title || undefined,
children: (props.children || []),
}),
};
//# sourceMappingURL=marks.js.map