From 0cbb469842d74381ad56d44b7975f34c405b78f8 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 3 May 2024 14:06:11 +0530 Subject: [PATCH] feat(build): add localeIndex to md.env (#3862) --- src/node/markdown/plugins/containers.ts | 6 ++++-- src/node/markdownToVue.ts | 10 +++++++--- src/node/plugins/localSearchPlugin.ts | 10 ++-------- src/shared/shared.ts | 22 +++++++++++++++------- types/shared.d.ts | 1 + 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/node/markdown/plugins/containers.ts b/src/node/markdown/plugins/containers.ts index a5d5cf90..99973bb2 100644 --- a/src/node/markdown/plugins/containers.ts +++ b/src/node/markdown/plugins/containers.ts @@ -1,8 +1,10 @@ import type MarkdownIt from 'markdown-it' +import container from 'markdown-it-container' import type { RenderRule } from 'markdown-it/lib/renderer.mjs' import type Token from 'markdown-it/lib/token.mjs' -import container from 'markdown-it-container' import { nanoid } from 'nanoid' +import type { MarkdownEnv } from '../../shared' + import { extractTitle, getAdaptiveThemeMarker, @@ -60,7 +62,7 @@ function createContainer( container, klass, { - render(tokens, idx, _options, env) { + render(tokens, idx, _options, env: MarkdownEnv & { references?: any }) { const token = tokens[idx] const info = token.info.trim().slice(klass.length).trim() const attrs = md.renderer.renderAttrs(token) diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index fd3b0d88..102f293b 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -11,11 +11,12 @@ import { } from './markdown/markdown' import { EXTERNAL_URL_RE, + getLocaleForPath, slash, + treatAsHtml, type HeadConfig, type MarkdownEnv, - type PageData, - treatAsHtml + type PageData } from './shared' import { getGitTimestamp } from './utils/getGitTimestamp' import { processIncludes } from './utils/processIncludes' @@ -95,13 +96,16 @@ export async function createMarkdownToVueRenderFn( let includes: string[] = [] src = processIncludes(srcDir, src, fileOrig, includes) + const localeIndex = getLocaleForPath(siteConfig?.site, relativePath) + // reset env before render const env: MarkdownEnv = { path: file, relativePath, cleanUrls, includes, - realPath: fileOrig + realPath: fileOrig, + localeIndex } const html = md.render(src, env) const { diff --git a/src/node/plugins/localSearchPlugin.ts b/src/node/plugins/localSearchPlugin.ts index c223c326..d025641c 100644 --- a/src/node/plugins/localSearchPlugin.ts +++ b/src/node/plugins/localSearchPlugin.ts @@ -7,7 +7,7 @@ import type { Plugin, ViteDevServer } from 'vite' import type { SiteConfig } from '../config' import { createMarkdownRenderer } from '../markdown/markdown' import { - resolveSiteDataByRoute, + getLocaleForPath, slash, type DefaultTheme, type MarkdownEnv @@ -83,12 +83,6 @@ export async function localSearchPlugin( return index } - function getLocaleForPath(file: string) { - const relativePath = slash(path.relative(siteConfig.srcDir, file)) - const siteData = resolveSiteDataByRoute(siteConfig.site, relativePath) - return siteData?.localeIndex ?? 'root' - } - let server: ViteDevServer | undefined function onIndexUpdated() { @@ -126,7 +120,7 @@ export async function localSearchPlugin( const file = path.join(siteConfig.srcDir, page) // get file metadata const fileId = getDocId(file) - const locale = getLocaleForPath(file) + const locale = getLocaleForPath(siteConfig.site, page) const index = getIndexByLocale(locale) // retrieve file and split into "sections" const html = await render(file) diff --git a/src/shared/shared.ts b/src/shared/shared.ts index 7b42d864..3cf5ecbe 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -72,6 +72,20 @@ export function isExternal(path: string): boolean { return EXTERNAL_URL_RE.test(path) } +export function getLocaleForPath( + siteData: SiteData | undefined, + relativePath: string +): string { + return ( + Object.keys(siteData?.locales || {}).find( + (key) => + key !== 'root' && + !isExternal(key) && + isActive(relativePath, `/${key}/`, true) + ) || 'root' + ) +} + /** * this merges the locales data to the main data by the route */ @@ -79,13 +93,7 @@ export function resolveSiteDataByRoute( siteData: SiteData, relativePath: string ): SiteData { - const localeIndex = - Object.keys(siteData.locales).find( - (key) => - key !== 'root' && - !isExternal(key) && - isActive(relativePath, `/${key}/`, true) - ) || 'root' + const localeIndex = getLocaleForPath(siteData, relativePath) return Object.assign({}, siteData, { localeIndex, diff --git a/types/shared.d.ts b/types/shared.d.ts index 72597e43..e0acd501 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -200,4 +200,5 @@ export interface MarkdownEnv { links?: string[] includes?: string[] realPath?: string + localeIndex?: string }