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
37 changes: 26 additions & 11 deletions kit/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,30 +466,45 @@ function escapeSvelteSpecialChars() {
visit(tree, "html", onHtml);
}

function isWithinDocBody(node) {
if (["<!--HF DOCBUILD BODY START-->", "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 DOCBUILD BODY END-->", "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("{", "&#123;");
node.value = node.value.replaceAll("<", "&#60;");
}

function onHtml(node) {
if(node.value === "<!--HF DOCBUILD BODY START-->"){
hfDocBodyStart = true;
hfDocBodyEnd = false;
}
if(node.value === "<!--HF DOCBUILD BODY END-->"){
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("<", "&#60;");
}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 "&#123;"
$('*').contents().each((index, element) => {
Expand Down
2 changes: 1 addition & 1 deletion kit/src/lib/DropdownEntry.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
export let useDeprecatedJS = true;
</script>

<li>
<li class="not-prose">
<a
class="flex items-center hover:bg-gray-50 dark:hover:bg-gray-800 cursor-pointer px-3 py-1.5 whitespace-nowrap
{classNames}
Expand Down
5 changes: 5 additions & 0 deletions src/doc_builder/convert_md_to_mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ def convert_md_to_mdx(md_text, page_info):

<!--HF DOCBUILD BODY START-->

HF_DOC_BODY_START

"""
+ process_md(md_text, page_info)
+ """

<!--HF DOCBUILD BODY END-->

HF_DOC_BODY_END

"""
)

Expand Down
5 changes: 5 additions & 0 deletions tests/test_convert_md_to_mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ def test_convert_md_to_mdx(self):

<!--HF DOCBUILD BODY START-->

HF_DOC_BODY_START

Lorem ipsum dolor sit amet, consectetur adipiscing elit

<!--HF DOCBUILD BODY END-->

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)
Expand Down