From 4b84951804c659183e90e0604b629b00c8aa29d0 Mon Sep 17 00:00:00 2001 From: Steven Serrata <9343811+sserrata@users.noreply.github.com> Date: Wed, 23 Jul 2025 14:21:02 -0500 Subject: [PATCH 1/4] feat: add raw markdown route --- docusaurus.config.ts | 3 +- plugins/markdown-route/index.cjs | 12 ++++++ src/components/MarkdownPage.tsx | 63 ++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 plugins/markdown-route/index.cjs create mode 100644 src/components/MarkdownPage.tsx diff --git a/docusaurus.config.ts b/docusaurus.config.ts index c39eb3651..7c52735fd 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -22,7 +22,7 @@ if (process.env.CI_MERGE_REQUEST_IID) { const config = { future: { experimental_faster: (process.env.DOCUSAURUS_FASTER ?? "true") === "true", - v4: true + v4: true, }, title: "Develop with Palo Alto Networks", tagline: @@ -1171,6 +1171,7 @@ const config = { }, ], tailwindPlugin, + require.resolve("./plugins/markdown-route/index.cjs"), ], stylesheets: [ { diff --git a/plugins/markdown-route/index.cjs b/plugins/markdown-route/index.cjs new file mode 100644 index 000000000..57e823ef4 --- /dev/null +++ b/plugins/markdown-route/index.cjs @@ -0,0 +1,12 @@ +function markdownRoutePlugin() { + return { + name: "markdown-route-plugin", + async contentLoaded({ actions }) { + actions.addRoute({ + path: "/:route+\\.md", + component: "@site/src/components/MarkdownPage", + }); + }, + }; +} +module.exports = markdownRoutePlugin; diff --git a/src/components/MarkdownPage.tsx b/src/components/MarkdownPage.tsx new file mode 100644 index 000000000..2eb763dff --- /dev/null +++ b/src/components/MarkdownPage.tsx @@ -0,0 +1,63 @@ +import React, { useEffect, useState } from "react"; +import { useLocation } from "@docusaurus/router"; +import TurndownService from "turndown"; + +export default function MarkdownPage() { + const location = useLocation(); + const [markdown, setMarkdown] = useState(null); + const [error, setError] = useState(null); + + useEffect(() => { + async function fetchAndConvert() { + const htmlPath = location.pathname.replace(/\.md$/, "/"); + try { + const res = await fetch(htmlPath); + if (!res.ok) { + throw new Error(`Failed to fetch source page: ${res.status}`); + } + const html = await res.text(); + const parser = new DOMParser(); + const doc = parser.parseFromString(html, "text/html"); + const content = + doc.querySelector(".openapi-left-panel__container") || + doc.querySelector(".theme-doc-markdown"); + if (!content) { + throw new Error("Unable to locate markdown source"); + } + const turndownService = new TurndownService(); + turndownService.addRule("details-rule", { + filter: "details", + replacement: function (content, node) { + const summary = node.querySelector("summary"); + const summaryText = summary ? summary.textContent?.trim() : ""; + const detailsContent = content.substring( + content.indexOf("") + 1 + ); + let md = `\n${summaryText}\n`; + const indented = detailsContent + .split("\n") + .map((line) => { + if (line.trim() === "") return line; + return ` ${line}`; + }) + .join("\n"); + md += indented; + return md + "\n"; + }, + }); + setMarkdown(turndownService.turndown(content)); + } catch (e) { + setError((e as Error).message); + } + } + fetchAndConvert(); + }, [location.pathname]); + + if (error) { + return
{error}
; + } + if (markdown === null) { + return

Loading...

; + } + return
{markdown}
; +} From 141771850b606032909f9f100b6bd9e62016c090 Mon Sep 17 00:00:00 2001 From: Steven Serrata <9343811+sserrata@users.noreply.github.com> Date: Wed, 23 Jul 2025 14:44:21 -0500 Subject: [PATCH 2/4] Fix markdown route path --- plugins/markdown-route/index.cjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/markdown-route/index.cjs b/plugins/markdown-route/index.cjs index 57e823ef4..378b5415f 100644 --- a/plugins/markdown-route/index.cjs +++ b/plugins/markdown-route/index.cjs @@ -3,8 +3,9 @@ function markdownRoutePlugin() { name: "markdown-route-plugin", async contentLoaded({ actions }) { actions.addRoute({ - path: "/:route+\\.md", + path: "/:route+.md", component: "@site/src/components/MarkdownPage", + exact: true, }); }, }; From 6231fed59d5c358544c16c1b42ae12fefd592777 Mon Sep 17 00:00:00 2001 From: Steven Serrata <9343811+sserrata@users.noreply.github.com> Date: Wed, 23 Jul 2025 15:07:24 -0500 Subject: [PATCH 3/4] Improve raw markdown viewer --- plugins/markdown-route/index.cjs | 3 ++- src/components/MarkdownPage.tsx | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/plugins/markdown-route/index.cjs b/plugins/markdown-route/index.cjs index 378b5415f..8ae7263d4 100644 --- a/plugins/markdown-route/index.cjs +++ b/plugins/markdown-route/index.cjs @@ -3,7 +3,8 @@ function markdownRoutePlugin() { name: "markdown-route-plugin", async contentLoaded({ actions }) { actions.addRoute({ - path: "/:route+.md", + path: "/__rawmd", + matchPath: "/:route+.md", component: "@site/src/components/MarkdownPage", exact: true, }); diff --git a/src/components/MarkdownPage.tsx b/src/components/MarkdownPage.tsx index 2eb763dff..52bb825fd 100644 --- a/src/components/MarkdownPage.tsx +++ b/src/components/MarkdownPage.tsx @@ -9,7 +9,7 @@ export default function MarkdownPage() { useEffect(() => { async function fetchAndConvert() { - const htmlPath = location.pathname.replace(/\.md$/, "/"); + const htmlPath = location.pathname.replace(/\.md$/, "/index.html"); try { const res = await fetch(htmlPath); if (!res.ok) { @@ -53,11 +53,22 @@ export default function MarkdownPage() { fetchAndConvert(); }, [location.pathname]); + const preStyle: React.CSSProperties = { + all: "unset", + whiteSpace: "pre-wrap", + fontFamily: "monospace", + display: "block", + padding: 0, + margin: 0, + background: "transparent", + color: "inherit", + }; + if (error) { - return
{error}
; + return
{error}
; } if (markdown === null) { return

Loading...

; } - return
{markdown}
; + return
{markdown}
; } From e62e62aa6c995aad900f8b7a4d66fa9691cb0307 Mon Sep 17 00:00:00 2001 From: Steven Serrata <9343811+sserrata@users.noreply.github.com> Date: Wed, 23 Jul 2025 16:25:37 -0400 Subject: [PATCH 4/4] Update index.cjs --- plugins/markdown-route/index.cjs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/markdown-route/index.cjs b/plugins/markdown-route/index.cjs index 8ae7263d4..d5d1483bd 100644 --- a/plugins/markdown-route/index.cjs +++ b/plugins/markdown-route/index.cjs @@ -3,10 +3,9 @@ function markdownRoutePlugin() { name: "markdown-route-plugin", async contentLoaded({ actions }) { actions.addRoute({ - path: "/__rawmd", + path: "/__md", matchPath: "/:route+.md", component: "@site/src/components/MarkdownPage", - exact: true, }); }, };