Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tutorials] Fixes for LaTeX and cell source #3401

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions scripts/convert_ipynb_to_mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ def sanitize_mdx(mdx: str) -> str:
mdx = re.sub("([^\\\\])([{}])", "\\g<1>\\\\\\g<2>", mdx)

# -- KaTeX --
# Wrap '\begin{}...\end{}' in $$ for KaTeX to work.
mdx = re.sub(
"(\\\\begin\\\\{(\\w*?)\\\\}(.|\n)*?end\\\\{\\2\\\\})", "$$\\g<1>$$", mdx
)
# Make sure $$ symbols are not escaped and include line breaks.
mdx = re.sub(
"\\\\?\\$\\\\?\\$((?:.|\n)*?)\\\\?\\$\\\\?\\$", "\n$$\n\\g<1>\n$$\n", mdx
Expand All @@ -341,6 +337,25 @@ def sanitize_mdx(mdx: str) -> str:
return mdx


def get_source(cell: NotebookNode) -> str:
"""
Extract the source code from a Jupyter notebook cell. The source is
characteristically multi-line strings but may be stored as a list of strings, in
which case we join them together.
https://ipython.readthedocs.io/en/3.x/notebook/nbformat.html

Args:
cell (NotebookNode): A Jupyter notebook cell object.

Returns:
str: The source code as a string.
"""
source = cell.get("source", "")
if isinstance(source, list):
return "".join(source)
return source


def handle_markdown_cell(
cell: NotebookNode,
new_img_dir: Path,
Expand All @@ -358,7 +373,7 @@ def handle_markdown_cell(
Returns:
str: Transformed Markdown object suitable for inclusion in MDX.
"""
markdown = cell["source"]
markdown = get_source(cell)

# Update image paths in the Markdown and copy them to the Markdown tutorials folder.
# Skip - Our images are base64 encoded, so we don't need to copy them to the docs
Expand Down Expand Up @@ -392,7 +407,7 @@ def handle_cell_input(cell: NotebookNode, language: str) -> str:
Returns:
str: Code block formatted Markdown string.
"""
cell_source = cell.get("source", "")
cell_source = get_source(cell)
return f"```{language}\n{cell_source}\n```\n\n"


Expand Down