MCPcopy Create free account
hub / github.com/AztecProtocol/aztec-packages / preprocessIncludeCode

Function preprocessIncludeCode

docs/src/preprocess/include_code.js:222–286  ·  view source on GitHub ↗
(markdownContent, filePath, rootDir)

Source from the content-addressed store, hash-verified

220 /^(?!<!--.*)(?=.*#include_code\s+(\S+)\s+(\S+)\s+(\S+)(?:[ ]+(\S+))?).*$/gm;
221
222async function preprocessIncludeCode(markdownContent, filePath, rootDir) {
223 // Process each include tag in the current markdown file
224 let updatedContent = markdownContent;
225 let matchesFound = false;
226 let match;
227 while ((match = regex.exec(markdownContent))) {
228 matchesFound = true;
229 const fullMatch = match[0];
230 const identifier = match[1];
231 let codeFilePath = match[2];
232 const language = match[3];
233 const opts = match[4] || "";
234
235 if (codeFilePath.slice(0) != "/") {
236 // Absolute path to the code file from the root of the Docusaurus project
237 // Note: without prefixing with `/`, the later call to `path.resolve()` gives an incorrect path (absolute instead of relative)
238 codeFilePath = `/${codeFilePath}`;
239 }
240
241 const noTitle = opts.includes("noTitle");
242 const noLineNumbers = opts.includes("noLineNumbers");
243 const noSourceLink = opts.includes("noSourceLink");
244
245 try {
246 const absCodeFilePath = path.join(rootDir, codeFilePath);
247
248 // Extract the code snippet between the specified comments
249 const [codeSnippet, startLine, endLine] = extractCodeSnippet(
250 absCodeFilePath,
251 identifier,
252 filePath
253 );
254
255 const relativeCodeFilePath = path
256 .resolve(rootDir, codeFilePath)
257 .replace(/^\//, "");
258 const urlText = `${relativeCodeFilePath}#L${startLine}-L${endLine}`;
259 const tag = process.env.COMMIT_TAG
260 ? `${process.env.COMMIT_TAG}`
261 : "next";
262 const url = `https://github.com/AztecProtocol/aztec-packages/blob/${tag}/${urlText}`;
263
264 const title = noTitle ? "" : `title="${identifier}"`;
265 const lineNumbers = noLineNumbers ? "" : "showLineNumbers";
266 const source = noSourceLink
267 ? ""
268 : `\n> <sup><sub><a href="${url}" target="_blank" rel="noopener noreferrer">Source code: ${urlText}</a></sub></sup>`;
269 const replacement =
270 language === "raw"
271 ? codeSnippet
272 : `\`\`\`${language} ${title} ${lineNumbers} \n${codeSnippet}\n\`\`\`${source}\n`;
273
274 // Replace the include tag with the code snippet
275 updatedContent = updatedContent.replace(fullMatch, replacement);
276 } catch (error) {
277 const lineNum = getLineNumberFromIndex(markdownContent, match.index);
278 // We were warning here, but code snippets were being broken. So making this throw an error instead:
279 throw new Error(

Callers

nothing calls this directly

Calls 7

joinMethod · 0.80
extractCodeSnippetFunction · 0.70
getLineNumberFromIndexFunction · 0.70
sliceMethod · 0.65
resolveMethod · 0.65
execMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected