From 7a9eacb7177b0cf100de02cda3bbf6913892fc19 Mon Sep 17 00:00:00 2001 From: Sam Watkins Date: Tue, 14 Mar 2023 03:07:02 +1100 Subject: [PATCH] Fix issue 3152 - convert nbdev directives to YAML --- src/core/jupyter/jupyter.ts | 5 ++++- src/core/lib/partition-cell-options.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/core/jupyter/jupyter.ts b/src/core/jupyter/jupyter.ts index 4a1d0496326..f81e398d2a8 100644 --- a/src/core/jupyter/jupyter.ts +++ b/src/core/jupyter/jupyter.ts @@ -375,7 +375,8 @@ export async function quartoMdToJupyter( } delete yaml[key]; } else { - if (!kJupyterCellOptionKeys.includes(key)) { + // an option value of null means that it is an nbdev-style directive + if (!kJupyterCellOptionKeys.includes(key) && yaml[key] !== null) { cell.metadata[key] = yaml[key]; delete yaml[key]; } @@ -844,6 +845,8 @@ export function jupyterCellOptionsAsComment( }); const commentChars = langCommentChars(language); const yamlOutput = mdTrimEmptyLines(lines(cellYaml)).map((line) => { + // an option value of null means that the key is a nbdev-style directive + line = line.replace(/: null$/, ""); line = optionCommentPrefix(commentChars[0]) + line + optionCommentSuffix(commentChars[1]); return line + "\n"; diff --git a/src/core/lib/partition-cell-options.ts b/src/core/lib/partition-cell-options.ts index 9418a466493..bf85eebc3b9 100644 --- a/src/core/lib/partition-cell-options.ts +++ b/src/core/lib/partition-cell-options.ts @@ -64,6 +64,7 @@ export function partitionCellOptions( yamlOption += line[line.length - 1]; } } + yamlOption = fixNbdevDirective(yamlOption); yamlLines.push(yamlOption); optionsSource.push(line); continue; @@ -96,6 +97,16 @@ export function partitionCellOptions( }; } +function fixNbdevDirective(line: string) { + // convert non-yaml nbdev-style directives to yaml keys with a null value + if (line.match(/^\w+\s*$/)) { // e.g. export + return line.replace(/(\s*)$/, ": null$1"); + } else if (line.match(/^\w+\s+[^:\s]/)) { // e.g. default_exp foo + return line.replace(/(\s*)$/, ": null$1"); + } + return line; +}; + export async function parseAndValidateCellOptions( mappedYaml: MappedString, language: string,