UNPKG

@curvenote/schema

Version:

Schema and markdown parser for @curvenote/editor

158 lines 7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.countWords = exports.countState = void 0; const wordcount_1 = require("@wordpress/wordcount"); const types_1 = require("../nodes/types"); const types_2 = require("../types"); const text_1 = require("../serialize/text"); const utils_1 = require("../utils"); function push(counter, attrs, title, meta) { const number = attrs.numbered ? counter.total + 1 : null; counter.all.push({ id: attrs.id || null, kind: counter.kind, label: attrs.label || null, title, number, numbered: attrs.numbered, meta, }); if (number) { counter.total += 1; } } const NO_CAPTION = { numbered: false, id: null, label: null }; function countState(state) { const counts = { [types_1.ReferenceKind.sec]: { kind: types_1.ReferenceKind.sec, total: 0, all: [] }, [types_1.ReferenceKind.fig]: { kind: types_1.ReferenceKind.fig, total: 0, all: [] }, [types_1.ReferenceKind.eq]: { kind: types_1.ReferenceKind.eq, total: 0, all: [] }, [types_1.ReferenceKind.code]: { kind: types_1.ReferenceKind.code, total: 0, all: [] }, [types_1.ReferenceKind.table]: { kind: types_1.ReferenceKind.table, total: 0, all: [] }, [types_1.ReferenceKind.link]: { kind: types_1.ReferenceKind.link, total: 0, all: [] }, [types_1.ReferenceKind.cite]: { kind: types_1.ReferenceKind.cite, total: 0, all: [] }, }; let tableCounted = false; state.doc.content.descendants((node) => { switch (node.type.name) { case types_2.nodeNames.cite: { const { kind, key } = node.attrs; if (kind === types_1.ReferenceKind.cite) { const attrs = node.attrs; push(counts.cite, { numbered: true, id: null, label: null }, attrs.title || '', { key, }); } return false; } case types_2.nodeNames.image: { const { src, alt } = node.attrs; push(counts.fig, NO_CAPTION, alt, { src, alt }); return false; } case types_2.nodeNames.code_block: { const { title, language } = node.attrs; const code = (0, text_1.toText)(node); push(counts.code, NO_CAPTION, title, { code, language }); return false; } case types_2.nodeNames.equation: { const attrs = node.attrs; const { title } = attrs; const math = (0, text_1.toText)(node); push(counts.eq, attrs, title, { math }); return false; } case types_2.nodeNames.heading: { const attrs = node.attrs; const { level } = node.attrs; const title = (0, text_1.toText)(node); push(counts.sec, attrs, title, { level, section: '' }); return true; } case types_2.nodeNames.table: { if (!tableCounted) push(counts.table, NO_CAPTION, '', {}); tableCounted = false; // There are children of tables to be counted return true; } // Continue to search case types_2.nodeNames.figure: { const child = (0, utils_1.findChildrenWithName)(node, [ types_2.nodeNames.image, types_2.nodeNames.iframe, types_2.nodeNames.table, types_2.nodeNames.code_block, types_2.nodeNames.equation, ])[0]; if (!child) return false; const caption = (0, utils_1.findChildrenWithName)(node, types_2.nodeNames.figcaption)[0]; const { numbered: isNumbered, id, label } = node.attrs; const numbered = caption && isNumbered; const captionText = (caption === null || caption === void 0 ? void 0 : caption.node) ? (0, text_1.toText)(caption.node) : ''; switch (child.node.type.name) { case types_2.nodeNames.image: { const { alt, src } = child.node.attrs; push(counts.fig, { numbered, id, label }, captionText !== null && captionText !== void 0 ? captionText : alt, { src, alt, }); return false; } case types_2.nodeNames.iframe: { const { src } = child.node.attrs; push(counts.fig, { numbered, id, label }, captionText, { src, alt: '', }); return false; } case types_2.nodeNames.table: { push(counts.table, { numbered, id, label }, captionText, {}); // There are children of tables to be counted tableCounted = true; return true; } case types_2.nodeNames.equation: { const math = (0, text_1.toText)(child.node); push(counts.eq, { numbered, id, label }, captionText, { math }); // There are children of tables to be counted return false; } case types_2.nodeNames.code_block: { const { title, language } = child.node.attrs; const code = (0, text_1.toText)(child.node); push(counts.code, { numbered, id, label }, captionText !== null && captionText !== void 0 ? captionText : title, { code, language, }); // There are children of tables to be counted return false; } default: break; } return false; } default: return true; } }); return counts; } exports.countState = countState; function countWords(state) { const text = (0, text_1.toText)(state.doc); const words = (0, wordcount_1.count)(text, 'words'); const characters_excluding_spaces = (0, wordcount_1.count)(text, 'characters_excluding_spaces'); const characters_including_spaces = (0, wordcount_1.count)(text, 'characters_including_spaces'); return { words, characters_excluding_spaces, characters_including_spaces, }; } exports.countWords = countWords; //# sourceMappingURL=countState.js.map