All files / src/pipeline/rfcxml terminology.js

93.82% Statements 76/81
80% Branches 40/50
100% Functions 13/13
93.67% Lines 74/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162        4x 4x 4x   4x     6x   14x         4x 1x   3x 3x     3x 3x 3x     3x     3x             5x 5x   5x 51x 51x 46x   5x 5x       5x 2x 2x 2x   3x 3x   5x       5x 5x 5x 5x   5x 10x 10x 4x   6x 3x 3x 3x 3x         6x     5x 5x 2x     5x 5x             5x         5x       4x 4x 3x 6x     4x       2x 1x         3x 1x   2x 3x 3x 3x 3x 3x 1x 1x     1x     3x 3x   2x       2x     4x                    
/**
 * Collect Spec-Up-T [[def:]] / [[tref:]] glossary entries and emit RFC 9940-style RFCXML <dl>.
 */
 
const { el, escapeXml } = require('./xml-builder');
const { termToAnchor, displayTermLabel, slugifyTerm } = require('./term-anchor');
const { renderParagraphsFromMarkdown } = require('./inline');
 
const DEF_LINE_RE = /^\[\[(def|tref):\s*([^\]]+)\]\]\s*$/i;
 
function parseArgs(raw) {
    return String(raw)
        .split(/\s*,\s*/)
        .map(part => part.trim())
        .filter(Boolean);
}
 
function lookupTrefDefinition(xtrefs, externalSpec, term) {
    if (!xtrefs) {
        return '';
    }
    const list = Array.isArray(xtrefs) ? xtrefs : xtrefs.xtrefs;
    Iif (!Array.isArray(list)) {
        return '';
    }
    const key = slugifyTerm(term);
    const found = list.find(entry =>
        entry.externalSpec === externalSpec &&
        slugifyTerm(entry.term) === key
    );
    Iif (!found || !found.content) {
        return '';
    }
    return String(found.content)
        .replace(/<[^>]+>/g, ' ')
        .replace(/\s+/g, ' ')
        .trim();
}
 
function collectTermsFromMarkdown(markdown, options = {}) {
    const lines = String(markdown || '').split('\n');
    const terms = [];
 
    for (let i = 0; i < lines.length; i++) {
        const match = lines[i].match(DEF_LINE_RE);
        if (!match) {
            continue;
        }
        const type = match[1].toLowerCase();
        const args = parseArgs(match[2]);
        let term;
        let aliases;
        let externalSpec;
        if (type === 'tref') {
            externalSpec = args[0];
            term = args[1];
            aliases = args.slice(2);
        } else {
            term = args[0];
            aliases = args.slice(1);
        }
        Iif (!term) {
            continue;
        }
 
        const defLines = [];
        let j = i + 1;
        while (j < lines.length && lines[j].trim() === '') {
            j++;
        }
        while (j < lines.length) {
            const line = lines[j];
            if (DEF_LINE_RE.test(line) || /^#{1,6}\s/.test(line)) {
                break;
            }
            if (line.startsWith('~ ')) {
                defLines.push(line.slice(2));
            } else if (line.trim() === '') {
                Eif (defLines.length) {
                    defLines.push('');
                }
            } else E{
                break;
            }
            j++;
        }
 
        let definitionMarkdown = defLines.join('\n').trim();
        if (!definitionMarkdown && type === 'tref') {
            definitionMarkdown = lookupTrefDefinition(options.xtrefs, externalSpec, term);
        }
 
        const aliasesLower = aliases.map(alias => alias.toLowerCase());
        terms.push({
            type,
            term,
            aliases,
            externalSpec,
            definitionMarkdown,
            anchor: termToAnchor(term),
            aliasAnchors: aliases.map(alias => termToAnchor(alias)),
            keys: [term.toLowerCase(), ...aliasesLower]
        });
    }
 
    return terms;
}
 
function buildTermAnchorMap(terms) {
    const map = new Map();
    for (const term of terms) {
        for (const key of term.keys) {
            map.set(key, term.anchor);
        }
    }
    return map;
}
 
function sortTerms(terms) {
    return [...terms].sort((a, b) =>
        displayTermLabel(a.term, a.aliases).localeCompare(displayTermLabel(b.term, b.aliases), undefined, { sensitivity: 'base' })
    );
}
 
function renderTerminologyDl(terms, md, ctx) {
    if (!terms.length) {
        return '';
    }
    const items = sortTerms(terms).map(term => {
        const label = displayTermLabel(term.term, term.aliases);
        const dt = el('dt', { anchor: term.anchor }, escapeXml(label));
        const paragraphs = renderParagraphsFromMarkdown(md, term.definitionMarkdown, ctx);
        let ddInner = paragraphs.length ? paragraphs.join('\n') : '<t>No definition provided.</t>';
        if (term.type === 'tref' && term.externalSpec && ctx.externalSpecAnchors?.has(term.externalSpec)) {
            const cite = `<t>Source: <xref target="${ctx.externalSpecAnchors.get(term.externalSpec)}"/>.</t>`;
            Iif (!term.definitionMarkdown) {
                ddInner = cite;
            } else {
                ddInner = `${ddInner}\n${cite}`;
            }
        }
        const dd = el('dd', ddInner);
        return `${dt}\n${dd}`;
    });
    return el('dl', { newline: 'true', spacing: 'normal' }, items);
}
 
function isTermDefinitionLine(line) {
    return DEF_LINE_RE.test(String(line || '').trim());
}
 
module.exports = {
    DEF_LINE_RE,
    parseArgs,
    lookupTrefDefinition,
    collectTermsFromMarkdown,
    buildTermAnchorMap,
    sortTerms,
    renderTerminologyDl,
    isTermDefinitionLine
};