Skip to content

Commit dd1e89a

Browse files
committed
Maps tags -> options
this allows users to provide tags on cells to have limited control of code behavior in Quarto (for example, when a cell is included in a document)
1 parent 6361153 commit dd1e89a

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

src/core/jupyter/jupyter.ts

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,18 +711,28 @@ export function jupyterCellWithOptions(
711711
}, {} as Record<string, unknown>);
712712

713713
// combine metadata options with yaml options (giving yaml options priority)
714-
const options = {
714+
const explicitOptions = {
715715
...metadataOptions,
716716
...yaml,
717717
};
718718

719719
// if we have layout or tbl-colwidths and it's not a string then json encode it
720720
[kLayout, kTblColwidths].forEach((option) => {
721-
if (options[option] && typeof (options[option]) !== "string") {
722-
options[option] = JSON.stringify(options[option]);
721+
if (
722+
explicitOptions[option] && typeof (explicitOptions[option]) !== "string"
723+
) {
724+
explicitOptions[option] = JSON.stringify(explicitOptions[option]);
723725
}
724726
});
725727

728+
// Resolve any tags that map to options
729+
const tags = cell.metadata.tags;
730+
const tagOptions = tagsToOptions(tags || []);
731+
const options = {
732+
...tagOptions,
733+
...explicitOptions,
734+
};
735+
726736
return {
727737
...cell,
728738
source,
@@ -835,6 +845,59 @@ export function mdEnsureTrailingNewline(source: string[]) {
835845
}
836846
}
837847

848+
// We decode some tags on cells into options for the cell
849+
// to better support control of Notebook code behavior when
850+
// included within documents (without requiring the addition of
851+
// configuration comments within the code cell)
852+
export const kHideCell = "hide-cell";
853+
export const kHideCode = "hide-code";
854+
export const kHideOutput = "hide-output";
855+
export const kHideWarnings = "hide-warnings";
856+
export const kShowCode = "show-code";
857+
export const kShowOutput = "show-output";
858+
export const kShowWarnings = "show-warnings";
859+
export const kRemoveCell = "remove-cell";
860+
const tagMapping: Record<string, Record<string, boolean>> = {
861+
[kHideCell]: {
862+
include: false,
863+
},
864+
[kHideCode]: {
865+
echo: false,
866+
},
867+
[kHideOutput]: {
868+
output: false,
869+
},
870+
[kHideWarnings]: {
871+
warning: false,
872+
},
873+
[kShowCode]: {
874+
echo: true,
875+
},
876+
[kShowOutput]: {
877+
output: true,
878+
},
879+
[kShowWarnings]: {
880+
warning: true,
881+
},
882+
[kRemoveCell]: {
883+
include: false,
884+
},
885+
};
886+
887+
function tagsToOptions(tags: string[]): Record<string, unknown> {
888+
const result: Record<string, unknown> = {};
889+
tags.forEach((tag) => {
890+
const mapping = tagMapping[tag];
891+
if (mapping) {
892+
const keys = Object.keys(mapping);
893+
keys.forEach((key) => {
894+
result[key] = mapping[key];
895+
});
896+
}
897+
});
898+
return result;
899+
}
900+
838901
function optionCommentPrefix(comment: string) {
839902
return comment + "| ";
840903
}

0 commit comments

Comments
 (0)