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
24 changes: 24 additions & 0 deletions faq-schema.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions scripts/build-faq-schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Build faq-schema.js (FAQPage JSON-LD for /platform/support/faqs) from faqs.mdx.

Run after editing platform/support/faqs.mdx: python3 scripts/build-faq-schema.py
"""
import json, re, pathlib

root = pathlib.Path(__file__).resolve().parent.parent
src = (root / "platform/support/faqs.mdx").read_text()


def plain(text):
text = re.sub(r"<[^>]+/>", "", text) # self-closing components
text = re.sub(r"</?[A-Za-z][^>]*>", "", text) # other tags
text = re.sub(r"!\[[^\]]*\]\([^)]*\)", "", text) # images
text = re.sub(r"\[([^\]]+)\]\([^)]*\)", r"\1", text) # links
text = re.sub(r"\*\*|`", "", text)
text = re.sub(r"^\s*[-*]\s+", "", text, flags=re.M)
text = re.sub(r"^\s*\|.*\|\s*$", "", text, flags=re.M) # tables
return re.sub(r"\s+", " ", text).strip()


items = []
for title, body in re.findall(r'<Accordion title="([^"]+)"[^>]*>(.*?)</Accordion>', src, re.S):
answer = plain(body)
if answer:
items.append({"@type": "Question", "name": title,
"acceptedAnswer": {"@type": "Answer", "text": answer}})

schema = {"@context": "https://schema.org", "@type": "FAQPage",
"@id": "https://docs.graphify.com/platform/support/faqs#faq", "mainEntity": items}

js = f"""/* FAQPage structured data for /platform/support/faqs. Generated by scripts/build-faq-schema.py; do not edit by hand. */
(() => {{
const PATH = "/platform/support/faqs";
const ID = "graphify-faq-schema";
const SCHEMA = {json.dumps(schema, ensure_ascii=False)};
const sync = () => {{
const existing = document.getElementById(ID);
const onPage = window.location.pathname.replace(/\\/$/, "") === PATH;
if (onPage && !existing) {{
const tag = document.createElement("script");
tag.type = "application/ld+json";
tag.id = ID;
tag.textContent = JSON.stringify(SCHEMA);
document.head.appendChild(tag);
}} else if (!onPage && existing) {{
existing.remove();
}}
}};
sync();
let last = window.location.pathname;
setInterval(() => {{
if (window.location.pathname !== last) {{ last = window.location.pathname; sync(); }}
}}, 500);
}})();
"""
(root / "faq-schema.js").write_text(js)
print(f"{len(items)} questions")
Loading