Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions docs/src/preprocess/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,28 @@ function useLastRelease() {
function readFile(filePath, tag) {
if (tag && tag !== "master") {
try {
const tag = getLatestTag();
const root = path.resolve(__dirname, "../../../");
const relPath = path.relative(root, filePath);
return childProcess.execSync(`git show ${tag}:${relPath}`).toString();
} catch (err) {
console.error(
`Error reading file ${filePath} from latest version. Falling back to current content.`
`Error reading file ${filePath} from version ${tag}. Falling back to current content.`
);
}
}
return fs.readFileSync(filePath, "utf-8");
}

/** Extracts a code snippet, trying with the last release if applicable, and falling back to current content. */
function extractCodeSnippet(filePath, identifier) {
function extractCodeSnippet(filePath, identifier, requesterFile) {
if (useLastRelease()) {
try {
return doExtractCodeSnippet(filePath, identifier, false);
} catch (err) {
console.error(
`Error extracting code snippet ${identifier} for ${filePath}: ${err}. Falling back to current content.`
`Error extracting code snippet ${identifier} from ${path.basename(
filePath
)} requested by ${requesterFile}: ${err}. Falling back to current content.`
);
}
}
Expand Down Expand Up @@ -286,7 +287,11 @@ async function preprocessIncludeCode(markdownContent, filePath, rootDir) {
const absCodeFilePath = path.join(rootDir, codeFilePath);

// Extract the code snippet between the specified comments
const extracted = extractCodeSnippet(absCodeFilePath, identifier);
const extracted = extractCodeSnippet(
absCodeFilePath,
identifier,
filePath
);
const [codeSnippet, startLine, endLine, tag] = extracted;

const relativeCodeFilePath = path.resolve(rootDir, codeFilePath);
Expand All @@ -297,9 +302,13 @@ async function preprocessIncludeCode(markdownContent, filePath, rootDir) {

const title = noTitle ? "" : `title="${identifier}"`;
const lineNumbers = noLineNumbers ? "" : "showLineNumbers";
const warn =
useLastRelease() && (!tag || tag === "master")
? `<br/>This example references unreleased code. Code from released packages may be different. Use with care.`
: "";
const source = noSourceLink
? ""
: `\n> [<sup><sub>Source code: ${urlText}</sub></sup>](${url})`;
: `\n> <sup><sub><a href="${url}" target="_blank" rel="noopener noreferrer">Source code: ${urlText}</a>${warn}</sub></sup>`;
const replacement =
language === "raw"
? codeSnippet
Expand Down