Skip to content

Commit bbcc4d8

Browse files
authored
fix: whitespace discrepancies on round trip conversions (#6521)
## 📝 Summary Fixes slight discrepancies between frontend provided serialization and markdown -> marimo serialization Unblocks mwouts/jupytext#1431
1 parent 1daa887 commit bbcc4d8

22 files changed

+275
-166
lines changed

frontend/src/core/codemirror/language/languages/markdown.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ export class MarkdownLanguageAdapter
103103
code: string,
104104
metadata: MarkdownLanguageAdapterMetadata,
105105
): [string, number] {
106+
// NB. Must be kept consistent with marimo/_convert/utils.py
107+
// ::markdown_to_marimo
108+
106109
// Empty string
107110
if (code === "") {
108111
// Need at least a space, otherwise the output will be 6 quotes

marimo/_convert/markdown/markdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _tree_to_ir(root: Element) -> SafeWrap[NotebookSerializationV1]:
247247
header = root.get("header", None)
248248
pyproject = root.get("pyproject", None)
249249
if pyproject and not header:
250-
header = "\n# ".join(["# ///script", *pyproject.splitlines(), "///"])
250+
header = "\n# ".join(["# /// script", *pyproject.splitlines(), "///"])
251251
notebook = NotebookSerializationV1(
252252
app=AppInstantiation(options=app_config),
253253
cells=[

marimo/_convert/utils.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@
55

66

77
def markdown_to_marimo(source: str) -> str:
8+
# NB. This should be kept in sync with the logic in
9+
# frontend/src/core/codemirror/language/languages/markdown.ts
10+
# ::transformOut
811
source = source.replace('"""', '\\"\\"\\"')
12+
13+
# 6 quotes in a row breaks
14+
if not source:
15+
source = " "
16+
17+
if "\n" not in source:
18+
return f'mo.md(r"""{source}""")'
19+
920
return "\n".join(
1021
[
1122
"mo.md(",
1223
# r-string: a backslash is just a backslash!
1324
codegen.indent_text('r"""'),
14-
codegen.indent_text(source),
15-
codegen.indent_text('"""'),
25+
source,
26+
'"""',
1627
")",
1728
]
1829
)

marimo/_server/export/exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def export_as_md(
291291
metadata["header"] = header.strip()
292292
else:
293293
header_file = previous if previous else filename
294-
if header_file:
294+
if header_file and Path(header_file).exists():
295295
with open(header_file, encoding="utf-8") as f:
296296
_metadata, _ = extract_frontmatter(f.read())
297297
metadata.update(_metadata)

tests/_cli/snapshots/markdown_to_marimo.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ app = marimo.App()
88
def _(mo):
99
mo.md(
1010
r"""
11-
# Test Markdown
11+
# Test Markdown
1212

13-
print('Hello from Markdown!')
14-
"""
13+
print('Hello from Markdown!')
14+
"""
1515
)
1616
return
1717

tests/_cli/snapshots/remote_markdown_to_marimo.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ app = marimo.App()
88
def _(mo):
99
mo.md(
1010
r"""
11-
# Remote Markdown Test
11+
# Remote Markdown Test
1212

13-
```python
14-
print('Hello from Remote Markdown!')
15-
```
16-
"""
13+
```python
14+
print('Hello from Remote Markdown!')
15+
```
16+
"""
1717
)
1818
return
1919

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import marimo
2+
3+
__generated_with = "0.15.2"
4+
app = marimo.App(width="medium")
5+
6+
7+
@app.cell(hide_code=True)
8+
def _(mo):
9+
mo.md(r"""This is a simple marimo notebook""")
10+
return
11+
12+
13+
@app.cell
14+
def _():
15+
x = 1
16+
return (x,)
17+
18+
19+
@app.cell
20+
def _(x):
21+
y = x+1
22+
y
23+
return
24+
25+
26+
@app.cell
27+
def _():
28+
import marimo as mo
29+
return (mo,)
30+
31+
32+
if __name__ == "__main__":
33+
app.run()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import marimo
2+
3+
__generated_with = "0.0.0"
4+
app = marimo.App(width="medium", app_title="Test Notebook")
5+
6+
7+
@app.cell(hide_code=True)
8+
def _(mo):
9+
mo.md(r"""This is a simple marimo notebook""")
10+
return
11+
12+
13+
@app.cell
14+
def _():
15+
x = 1
16+
return (x,)
17+
18+
19+
@app.cell
20+
def _(x):
21+
y = x+1
22+
y
23+
return
24+
25+
26+
@app.cell
27+
def _():
28+
import marimo as mo
29+
return (mo,)
30+
31+
32+
if __name__ == "__main__":
33+
app.run()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Test Notebook
3+
marimo-version: 0.0.0
4+
width: medium
5+
---
6+
7+
This is a simple marimo notebook
8+
9+
```python {.marimo}
10+
x = 1
11+
```
12+
13+
```python {.marimo}
14+
y = x+1
15+
y
16+
```
17+
18+
```python {.marimo}
19+
import marimo as mo
20+
```

tests/_convert/snapshots/convert_hides_markdown_cells.py.txt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,21 @@ app = marimo.App()
55

66
@app.cell(hide_code=True)
77
def _(mo):
8-
mo.md(
9-
r"""
10-
A markdown cell.
11-
"""
12-
)
8+
mo.md(r"""A markdown cell.""")
139
return
1410

1511

1612
@app.cell(hide_code=True)
1713
def _(mo):
1814
# Cell tags: blah
19-
mo.md(
20-
r"""
21-
A markdown cell with tags: ['blah'].
22-
"""
23-
)
15+
mo.md(r"""A markdown cell with tags: ['blah'].""")
2416
return
2517

2618

2719
@app.cell(hide_code=True)
2820
def _(mo):
2921
# Cell tags: blah
30-
mo.md(
31-
r"""
32-
A markdown cell with tags: ['blah', 'hide-cell'].
33-
"""
34-
)
22+
mo.md(r"""A markdown cell with tags: ['blah', 'hide-cell'].""")
3523
return
3624

3725

0 commit comments

Comments
 (0)