import { stripIndent } from 'common-tags'; import { get } from '../util/ajax.js'; const cached = {}; /** * Extracts the content between matching fragment markers in the text. * * Supported markers: * - ### [fragment] ... ### [fragment] * - /// [fragment] ... /// [fragment] * * @param {string} text - The input text that may contain embedded fragments. * @param {string} fragment - The fragment identifier to search for. * @param {boolean} fullLine - Boolean flag to enable full-line matching of fragment identifiers. * @returns {string} - The extracted and dedented content, or an empty string if not found. */ function extractFragmentContent(text, fragment, fullLine) { if (!fragment) { return text; } let fragmentRegex = `(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]`; const contentRegex = `[\\s\\S]*?`; if (fullLine) { // Match full line containing fragment identifier (e.g. /// [demo]) fragmentRegex = `.*${fragmentRegex}.*\n`; } const pattern = new RegExp( `(?:${fragmentRegex})(${contentRegex})(?:${fragmentRegex})`, ); // content is the capture group const match = text.match(pattern); return stripIndent((match || [])[1] || '').trim(); } function walkFetchEmbed({ embedTokens, compile, fetch }, cb) { let token; let step = 0; let count = 0; if (!embedTokens.length) { return cb({}); } while ((token = embedTokens[step++])) { const currentToken = token; // eslint-disable-next-line no-loop-func const next = text => { let embedToken; if (text) { if (currentToken.embed.type === 'markdown') { let path = currentToken.embed.url.split('/'); path.pop(); path = path.join('/'); // Resolves relative links to absolute text = text.replace(/\[([^[\]]+)\]\(([^)]+)\)/g, x => { const linkBeginIndex = x.indexOf('('); if (x.slice(linkBeginIndex, linkBeginIndex + 2) === '(.') { return ( x.substring(0, linkBeginIndex) + `(${window.location.protocol}//${window.location.host}${path}/` + x.substring(linkBeginIndex + 1, x.length - 1) + ')' ); } return x; }); // This may contain YAML front matter and will need to be stripped. const frontMatterInstalled = ($docsify.frontMatter || {}).installed || false; if (frontMatterInstalled === true) { text = $docsify.frontMatter.parseMarkdown(text); } if (currentToken.embed.fragment) { text = extractFragmentContent( text, currentToken.embed.fragment, currentToken.embed.omitFragmentLine, ); } embedToken = compile.lexer(text); } else if (currentToken.embed.type === 'code') { if (currentToken.embed.fragment) { text = extractFragmentContent( text, currentToken.embed.fragment, currentToken.embed.omitFragmentLine, ); } embedToken = compile.lexer( '```' + currentToken.embed.lang + '\n' + text.replace(/`/g, '@DOCSIFY_QM@') + '\n```\n', ); } else if (currentToken.embed.type === 'mermaid') { embedToken = [ { type: 'html', text: /* html */ `