feat(build): add localeIndex to md.env (#3862)

pull/3879/head
Divyansh Singh 2 months ago committed by GitHub
parent ed6ada7a68
commit 0cbb469842
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,8 +1,10 @@
import type MarkdownIt from 'markdown-it' import type MarkdownIt from 'markdown-it'
import container from 'markdown-it-container'
import type { RenderRule } from 'markdown-it/lib/renderer.mjs' import type { RenderRule } from 'markdown-it/lib/renderer.mjs'
import type Token from 'markdown-it/lib/token.mjs' import type Token from 'markdown-it/lib/token.mjs'
import container from 'markdown-it-container'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import type { MarkdownEnv } from '../../shared'
import { import {
extractTitle, extractTitle,
getAdaptiveThemeMarker, getAdaptiveThemeMarker,
@ -60,7 +62,7 @@ function createContainer(
container, container,
klass, klass,
{ {
render(tokens, idx, _options, env) { render(tokens, idx, _options, env: MarkdownEnv & { references?: any }) {
const token = tokens[idx] const token = tokens[idx]
const info = token.info.trim().slice(klass.length).trim() const info = token.info.trim().slice(klass.length).trim()
const attrs = md.renderer.renderAttrs(token) const attrs = md.renderer.renderAttrs(token)

@ -11,11 +11,12 @@ import {
} from './markdown/markdown' } from './markdown/markdown'
import { import {
EXTERNAL_URL_RE, EXTERNAL_URL_RE,
getLocaleForPath,
slash, slash,
treatAsHtml,
type HeadConfig, type HeadConfig,
type MarkdownEnv, type MarkdownEnv,
type PageData, type PageData
treatAsHtml
} from './shared' } from './shared'
import { getGitTimestamp } from './utils/getGitTimestamp' import { getGitTimestamp } from './utils/getGitTimestamp'
import { processIncludes } from './utils/processIncludes' import { processIncludes } from './utils/processIncludes'
@ -95,13 +96,16 @@ export async function createMarkdownToVueRenderFn(
let includes: string[] = [] let includes: string[] = []
src = processIncludes(srcDir, src, fileOrig, includes) src = processIncludes(srcDir, src, fileOrig, includes)
const localeIndex = getLocaleForPath(siteConfig?.site, relativePath)
// reset env before render // reset env before render
const env: MarkdownEnv = { const env: MarkdownEnv = {
path: file, path: file,
relativePath, relativePath,
cleanUrls, cleanUrls,
includes, includes,
realPath: fileOrig realPath: fileOrig,
localeIndex
} }
const html = md.render(src, env) const html = md.render(src, env)
const { const {

@ -7,7 +7,7 @@ import type { Plugin, ViteDevServer } from 'vite'
import type { SiteConfig } from '../config' import type { SiteConfig } from '../config'
import { createMarkdownRenderer } from '../markdown/markdown' import { createMarkdownRenderer } from '../markdown/markdown'
import { import {
resolveSiteDataByRoute, getLocaleForPath,
slash, slash,
type DefaultTheme, type DefaultTheme,
type MarkdownEnv type MarkdownEnv
@ -83,12 +83,6 @@ export async function localSearchPlugin(
return index 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 let server: ViteDevServer | undefined
function onIndexUpdated() { function onIndexUpdated() {
@ -126,7 +120,7 @@ export async function localSearchPlugin(
const file = path.join(siteConfig.srcDir, page) const file = path.join(siteConfig.srcDir, page)
// get file metadata // get file metadata
const fileId = getDocId(file) const fileId = getDocId(file)
const locale = getLocaleForPath(file) const locale = getLocaleForPath(siteConfig.site, page)
const index = getIndexByLocale(locale) const index = getIndexByLocale(locale)
// retrieve file and split into "sections" // retrieve file and split into "sections"
const html = await render(file) const html = await render(file)

@ -72,6 +72,20 @@ export function isExternal(path: string): boolean {
return EXTERNAL_URL_RE.test(path) 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 * this merges the locales data to the main data by the route
*/ */
@ -79,13 +93,7 @@ export function resolveSiteDataByRoute(
siteData: SiteData, siteData: SiteData,
relativePath: string relativePath: string
): SiteData { ): SiteData {
const localeIndex = const localeIndex = getLocaleForPath(siteData, relativePath)
Object.keys(siteData.locales).find(
(key) =>
key !== 'root' &&
!isExternal(key) &&
isActive(relativePath, `/${key}/`, true)
) || 'root'
return Object.assign({}, siteData, { return Object.assign({}, siteData, {
localeIndex, localeIndex,

1
types/shared.d.ts vendored

@ -200,4 +200,5 @@ export interface MarkdownEnv {
links?: string[] links?: string[]
includes?: string[] includes?: string[]
realPath?: string realPath?: string
localeIndex?: string
} }

Loading…
Cancel
Save