Skip to content

Commit

Permalink
Fix MD Parser (#45, #46) (#48)
Browse files Browse the repository at this point in the history
* Fix MD Parser

* per Fabien & Fred
  • Loading branch information
dinhlongviolin1 authored Nov 7, 2023
1 parent c1c2923 commit c8183af
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions markdownPreview/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import { createRoot } from "react-dom/client";

import App from "./components/App";

function init() {
const init = () => {
let allElements = Array.from(document.querySelectorAll("*"));
// Checks if the markdown contains any taipy elements
if (!allElements.some((el) => el.tagName.toLowerCase().startsWith("taipy"))) {
return;
}
const mdbd = document.getElementsByClassName("markdown-body");
const markdownBodyHTML = mdbd[mdbd.length - 1].outerHTML;
mdbd[mdbd.length - 1].remove();
Expand All @@ -27,8 +32,17 @@ function init() {
}
rootDiv.innerHTML = "";
const root = createRoot(rootDiv);
root.render(<App jsx={markdownBodyHTML} baseHref={baseHref} />);
}
root.render(<App jsx={formatHtmlBody(markdownBodyHTML)} baseHref={baseHref} />);
};

// Regular expression to find curly braces outside of HTML tags
const replaceCurlyBracesRegex = /(?:\{[^}]*\})(?![^<]*>)/g;
const replaceCurlyBraces = (match: string) => {
return match.replace(/{/g, "&#123;").replace(/}/g, "&#125;");
};
const formatHtmlBody = (html: string) => {
return html.replace(replaceCurlyBracesRegex, replaceCurlyBraces);
};

window.addEventListener("vscode.markdown.updateContent", init);

Expand Down

0 comments on commit c8183af

Please sign in to comment.