diff --git a/src/components/navigation/Search.astro b/src/components/navigation/Search.astro index c5bdb45..1835ed9 100644 --- a/src/components/navigation/Search.astro +++ b/src/components/navigation/Search.astro @@ -107,8 +107,9 @@ import magnifierIcon from "@icons/magnifier.svg?raw"; const match = [...excerpt.matchAll(linkRegex)]; if (match.length > 0) { for (const matching of match) { + const currentVersion = import.meta.url; const linkObject = getQMLTypeLinkObject(matching[1]); - const link = getQMLTypeLink("NOVERSION", linkObject); + const link = getQMLTypeLink(currentVersion, linkObject); const icon = linkObject.mtype ? getIconForLink(linkObject.mtype, false) : null; @@ -133,7 +134,8 @@ import magnifierIcon from "@icons/magnifier.svg?raw"; //@ts-expect-error "@pagefind/default-ui" ); - const versionMatch = window.location.pathname.match(/^\/docs\/([^/]+)/); + const versionMatch = + window.location.pathname.match(/^\/docs\/([^/]+)/); const activeVersion = versionMatch?.[1]; const search = new PagefindUI({ diff --git a/src/config/io/helpers.ts b/src/config/io/helpers.ts index e30c25b..e27bc9b 100644 --- a/src/config/io/helpers.ts +++ b/src/config/io/helpers.ts @@ -12,7 +12,7 @@ import type { } from "@components/navigation/sidebars/types"; import type { QMLTypeLinkObject } from "@_types"; -export function buildHierarchy(headings: ConfigHeading[]) { +function buildHierarchy(headings: ConfigHeading[]) { const toc: ConfigTOC[] = []; const parentHeadings = new Map(); @@ -42,7 +42,7 @@ export function buildHierarchy(headings: ConfigHeading[]) { return toc; } -export function getQMLTypeLinkObject(unparsed: string) { +function getQMLTypeLinkObject(unparsed: string) { const isLocal = unparsed.startsWith("MQS_") ? "local" : false; const isQT = unparsed.startsWith("MQT_") ? "qt" : false; const index = isLocal || isQT || "self"; @@ -96,7 +96,7 @@ export function getQMLTypeLinkObject(unparsed: string) { return hashMap[index](); } -export function getQMLTypeLink( +function getQMLTypeLink( version: string, { type, module, name, mtype, mname }: QMLTypeLinkObject ) { @@ -132,7 +132,7 @@ export function getQMLTypeLink( return hashMap[type as keyof typeof hashMap](); } -export function getIconForLink(mtype: string, isJsx: boolean) { +function getIconForLink(mtype: string, isJsx: boolean) { const TagIconString: string = ` = () => @@ -74,7 +76,15 @@ const remarkParseAtTypes: RemarkPlugin<[]> = const rehypeRewriteTypelinks: RehypePlugin<[]> = () => - (root: Html.Root): Html.Root => { + (root: Html.Root, file: VFile): Html.Root => { + const frontmatter = (file.data as any)?.astro?.frontmatter; + const frontmatterVersion = frontmatter?.version; + const pathVersion = + file && file.path && file.path.match(versionMatcher); + const version = + (frontmatter && frontmatterVersion) || + (pathVersion && pathVersion[0]) || + null; visit( root as Unist.Parent, "text", @@ -88,7 +98,7 @@ const rehypeRewriteTypelinks: RehypePlugin<[]> = const linkObject = getQMLTypeLinkObject(match); const link = getQMLTypeLink( - currentVersion, + version ?? "", linkObject ); const icon = @@ -118,7 +128,15 @@ const rehypeRewriteTypelinks: RehypePlugin<[]> = const rehypeRewriteVersionedDoclinks: RehypePlugin<[]> = () => - (root: Html.Root): Html.Root => { + (root: Html.Root, file: VFile): Html.Root => { + const frontmatter = (file.data as any)?.astro?.frontmatter; + const frontmatterVersion = frontmatter?.version; + const pathVersion = + file && file.path && file.path.match(versionMatcher); + const version = + (frontmatter && frontmatterVersion) || + (pathVersion && pathVersion[0]) || + null; visit( root as Unist.Parent, "element", @@ -128,7 +146,7 @@ const rehypeRewriteVersionedDoclinks: RehypePlugin<[]> = !((properties.href as string) ?? "").startsWith("@docs") ) return CONTINUE; - properties.href = `/docs/${currentVersion}/${(properties.href as string).slice(6)}`; + properties.href = `/docs/${version || "master"}/${(properties.href as string).slice(6)}`; return CONTINUE; } ); @@ -138,15 +156,21 @@ const rehypeRewriteVersionedDoclinks: RehypePlugin<[]> = const shikiRewriteTypelinks: ShikiTransformer = { name: "rewrite-typelinks", - postprocess(code, _options) { + postprocess(code, options) { // WARN: need to change the code link identifier to this const regExp = /TYPE99(\w+.)99TYPE/g; const hasTypelinks = code.search(regExp) !== -1; + const rawMeta = this.options.meta?.__raw || ""; + const version = rawMeta && rawMeta.match(versionMatcher); + const currentVersion = version && version[0]; if (hasTypelinks) { code.replace(regExp, (_full: string, match: string) => { const linkObject = getQMLTypeLinkObject(match); - const link = getQMLTypeLink(currentVersion, linkObject); + const link = getQMLTypeLink( + currentVersion || "", + linkObject + ); return `${linkObject.name ?? ""}`; }); } @@ -267,14 +291,12 @@ async function getMarkdownProcessor(): Promise { } async function processMarkdown( - version: string, + _version: string, markdown: string ): Promise { - currentVersion = version; const r = ( await (await getMarkdownProcessor()).render(markdown) ).code; - currentVersion = "NOVERSION"; return r; }