@podlite/toc
Version:
'Table of contents' plugin for Podlite markup language
118 lines (116 loc) • 5.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginRegister = exports.plugin = exports.getContentForToc = void 0;
const schema_1 = require("@podlite/schema");
const helpers_1 = require("./helpers");
const getContentForToc = (node) => {
if (typeof node !== 'string' && 'type' in node) {
if (node.type === 'block') {
const conf = (0, schema_1.makeAttrs)(node, {});
if ((0, schema_1.isNamedBlock)(node.name)) {
const caption = ((conf, nodeName) => {
if (conf.exists('caption')) {
return conf.getFirstValue('caption');
}
else if (conf.exists('title')) {
return conf.getFirstValue('title');
}
else {
// try to find content child node
const [captionNode] = (0, schema_1.getFromTree)(node, 'caption');
if (captionNode) {
return (0, schema_1.getTextContentFromNode)(captionNode);
}
}
return `${nodeName} not have :caption`;
})(conf, node.name);
return caption;
}
if (node.name == 'image') {
const caption = (0, schema_1.getTextContentFromNode)(conf.getFirstValue('caption'));
return caption || 'image not have caption';
}
if (node.name == 'table') {
const caption = (0, schema_1.getTextContentFromNode)(conf.getFirstValue('caption'));
return caption || 'table not have :caption';
}
if (node.type === 'block' && node.name === 'item') {
if (Array.isArray(node.content) && node.content.length > 0) {
return (0, schema_1.getTextContentFromNode)(node.content[0]);
}
}
const caption = ((conf, nodeName) => {
if (conf.exists('caption')) {
return (0, schema_1.getTextContentFromNode)(conf.getFirstValue('caption'));
}
else if (conf.exists('title')) {
return (0, schema_1.getTextContentFromNode)(conf.getFirstValue('title'));
}
else {
// try to find content child node
const [captionNode] = (0, schema_1.getFromTree)(node, 'caption');
if (captionNode) {
return (0, schema_1.getTextContentFromNode)(captionNode);
}
return null;
}
})(conf, node.name);
if (caption) {
return caption;
}
return (0, schema_1.getTextContentFromNode)(node);
}
}
return 'Not supported toc element';
};
exports.getContentForToc = getContentForToc;
/*
*/
exports.plugin = {
toAstAfter: (writer, processor, fulltree) => {
return (node, ctx) => {
const content = (0, schema_1.getTextContentFromNode)(node);
const blocks = content
.trim()
.split(/(?:\s*,\s*|\s+)/)
.filter(Boolean);
if (blocks.length == 0) {
blocks.push({ name: 'head' });
}
const nodes = (0, schema_1.getFromTree)(fulltree, ...blocks);
const tocTree = (0, helpers_1.prepareDataForToc)(nodes);
const createList = (items, level) => {
const resultList = [];
items.map(item => {
const { level, node, content } = item;
// create new node for each item
const text = (0, exports.getContentForToc)(node) || ' '; // ' ' needs to avoid lack of L<>
//TODO: 1. getNodeId should use ctx of node, but using {} instead
//TODO: 2. refactor linking for blocks
const para = (0, schema_1.mkNode)({
type: 'para',
content: [(0, schema_1.mkFomattingCodeL)({ meta: `#${(0, schema_1.getNodeId)(node, {})}` }, [text])],
});
const tocNode = para;
resultList.push((0, schema_1.mkTocItem)(tocNode));
if (Array.isArray(content) && content.length > 0) {
resultList.push(createList(content, level + 1));
}
});
return (0, schema_1.mkTocList)(resultList, level);
};
const conf = (0, schema_1.makeAttrs)(node, ctx);
const tocTitle = conf.getFirstValue('caption') || conf.getFirstValue('title');
const makeToc = (tocTree, title) => {
return (0, schema_1.mkToc)(createList(tocTree.content, 1), title, node.location);
};
return makeToc(tocTree, tocTitle);
};
},
};
exports.PluginRegister = {
Toc: exports.plugin,
toc: exports.plugin,
};
exports.default = exports.plugin;
//# sourceMappingURL=index.js.map