import { // Flag, PowerCord, Tag, FourDiamonds, RoundBrackets, // @icons breaks when imported indirectly from astro.config.mjs } from "../../components/icons.tsx"; import type { ConfigHeading, ConfigTOC, GroupedRoutes, } from "@components/navigation/sidebars/types"; import type { QMLTypeLinkObject, RouteData } from "./types"; export function buildHierarchy(headings: ConfigHeading[]) { const toc: ConfigTOC[] = []; const parentHeadings = new Map(); if (!headings || headings.length === 0) { return toc; } for (const h of headings) { const heading = { ...h, subheadings: [] }; parentHeadings.set(heading.depth, heading); if (heading.depth === 1) { toc.push(heading); } else { parentHeadings .get(heading.depth - 1) .subheadings.push(heading); } } return toc; } export function groupRoutes(routes: RouteData[]): GroupedRoutes { const froutes = routes.filter(route => route.name !== "index"); const defaultValue = { tutorials: { configuration: [ { name: "Getting Started", type: "getting-started" }, { name: "Intro", type: "intro" }, { name: "Positioning", type: "positioning" }, { name: "QML Overview", type: "qml-overview" }, ], }, types: {}, }; return froutes.reduce((acc, route) => { if (!acc.tutorials) { acc.tutorials = { configuration: [ { name: "Getting Started", type: "getting-started" }, { name: "Intro", type: "intro" }, { name: "Positioning", type: "positioning" }, { name: "QML Overview", type: "qml-overview" }, ], }; } if (!acc.types) acc.types = {}; if (!acc.types[route.type]) { acc.types[route.type] = []; } acc.types[route.type].push({ name: route.name, type: route.type, }); return acc; }, defaultValue); } export function getQMLTypeLinkObject(unparsed: string) { const isLocal = unparsed.startsWith("MQS_") ? "local" : false; const isQT = unparsed.startsWith("MQT_") ? "qt" : false; const index = isLocal || isQT || "self"; const hashMap = { local: () => { const linkSplit = unparsed.slice(4).split("99"); const hasSubmodule = linkSplit[0].indexOf("_") !== -1; const linkModule = hasSubmodule ? linkSplit[0].replace("_", ".") : linkSplit[0]; const linkObj: QMLTypeLinkObject = { type: "local", module: linkModule.replace("_", "."), name: linkSplit[1].slice(1), }; if (linkSplit.length > 2) { linkObj.mname = linkSplit[2].slice(1); linkObj.mtype = linkSplit[3].slice(1); } return linkObj; }, qt: () => { const linkSplit = unparsed.slice(4).split("99"); const hasSubmodule = linkSplit[0].indexOf("_") !== -1; const linkModule = hasSubmodule ? linkSplit[0].replace("_", "-") : linkSplit[0]; const linkObj: QMLTypeLinkObject = { type: "qt", module: linkModule.replace("_", "-"), name: linkSplit[1].slice(1), }; if (linkSplit.length > 2) { linkObj.mname = linkSplit[2].slice(1); linkObj.mtype = linkSplit[3].slice(1); } return linkObj; }, self: () => { const linkSplit = unparsed.slice(1).split("99"); const linkObj: QMLTypeLinkObject = { type: "self", mname: linkSplit[0], mtype: linkSplit[1].slice(1), }; return linkObj; }, }; return hashMap[index](); } export function getQMLTypeLink({ type, module, name, mtype, mname, }: QMLTypeLinkObject) { if (type === "unknown") { return "#unknown"; } const qtStart = "https://doc.qt.io/qt-6/"; const localStart = "/docs/types"; const hashMap = { local: () => { const isSpecific = mname ? `#${mname}` : ""; const localLink = `${localStart}/${module}/${name}${isSpecific}`; return localLink; }, qt: () => { const isSpecific = mname ? `#${mname}-${mtype === "func" ? "method" : mtype}` : ""; const qtLink = `${qtStart}${module!.toLowerCase().replace(".", "-")}-${name!.toLowerCase()}.html${isSpecific}`; return qtLink; }, self: () => { const selfLink = `#${mname}`; return selfLink; }, }; if (!type) { type = "self"; } return hashMap[type as keyof typeof hashMap](); } export function getIconForLink(mtype: string, isJsx: boolean) { const TagIconString: string = ` Go to `; const RoundBracketsIconString: string = ` Go to `; const PowerCordIconString: string = ` Go to `; const FourDiamondsIconString: string = ` Go to `; const map = { prop: () => (isJsx ? Tag : TagIconString), func: () => (isJsx ? RoundBrackets : RoundBracketsIconString), signal: () => (isJsx ? PowerCord : PowerCordIconString), variant: () => isJsx ? FourDiamonds : FourDiamondsIconString, }; return map[mtype as keyof typeof map](); }