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 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)

@ -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 {

@ -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)

@ -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,

1
types/shared.d.ts vendored

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

Loading…
Cancel
Save