All files / src/pipeline/rfcxml inline.js

79.46% Statements 89/112
63.93% Branches 78/122
88.88% Functions 8/9
80% Lines 88/110

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193        5x 5x 5x   5x     42x       15x 15x   15x 3x 3x 3x     12x 1x 1x 1x     11x 1x 1x 1x     10x 1x 1x 1x 1x 1x 1x         9x 2x 2x 2x 2x 2x 2x         7x 7x 7x 2x   5x 5x     5x             27x     27x 27x 57x 57x   40x 40x     1x 1x   1x 1x   1x 1x   1x 1x                           2x 2x 2x     2x 2x   8x 8x   1x 1x 1x 1x 1x                               27x       27x     27x 27x           2x 1x   1x 1x 3x 1x           5x 1x   4x 4x 4x 15x 5x 5x 5x       4x     5x                  
/**
 * Convert markdown-it inline tokens (and plain markdown snippets) to RFCXML inline markup.
 */
 
const { escapeXml } = require('./xml-builder');
const { termToAnchor } = require('./term-anchor');
const { classifyCitation } = require('./bibliography');
 
const BCP14_RE = /\b(MUST NOT|SHALL NOT|SHOULD NOT|NOT RECOMMENDED|MUST|REQUIRED|SHALL|SHOULD|RECOMMENDED|MAY|OPTIONAL)\b/g;
 
function wrapBcp14(text) {
    return escapeXml(text).replace(BCP14_RE, '<bcp14>$1</bcp14>');
}
 
function renderTemplateToken(token, ctx) {
    const type = (token.info && token.info.type) || '';
    const args = (token.info && token.info.args) || [];
 
    if (type === 'ref') {
        const term = args[0] || '';
        const anchor = ctx.termAnchors.get(term.toLowerCase()) || termToAnchor(term);
        return `<xref target="${anchor}">${escapeXml(term)}</xref>`;
    }
 
    if (type === 'iref') {
        const term = args[0] || '';
        const anchor = ctx.termAnchors.get(term.toLowerCase()) || termToAnchor(term);
        return `<iref item="${escapeXml(term)}"/><xref target="${anchor}">${escapeXml(term)}</xref>`;
    }
 
    if (type === 'def') {
        const term = args[0] || '';
        const aliases = args.slice(1);
        return escapeXml(aliases[0] || term);
    }
 
    if (type === 'tref') {
        const term = args[1] || args[0] || '';
        const aliases = args.slice(2);
        const specKey = args[0];
        const label = escapeXml(aliases[0] || term);
        Eif (specKey && ctx.externalSpecAnchors && ctx.externalSpecAnchors.has(specKey)) {
            return `${label} <xref target="${ctx.externalSpecAnchors.get(specKey)}"/>`;
        }
        return label;
    }
 
    if (type === 'xref') {
        const specKey = args[0];
        const term = args[1] || '';
        const aliases = args.slice(2);
        const label = escapeXml(aliases[0] || term);
        Eif (specKey && ctx.externalSpecAnchors && ctx.externalSpecAnchors.has(specKey)) {
            return `<xref target="${ctx.externalSpecAnchors.get(specKey)}">${label}</xref>`;
        }
        return label;
    }
 
    Eif (/^spec/i.test(type)) {
        const name = args[0];
        if (!name) {
            return '';
        }
        const classified = classifyCitation(name);
        const anchor = (ctx.citations && ctx.citations.has(classified.anchor))
            ? classified.anchor
            : classified.anchor;
        return `<xref target="${anchor}"/>`;
    }
 
    return escapeXml(token.content || '');
}
 
function renderInlineTokens(tokens, ctx) {
    Iif (!tokens || tokens.length === 0) {
        return '';
    }
    let out = '';
    for (let i = 0; i < tokens.length; i++) {
        const token = tokens[i];
        switch (token.type) {
            case 'text':
                out += wrapBcp14(token.content);
                break;
            case 'softbreak':
            case 'hardbreak':
                out += '<br/>';
                break;
            case 'code_inline':
                out += `<tt>${escapeXml(token.content)}</tt>`;
                break;
            case 'strong_open':
                out += '<strong>';
                break;
            case 'strong_close':
                out += '</strong>';
                break;
            case 'em_open':
                out += '<em>';
                break;
            case 'em_close':
                out += '</em>';
                break;
            case 's_open':
                out += '<em>';
                break;
            case 's_close':
                out += '</em>';
                break;
            case 'link_open': {
                const href = token.attrGet ? token.attrGet('href') : (token.attrs || []).find(a => a[0] === 'href')?.[1];
                out += `<eref target="${escapeXml(href || '')}">`;
                break;
            }
            case 'link_close':
                out += '</eref>';
                break;
            case 'template':
                out += renderTemplateToken(token, ctx);
                break;
            case 'image': {
                const src = token.attrGet ? token.attrGet('src') : '';
                const alt = token.content || '';
                ctx.warnings.push(`Images are not native RFCXML artwork; emitting an eref for ${src || alt}`);
                out += `<eref target="${escapeXml(src || '')}">${escapeXml(alt || src)}</eref>`;
                break;
            }
            case 'html_inline':
                if (/^<br\s*\/?>$/i.test(token.content)) {
                    out += '<br/>';
                }
                break;
            default:
                if (token.children) {
                    out += renderInlineTokens(token.children, ctx);
                } else if (token.content && token.type === 'text') {
                    out += wrapBcp14(token.content);
                }
                break;
        }
    }
    return out;
}
 
function renderInlineToken(token, ctx) {
    Iif (!token) {
        return '';
    }
    Eif (token.type === 'inline') {
        return renderInlineTokens(token.children || [], ctx);
    }
    return renderInlineTokens([token], ctx);
}
 
function renderMarkdownInline(md, text, ctx) {
    if (!text || !String(text).trim()) {
        return '';
    }
    const tokens = md.parse(String(text), {});
    return tokens
        .filter(token => token.type === 'inline')
        .map(token => renderInlineToken(token, ctx))
        .filter(Boolean)
        .join(' ');
}
 
function renderParagraphsFromMarkdown(md, text, ctx) {
    if (!text || !String(text).trim()) {
        return [];
    }
    const tokens = md.parse(String(text), {});
    const paragraphs = [];
    for (const token of tokens) {
        if (token.type === 'inline' && token.children && token.children.length) {
            const inner = renderInlineToken(token, ctx);
            Eif (inner.trim()) {
                paragraphs.push(`<t>${inner}</t>`);
            }
        }
    }
    return paragraphs;
}
 
module.exports = {
    BCP14_RE,
    wrapBcp14,
    renderTemplateToken,
    renderInlineTokens,
    renderInlineToken,
    renderMarkdownInline,
    renderParagraphsFromMarkdown
};