diff --git a/nbconvert/nbconvertapp.py b/nbconvert/nbconvertapp.py index cd305afbf..d3de5d143 100755 --- a/nbconvert/nbconvertapp.py +++ b/nbconvert/nbconvertapp.py @@ -118,6 +118,18 @@ def validate(self, obj, value): """Clear output of current file and save in place, overwriting the existing notebook. """, ), + "clear-metadata": ( + { + "NbConvertApp": { + "use_output_suffix": False, + "export_format": "notebook", + }, + "FilesWriter": {"build_directory": ""}, + "ClearMetadataPreprocessor": {"enabled": True}, + }, + """Clear metadata of current file and save in place, + overwriting the existing notebook. """, + ), "coalesce-streams": ( { "NbConvertApp": {"use_output_suffix": False, "export_format": "notebook"}, diff --git a/nbconvert/preprocessors/clearmetadata.py b/nbconvert/preprocessors/clearmetadata.py index 609a01653..4996840c3 100644 --- a/nbconvert/preprocessors/clearmetadata.py +++ b/nbconvert/preprocessors/clearmetadata.py @@ -1,4 +1,4 @@ -"""Module containing a preprocessor that removes metadata from code cells""" +"""Module containing a preprocessor that removes notebook or cell metadata""" # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. @@ -10,7 +10,7 @@ class ClearMetadataPreprocessor(Preprocessor): """ - Removes all the metadata from all code cells in a notebook. + Removes global or cell-level metadata from a notebook. """ clear_cell_metadata = Bool( @@ -24,16 +24,16 @@ class ClearMetadataPreprocessor(Preprocessor): preserve_nb_metadata_mask = Set( [("language_info", "name")], help=( - "Indicates the key paths to preserve when deleting metadata " - "across both cells and notebook metadata fields. Tuples of " - "keys can be passed to preserved specific nested values" + "Indicates the key paths to preserve when deleting notebook " + "metadata fields. Tuples of keys can be passed to preserve " + "specific nested values." ), ).tag(config=True) preserve_cell_metadata_mask = Set( help=( "Indicates the key paths to preserve when deleting metadata " - "across both cells and notebook metadata fields. Tuples of " - "keys can be passed to preserved specific nested values" + "across all cells in a notebook. Tuples of keys can be passed " + "to preserve specific nested values." ) ).tag(config=True) @@ -73,14 +73,13 @@ def nested_filter(self, items, mask): def preprocess_cell(self, cell, resources, cell_index): """ - All the code cells are returned with an empty metadata field. + All the cells are returned with a trimmed down metadata field. """ - if self.clear_cell_metadata and cell.cell_type == "code": # noqa: SIM102 + if self.clear_cell_metadata and "metadata" in cell: # Remove metadata - if "metadata" in cell: - cell.metadata = dict( - self.nested_filter(cell.metadata.items(), self.preserve_cell_metadata_mask) - ) + cell.metadata = dict( + self.nested_filter(cell.metadata.items(), self.preserve_cell_metadata_mask) + ) return cell, resources def preprocess(self, nb, resources):