diff --git a/kit/preprocess.js b/kit/preprocess.js index bc55952a..99ecd745 100644 --- a/kit/preprocess.js +++ b/kit/preprocess.js @@ -466,30 +466,45 @@ function escapeSvelteSpecialChars() { visit(tree, "html", onHtml); } + function isWithinDocBody(node) { + if (["", "HF_DOC_BODY_START"].includes(node.value)) { + hfDocBodyStart = true; + hfDocBodyEnd = false; + // delete the marker + if(node.value === "HF_DOC_BODY_START"){ + node.value = ""; + } + } + if (["", "HF_DOC_BODY_END"].includes(node.value)) { + hfDocBodyEnd = true; + // delete the marker + if(node.value === "HF_DOC_BODY_END"){ + node.value = ""; + } + } + return hfDocBodyStart && !hfDocBodyEnd; + } + function onText(node) { + if (!isWithinDocBody(node)) { + return; + } node.value = node.value.replaceAll("{", "{"); node.value = node.value.replaceAll("<", "<"); } function onHtml(node) { - if(node.value === ""){ - hfDocBodyStart = true; - hfDocBodyEnd = false; - } - if(node.value === ""){ - hfDocBodyEnd = true; + if (!isWithinDocBody(node)) { + return; } const RE_TAG_NAME = /<\/?(\w+)/; const match = node.value.match(RE_TAG_NAME); + const REGEX_VALID_START_END_TAG = /^<(\w+)[^>]*>.*<\/\1>$/s; if (match) { const tagName = match[1]; if (!validTags.includes(tagName)) { node.value = node.value.replaceAll("<", "<"); - }else if(hfDocBodyStart && !hfDocBodyEnd && htmlTags.includes(tagName)){ - const REGEX_VALID_START_END_TAG = /^<(\w+)[^>]*>.*<\/\1>$/s; - if(!REGEX_VALID_START_END_TAG.test(node.value.trim())){ - return; - } + }else if(htmlTags.includes(tagName) && REGEX_VALID_START_END_TAG.test(node.value.trim())){ const $ = cheerio.load(node.value); // Go through each text node in the HTML and replace "{" with "{" $('*').contents().each((index, element) => { diff --git a/kit/src/lib/DropdownEntry.svelte b/kit/src/lib/DropdownEntry.svelte index f49a33f1..2a02f716 100644 --- a/kit/src/lib/DropdownEntry.svelte +++ b/kit/src/lib/DropdownEntry.svelte @@ -16,7 +16,7 @@ export let useDeprecatedJS = true; -
  • +
  • + +HF_DOC_BODY_END + """ ) diff --git a/tests/test_convert_md_to_mdx.py b/tests/test_convert_md_to_mdx.py index 35e68c82..da311f45 100644 --- a/tests/test_convert_md_to_mdx.py +++ b/tests/test_convert_md_to_mdx.py @@ -65,9 +65,14 @@ def test_convert_md_to_mdx(self): +HF_DOC_BODY_START + Lorem ipsum dolor sit amet, consectetur adipiscing elit + +HF_DOC_BODY_END + """ print(convert_md_to_mdx(md_text, page_info)) self.assertEqual(convert_md_to_mdx(md_text, page_info), expected_conversion)