diff --git a/jupytext/metadata_filter.py b/jupytext/metadata_filter.py index c4d80cb8a..fecb28ab8 100644 --- a/jupytext/metadata_filter.py +++ b/jupytext/metadata_filter.py @@ -92,6 +92,18 @@ def update_metadata_filters(metadata, jupyter_md, cell_metadata): cell_metadata = {'additional': cell_metadata, 'excluded': 'all'} metadata.setdefault('jupytext', {})['notebook_metadata_filter'] = '-all' metadata.setdefault('jupytext', {})['cell_metadata_filter'] = metadata_filter_as_string(cell_metadata) + else: + # Update the notebook metadata filter to include existing entries 376 + nb_md_filter = metadata.get('jupytext', {}).get('notebook_metadata_filter', '').split(',') + nb_md_filter = [key for key in nb_md_filter if key] + if 'all' in nb_md_filter or '-all' in nb_md_filter: + return + for key in metadata: + if key in _DEFAULT_NOTEBOOK_METADATA or key in nb_md_filter or ('-' + key) in nb_md_filter: + continue + nb_md_filter.append(key) + if nb_md_filter: + metadata.setdefault('jupytext', {})['notebook_metadata_filter'] = ','.join(nb_md_filter) def apply_metadata_filters(user_filter, default_filter, actual_keys): diff --git a/tests/test_read_simple_markdown.py b/tests/test_read_simple_markdown.py index 8e9c38e46..711206b6f 100644 --- a/tests/test_read_simple_markdown.py +++ b/tests/test_read_simple_markdown.py @@ -595,9 +595,9 @@ def test_two_markdown_cell_with_no_language_code_works(nb=new_notebook(cells=[ def test_notebook_with_python3_magic(no_jupytext_version_number, nb=new_notebook(metadata={ 'kernelspec': {'display_name': 'Python 3', 'language': 'python', - 'name': 'python3'}}, - cells=[new_code_cell('%%python2\na = 1\nprint a'), - new_code_cell('%%python3\nb = 2\nprint(b)')]), + 'name': 'python3'}}, cells=[ + new_code_cell('%%python2\na = 1\nprint a'), + new_code_cell('%%python3\nb = 2\nprint(b)')]), text="""--- jupyter: kernelspec: @@ -621,3 +621,59 @@ def test_notebook_with_python3_magic(no_jupytext_version_number, nb2 = jupytext.reads(md, 'md') compare_notebooks(nb2, nb) + + +def test_update_metadata_filter( + no_jupytext_version_number, + org="""--- +jupyter: + kernelspec: + display_name: Python 3 + language: python + name: python3 + extra: + key: value +--- +""", target="""--- +jupyter: + extra: + key: value + jupytext: + notebook_metadata_filter: extra + kernelspec: + display_name: Python 3 + language: python + name: python3 +--- +"""): + nb = jupytext.reads(org, 'md') + text = jupytext.writes(nb, 'md') + compare(text, target) + + +def test_update_metadata_filter_2( + no_jupytext_version_number, + org="""--- +jupyter: + jupytext: + notebook_metadata_filter: -extra + kernelspec: + display_name: Python 3 + language: python + name: python3 + extra: + key: value +--- +""", target="""--- +jupyter: + jupytext: + notebook_metadata_filter: -extra + kernelspec: + display_name: Python 3 + language: python + name: python3 +--- +"""): + nb = jupytext.reads(org, 'md') + text = jupytext.writes(nb, 'md') + compare(text, target)