import { get } from '../fetch/ajax'; import { merge } from '../util/core'; import stripIndent from 'strip-indent'; const cached = {}; function walkFetchEmbed({ embedTokens, compile, fetch }, cb) { let token; let step = 0; let count = 1; if (!embedTokens.length) { return cb({}); } while ((token = embedTokens[step++])) { const next = (function(token) { return text => { let embedToken; if (text) { if (token.embed.type === 'markdown') { let path = token.embed.url.split('/'); path.pop(); path = path.join('/'); // Resolves relative links to absolute text = text.replace(/\[([^[\]]+)\]\(([^)]+)\)/g, x => { const linkBeginIndex = x.indexOf('('); if (x.substring(linkBeginIndex).startsWith('(.')) { return ( x.substring(0, linkBeginIndex) + `(${window.location.protocol}//${window.location.host}${path}/` + x.substring(linkBeginIndex + 1, x.length - 1) + ')' ); } return x; }); embedToken = compile.lexer(text); } else if (token.embed.type === 'code') { if (token.embed.fragment) { const fragment = token.embed.fragment; const pattern = new RegExp( `(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]([\\s\\S]*)(?:###|\\/\\/\\/)\\s*\\[${fragment}\\]` ); text = stripIndent((text.match(pattern) || [])[1] || '').trim(); } embedToken = compile.lexer( '```' + token.embed.lang + '\n' + text.replace(/`/g, '@DOCSIFY_QM@') + '\n```\n' ); } else if (token.embed.type === 'mermaid') { embedToken = [ { type: 'html', text: `