Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix #2139: disable heading detection for carousel, accordion and tabs (#2154)

eugene88be8cc42577
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index cb370c6e6f..0ab296d012 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -287,7 +287,6 @@ export function useNotebookActions() {
});
},
},
-
{
icon: <ZapIcon size={14} strokeWidth={1.5} />,
label: "Enable all cells",
diff --git a/frontend/src/core/dom/__tests__/outline.test.ts b/frontend/src/core/dom/__tests__/outline.test.ts
index 7d254cf960..2606c6182b 100644
--- a/frontend/src/core/dom/__tests__/outline.test.ts
+++ b/frontend/src/core/dom/__tests__/outline.test.ts
@@ -199,6 +199,49 @@ describe("parseOutline", () => {
}
`);
});
+ it("excludes headings within excluded tags", () => {
+ const html = `
+ <div>
+ <h1 id="included-heading">Included Heading</h1>
+ <marimo-carousel>
+ <h2 id="excluded-heading-carousel">Excluded Heading in Carousel</h2>
+ </marimo-carousel>
+ <marimo-tabs>
+ <h2 id="excluded-heading-tabs">Excluded Heading in Tabs</h2>
+ </marimo-tabs>
+ <marimo-accordion>
+ <h2 id="excluded-heading-accordion">Excluded Heading in Accordion</h2>
+ </marimo-accordion>
+ <h2 id="another-included-heading">Another Included Heading</h2>
+ </div>
+ `;
+ const outline = parseOutline({
+ mimetype: "text/html",
+ timestamp: 0,
+ channel: "output",
+ data: html,
+ });
+ expect(outline).toMatchInlineSnapshot(`
+ {
+ "items": [
+ {
+ "by": {
+ "id": "included-heading",
+ },
+ "level": 1,
+ "name": "Included Heading",
+ },
+ {
+ "by": {
+ "id": "another-included-heading",
+ },
+ "level": 2,
+ "name": "Another Included Heading",
+ },
+ ],
+ }
+ `);
+ });
});
const OUTLINE_1: Outline = {
diff --git a/frontend/src/core/dom/outline.ts b/frontend/src/core/dom/outline.ts
index 6287374bbf..75b0776905 100644
--- a/frontend/src/core/dom/outline.ts
+++ b/frontend/src/core/dom/outline.ts
@@ -5,6 +5,9 @@ import type { OutputMessage } from "../kernel/messages";
import type { Outline, OutlineItem } from "../cells/outline";
import { invariant } from "@/utils/invariant";
+// Tags that we don't want to include in the outline
+const excludedTags = ["marimo-carousel", "marimo-tabs", "marimo-accordion"];
+
function getOutline(html: string): Outline {
const items: Outline["items"] = [];
@@ -15,6 +18,11 @@ function getOutline(html: string): Outline {
// eslint-disable-next-line unicorn/prefer-spread
for (const heading of Array.from(headings)) {
const name = heading.textContent;
+ // Check if the heading is within any of the excluded tags
+ if (excludedTags.some((tag) => heading.closest(tag))) {
+ continue;
+ }
+
if (!name) {
continue;
}
diff --git a/marimo/_smoke_tests/outline.py b/marimo/_smoke_tests/outline.py
new file mode 100644
index 0000000000..ec3249c56e
--- /dev/null
+++ b/marimo/_smoke_tests/outline.py
@@ -0,0 +1,59 @@
+# Copyright 2024 Marimo. All rights reserved.
+
+import marimo
+
+__generated_with = "0.8.3"
+app = marimo.App(width="medium")
+
+
+def __():
+ import marimo as mo
+
+ return (mo,)
+
+
+def __(mo):
+ mo.md("# Heading 1")
+ return
+
+
+def __(mo):
+ mo.carousel([mo.md("# Carousel Heading")])
+ return
+
+
+def __(mo):
+ mo.md("# Heading 2 \n\n Headings between carousel and tabs are detected")
+ return
+
+
+def __(mo):
+ mo.ui.tabs({"Tab 1": mo.md("# Tabs Heading")})
+ return
+
+
+def __(mo):
+ mo.md("# Heading 3 \n\n Headings between tabs and accordion are detected")
+ return
+
+
+def __(mo):
+ mo.accordion({"Accordion 1": mo.md("# Accordion Heading")})
+ return
+
+
+def __(mo):
+ mo.md("# Heading 4")
+ return
+
+
+if __name__ == "__main__":
+ app.run()