Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions frontend/src/core/codemirror/language/languages/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ export class MarkdownLanguageAdapter
code: string,
metadata: MarkdownLanguageAdapterMetadata,
): [string, number] {
// NB. Must be kept consistent with marimo/_convert/utils.py
// ::markdown_to_marimo

// Empty string
if (code === "") {
// Need at least a space, otherwise the output will be 6 quotes
Expand Down
2 changes: 1 addition & 1 deletion marimo/_convert/markdown/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def _tree_to_ir(root: Element) -> SafeWrap[NotebookSerializationV1]:
header = root.get("header", None)
pyproject = root.get("pyproject", None)
if pyproject and not header:
header = "\n# ".join(["# ///script", *pyproject.splitlines(), "///"])
header = "\n# ".join(["# /// script", *pyproject.splitlines(), "///"])
notebook = NotebookSerializationV1(
app=AppInstantiation(options=app_config),
cells=[
Expand Down
15 changes: 13 additions & 2 deletions marimo/_convert/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@


def markdown_to_marimo(source: str) -> str:
# NB. This should be kept in sync with the logic in
# frontend/src/core/codemirror/language/languages/markdown.ts
# ::transformOut
source = source.replace('"""', '\\"\\"\\"')

# 6 quotes in a row breaks
if not source:
source = " "

if "\n" not in source:
return f'mo.md(r"""{source}""")'

return "\n".join(
[
"mo.md(",
# r-string: a backslash is just a backslash!
codegen.indent_text('r"""'),
codegen.indent_text(source),
codegen.indent_text('"""'),
source,
'"""',
")",
]
)
Expand Down
2 changes: 1 addition & 1 deletion marimo/_server/export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def export_as_md(
metadata["header"] = header.strip()
else:
header_file = previous if previous else filename
if header_file:
if header_file and Path(header_file).exists():
with open(header_file, encoding="utf-8") as f:
_metadata, _ = extract_frontmatter(f.read())
metadata.update(_metadata)
Expand Down
6 changes: 3 additions & 3 deletions tests/_cli/snapshots/markdown_to_marimo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ app = marimo.App()
def _(mo):
mo.md(
r"""
# Test Markdown
# Test Markdown

print('Hello from Markdown!')
"""
print('Hello from Markdown!')
"""
)
return

Expand Down
10 changes: 5 additions & 5 deletions tests/_cli/snapshots/remote_markdown_to_marimo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ app = marimo.App()
def _(mo):
mo.md(
r"""
# Remote Markdown Test
# Remote Markdown Test

```python
print('Hello from Remote Markdown!')
```
"""
```python
print('Hello from Remote Markdown!')
```
"""
)
return

Expand Down
33 changes: 33 additions & 0 deletions tests/_convert/snapshots/basic_marimo_example.py.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import marimo

__generated_with = "0.15.2"
app = marimo.App(width="medium")


@app.cell(hide_code=True)
def _(mo):
mo.md(r"""This is a simple marimo notebook""")
return


@app.cell
def _():
x = 1
return (x,)


@app.cell
def _(x):
y = x+1
y
return


@app.cell
def _():
import marimo as mo
return (mo,)


if __name__ == "__main__":
app.run()
33 changes: 33 additions & 0 deletions tests/_convert/snapshots/basic_marimo_example_roundtrip.py.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import marimo

__generated_with = "0.0.0"
app = marimo.App(width="medium", app_title="Test Notebook")


@app.cell(hide_code=True)
def _(mo):
mo.md(r"""This is a simple marimo notebook""")
return


@app.cell
def _():
x = 1
return (x,)


@app.cell
def _(x):
y = x+1
y
return


@app.cell
def _():
import marimo as mo
return (mo,)


if __name__ == "__main__":
app.run()
20 changes: 20 additions & 0 deletions tests/_convert/snapshots/basic_marimo_example_to_md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Test Notebook
marimo-version: 0.0.0
width: medium
---

This is a simple marimo notebook

```python {.marimo}
x = 1
```

```python {.marimo}
y = x+1
y
```

```python {.marimo}
import marimo as mo
```
18 changes: 3 additions & 15 deletions tests/_convert/snapshots/convert_hides_markdown_cells.py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,21 @@ app = marimo.App()

@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
A markdown cell.
"""
)
mo.md(r"""A markdown cell.""")
return


@app.cell(hide_code=True)
def _(mo):
# Cell tags: blah
mo.md(
r"""
A markdown cell with tags: ['blah'].
"""
)
mo.md(r"""A markdown cell with tags: ['blah'].""")
return


@app.cell(hide_code=True)
def _(mo):
# Cell tags: blah
mo.md(
r"""
A markdown cell with tags: ['blah', 'hide-cell'].
"""
)
mo.md(r"""A markdown cell with tags: ['blah', 'hide-cell'].""")
return


Expand Down
6 changes: 1 addition & 5 deletions tests/_convert/snapshots/frontmatter-test.py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ app = marimo.App(app_title="My Title")

@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# Notebook
"""
)
mo.md(r"""# Notebook""")
return


Expand Down
12 changes: 4 additions & 8 deletions tests/_convert/snapshots/no-frontmatter.py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ app = marimo.App()

@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# My Notebook
"""
)
mo.md(r"""# My Notebook""")
return


Expand All @@ -24,9 +20,9 @@ def _():
def _(mo):
mo.md(
r"""
**Appendix**
- This is the end of the notebook
"""
**Appendix**
- This is the end of the notebook
"""
)
return

Expand Down
12 changes: 4 additions & 8 deletions tests/_convert/snapshots/pypercent_format.py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ def _():
def _(mo):
mo.md(
r"""
This is a markdown cell
with multiple lines
"""
This is a markdown cell
with multiple lines
"""
)
return


@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
This is a doc string, but also markdown
"""
)
mo.md(r"""This is a doc string, but also markdown""")
return


Expand Down
12 changes: 6 additions & 6 deletions tests/_convert/snapshots/pypercent_markdown_only.py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def _():
def _(mo):
mo.md(
r"""
Introduction
This is a documentation file.
"""
Introduction
This is a documentation file.
"""
)
return

Expand All @@ -25,9 +25,9 @@ def _(mo):
def _(mo):
mo.md(
r"""
Usage
Here's how to use this module.
"""
Usage
Here's how to use this module.
"""
)
return

Expand Down
6 changes: 1 addition & 5 deletions tests/_convert/snapshots/sql-notebook.py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ app = marimo.App(app_title="My Title")

@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# SQL notebook
"""
)
mo.md(r"""# SQL notebook""")
return


Expand Down
12 changes: 6 additions & 6 deletions tests/_convert/snapshots/unsafe-app.py.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def _(mo):
def _(mo):
mo.md(
r"""
Nested fence
````text
The guards are
```python {.marimo}
````
"""
Nested fence
````text
The guards are
```python {.marimo}
````
"""
)
return

Expand Down
16 changes: 8 additions & 8 deletions tests/_convert/snapshots/unsafe-doc-old.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ print("Hello, World!")
````python {.marimo hide_code="true"}
mo.md(
r"""
-->
-->

```marimo run convert document.md```
"""
```marimo run convert document.md```
"""
)
````

Expand All @@ -31,12 +31,12 @@ it's an unparsable cell
````python {.marimo hide_code="true"}
mo.md(
r"""
<!-- Actually markdown -->
```{python} `
print("Hello, World!")
<!-- Actually markdown -->
```{python} `
print("Hello, World!")

<!-- Disabled code block -->
"""
<!-- Disabled code block -->
"""
)
````

Expand Down
Loading
Loading