@curvenote/schema
Version:
Schema and markdown parser for @curvenote/editor
89 lines • 3.72 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.toTex = exports.toMarkdown = void 0;
const defaults_1 = require("../defaults");
const types_1 = require("./types");
const utils_1 = require("./utils");
const image = {
attrs: Object.assign(Object.assign({}, (0, utils_1.getNumberedDefaultAttrs)()), { src: {}, alt: { default: null }, title: { default: null }, width: { default: defaults_1.DEFAULT_IMAGE_WIDTH }, align: { default: 'center' }, caption: { default: false } }),
group: types_1.NodeGroups.block,
draggable: true,
parseDOM: [
{
tag: 'img[src]',
getAttrs(dom) {
var _a;
return Object.assign(Object.assign({}, (0, utils_1.getNumberedAttrs)(dom)), { src: dom.getAttribute('src'), title: dom.getAttribute('title'), alt: dom.getAttribute('alt'), align: (_a = dom.getAttribute('align')) !== null && _a !== void 0 ? _a : 'center', width: (0, utils_1.getImageWidth)(dom.getAttribute('width')), caption: (0, utils_1.readBooleanDomAttr)(dom, 'caption') });
},
},
],
toDOM(node) {
const { src, alt, title, width, align } = node.attrs;
return [
'img',
Object.assign(Object.assign({}, (0, utils_1.setNumberedAttrs)(node.attrs)), { // Deprecated, use figure
src,
align, alt: alt || undefined, title: title || undefined, width: `${width}%` }),
];
},
attrsFromMyst: (token) => ({
id: null,
label: null,
numbered: false,
src: token.url || '',
alt: token.alt || '',
title: token.title || '',
width: (0, utils_1.getImageWidth)(token.width),
align: token.align || 'center',
caption: false,
}),
toMyst: (props, opts) => {
var _a;
return ({
type: 'image',
url: ((_a = opts.localizeImageSrc) === null || _a === void 0 ? void 0 : _a.call(opts, props.src)) || props.src,
alt: props.alt || undefined,
title: props.title || undefined,
align: undefined,
width: `${(0, utils_1.getImageWidth)(props.width)}%` || undefined,
});
},
};
const toMarkdown = (state, node) => {
var _a, _b;
const src = ((_b = (_a = state.options).localizeImageSrc) === null || _b === void 0 ? void 0 : _b.call(_a, node.attrs.src)) || node.attrs.src;
const md = `}${node.attrs.title ? ` ${state.quote(node.attrs.title)}` : ''})`;
state.write(md);
state.closeBlock(node);
};
exports.toMarkdown = toMarkdown;
const toTex = (state, node) => {
var _a, _b;
const { width: nodeWidth } = node.attrs;
const src = ((_b = (_a = state.options).localizeImageSrc) === null || _b === void 0 ? void 0 : _b.call(_a, node.attrs.src)) || node.attrs.src;
const width = Math.round(nodeWidth !== null && nodeWidth !== void 0 ? nodeWidth : defaults_1.DEFAULT_IMAGE_WIDTH);
// let align = 'center';
// switch (nodeAlign?.toLowerCase()) {
// case 'left':
// align = 'flushleft';
// break;
// case 'right':
// align = 'flushright';
// break;
// default:
// break;
// }
// if (!caption) {
// const template = `
// \\begin{${align}}
// \\includegraphics[width=${width / 100}\\linewidth]{${src}}
// \\end{${align}}\n`;
// state.write(template);
// return;
// }
state.write(`\\includegraphics[width=${width / 100}\\linewidth]{${src}}`);
state.closeBlock(node);
};
exports.toTex = toTex;
exports.default = image;
//# sourceMappingURL=image.js.map
;