Skip to content

Commit

Permalink
Merge pull request #9306 from quarto-dev/feature/inspect-improve-code…
Browse files Browse the repository at this point in the history
…-cell-tracking

inspect - track multiple inclusions
  • Loading branch information
cscheid authored Apr 8, 2024
2 parents 3784b6b + a031ac8 commit fda08e9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/core/lib/break-quarto-md-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Shortcode } from "./parse-shortcode-types.ts";
import { MappedString } from "./text-types.ts";

export interface CodeCellType {
language: string;
language: string; // I'd like to say "string but not '_directive'" but I don't know how
}

export interface DirectiveCell {
Expand Down
72 changes: 50 additions & 22 deletions src/project/project-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ import { RenderContext, RenderFlags } from "../command/render/types.ts";
import { LanguageCellHandlerOptions } from "../core/handlers/types.ts";
import { ExecutionEngine } from "../execute/types.ts";
import { InspectedMdCell } from "../quarto-core/inspect-types.ts";
import { breakQuartoMd } from "../core/lib/break-quarto-md.ts";
import { breakQuartoMd, QuartoMdCell } from "../core/lib/break-quarto-md.ts";
import { partitionCellOptionsText } from "../core/lib/partition-cell-options.ts";
import { parse } from "yaml/mod.ts";
import { mappedIndexToLineCol } from "../core/lib/mapped-text.ts";
import { normalizeNewlines } from "../core/lib/text.ts";
import { DirectiveCell } from "../core/lib/break-quarto-md-types.ts";

export function projectExcludeDirs(context: ProjectContext): string[] {
const outputDir = projectOutputDir(context);
Expand Down Expand Up @@ -368,30 +369,57 @@ export async function projectResolveCodeCellsForFile(
markdown = await mdForFile(project, engine, file);
}

const chunks = await breakQuartoMd(markdown);
const result: InspectedMdCell[] = [];
for (const cell of chunks.cells) {
if (
typeof cell.cell_type !== "string" &&
cell.cell_type.language !== "_directive"
) {
const cellOptions = partitionCellOptionsText(
cell.cell_type.language,
cell.sourceWithYaml ?? cell.source,
const fileStack: string[] = [];

const inner = async (file: string, cells: QuartoMdCell[]) => {
if (fileStack.includes(file)) {
throw new Error(
"Circular include detected:\n " + fileStack.join(" ->\n "),
);
const metadata = cellOptions.yaml
? parse(cellOptions.yaml.value) as Record<string, unknown>
: {};
const lineLocator = mappedIndexToLineCol(cell.sourceVerbatim);
result.push({
start: lineLocator(0).line,
end: lineLocator(cell.sourceVerbatim.value.length - 1).line,
source: normalizeNewlines(cell.source.value),
language: cell.cell_type.language,
metadata,
});
}
}
fileStack.push(file);
for (const cell of cells) {
if (typeof cell.cell_type === "string") {
continue;
}
if (cell.cell_type.language === "_directive") {
const directiveCell = cell.cell_type as DirectiveCell;
if (directiveCell.name !== "include") {
continue;
}
const innerFile = join(project.dir, directiveCell.shortcode.params[0]);
await inner(
innerFile,
(await breakQuartoMd(
await mdForFile(project, engine, innerFile),
)).cells,
);
}
if (
cell.cell_type.language !== "_directive"
) {
const cellOptions = partitionCellOptionsText(
cell.cell_type.language,
cell.sourceWithYaml ?? cell.source,
);
const metadata = cellOptions.yaml
? parse(cellOptions.yaml.value) as Record<string, unknown>
: {};
const lineLocator = mappedIndexToLineCol(cell.sourceVerbatim);
result.push({
start: lineLocator(0).line,
end: lineLocator(cell.sourceVerbatim.value.length - 1).line,
file: file,
source: normalizeNewlines(cell.source.value),
language: cell.cell_type.language,
metadata,
});
}
}
fileStack.pop();
};
await inner(file, (await breakQuartoMd(markdown)).cells);
cache.codeCells = result;
return result;
}
Expand Down
1 change: 1 addition & 0 deletions src/quarto-core/inspect-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
export type InspectedMdCell = {
start: number;
end: number;
file: string;
source: string;
language: string;
metadata: Record<string, unknown>;
Expand Down
2 changes: 2 additions & 0 deletions tests/docs/websites/issue-9253/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ This is a Quarto website.

To learn more about Quarto websites visit <https://quarto.org/docs/websites>.

{{< include _include.qmd >}}

{{< include _include.qmd >}}

0 comments on commit fda08e9

Please sign in to comment.