Skip to content

Commit

Permalink
convert: write kernel & jupytext metadata into qmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Aug 31, 2021
1 parent aff5a7d commit 1cf8c7f
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/command/convert/jupyter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,29 @@ export function jupyterNotebookToMarkdown(file: string, includeIds: boolean) {
// join into source
const mdSource = md.join("");

// add jupyter kernelspec to front-matter
if (frontMatter) {
const yaml = readYamlFromMarkdown(frontMatter);
yaml.jupyter = notebook.metadata;
const yamlText = stringify(yaml, {
indent: 2,
sortKeys: false,
skipInvalid: true,
});
return `---\n${yamlText}---\n\n${mdSource}`;
} else {
return mdSource;
}
// read any yaml front matter defined in a 'raw' cell
const yaml: Metadata = frontMatter ? readYamlFromMarkdown(frontMatter) : {};

// forward qmd-relevant notebook metadata. known metadata we exclude:
// - toc: Jupyter UI specific metadata
// - language_info: Jupyter UI specific metadata
// - widgets: Output artifacts that don't belong in qmd source
// for jupytext we provide a full kernelspec + jupytext metadata,
// otherwise we provide an abbreviated spec w/ just the kernel name
yaml.jupyter = notebook.metadata.jupytext
? {
jupytext: notebook.metadata.jupytext,
kernelspec: notebook.metadata.kernelspec,
}
: notebook.metadata.kernelspec.name;

// return yaml + markdown
const yamlText = stringify(yaml, {
indent: 2,
sortKeys: false,
skipInvalid: true,
});
return `---\n${yamlText}---\n\n${mdSource}`;
}

function mdFromCodeCell(
Expand Down

0 comments on commit 1cf8c7f

Please sign in to comment.