import {
// Flag,
PowerCord,
Tag,
FourDiamonds,
RoundBrackets,
// @icons breaks when imported indirectly from astro.config.mjs
} from "../../components/icons.tsx";
import type {
ConfigHeading,
ConfigTOC,
} from "@components/navigation/sidebars/types";
import type { QMLTypeLinkObject } 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 {
let depth = heading.depth - 1;
let parent = null;
while (!parent && depth !== 0) {
parent = parentHeadings.get(depth);
depth -= 1;
}
if (parent) parent.subheadings.push(heading);
else toc.push(heading);
}
}
return toc;
}
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(
version: string,
{ type, module, name, mtype, mname }: QMLTypeLinkObject
) {
if (type === "unknown") {
return "#unknown";
}
const qtStart = "https://doc.qt.io/qt-6/";
const localStart = `/docs/${version}/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 = ``;
const RoundBracketsIconString: string = ``;
const PowerCordIconString: string = ``;
const FourDiamondsIconString: string = ``;
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]();
}