Skip to content

Commit 0cbb469

Browse files
authoredMay 3, 2024··
feat(build): add localeIndex to md.env (#3862)
1 parent ed6ada7 commit 0cbb469

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed
 

‎src/node/markdown/plugins/containers.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type MarkdownIt from 'markdown-it'
2+
import container from 'markdown-it-container'
23
import type { RenderRule } from 'markdown-it/lib/renderer.mjs'
34
import type Token from 'markdown-it/lib/token.mjs'
4-
import container from 'markdown-it-container'
55
import { nanoid } from 'nanoid'
6+
import type { MarkdownEnv } from '../../shared'
7+
68
import {
79
extractTitle,
810
getAdaptiveThemeMarker,
@@ -60,7 +62,7 @@ function createContainer(
6062
container,
6163
klass,
6264
{
63-
render(tokens, idx, _options, env) {
65+
render(tokens, idx, _options, env: MarkdownEnv & { references?: any }) {
6466
const token = tokens[idx]
6567
const info = token.info.trim().slice(klass.length).trim()
6668
const attrs = md.renderer.renderAttrs(token)

‎src/node/markdownToVue.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import {
1111
} from './markdown/markdown'
1212
import {
1313
EXTERNAL_URL_RE,
14+
getLocaleForPath,
1415
slash,
16+
treatAsHtml,
1517
type HeadConfig,
1618
type MarkdownEnv,
17-
type PageData,
18-
treatAsHtml
19+
type PageData
1920
} from './shared'
2021
import { getGitTimestamp } from './utils/getGitTimestamp'
2122
import { processIncludes } from './utils/processIncludes'
@@ -95,13 +96,16 @@ export async function createMarkdownToVueRenderFn(
9596
let includes: string[] = []
9697
src = processIncludes(srcDir, src, fileOrig, includes)
9798

99+
const localeIndex = getLocaleForPath(siteConfig?.site, relativePath)
100+
98101
// reset env before render
99102
const env: MarkdownEnv = {
100103
path: file,
101104
relativePath,
102105
cleanUrls,
103106
includes,
104-
realPath: fileOrig
107+
realPath: fileOrig,
108+
localeIndex
105109
}
106110
const html = md.render(src, env)
107111
const {

‎src/node/plugins/localSearchPlugin.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Plugin, ViteDevServer } from 'vite'
77
import type { SiteConfig } from '../config'
88
import { createMarkdownRenderer } from '../markdown/markdown'
99
import {
10-
resolveSiteDataByRoute,
10+
getLocaleForPath,
1111
slash,
1212
type DefaultTheme,
1313
type MarkdownEnv
@@ -83,12 +83,6 @@ export async function localSearchPlugin(
8383
return index
8484
}
8585

86-
function getLocaleForPath(file: string) {
87-
const relativePath = slash(path.relative(siteConfig.srcDir, file))
88-
const siteData = resolveSiteDataByRoute(siteConfig.site, relativePath)
89-
return siteData?.localeIndex ?? 'root'
90-
}
91-
9286
let server: ViteDevServer | undefined
9387

9488
function onIndexUpdated() {
@@ -126,7 +120,7 @@ export async function localSearchPlugin(
126120
const file = path.join(siteConfig.srcDir, page)
127121
// get file metadata
128122
const fileId = getDocId(file)
129-
const locale = getLocaleForPath(file)
123+
const locale = getLocaleForPath(siteConfig.site, page)
130124
const index = getIndexByLocale(locale)
131125
// retrieve file and split into "sections"
132126
const html = await render(file)

‎src/shared/shared.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,28 @@ export function isExternal(path: string): boolean {
7272
return EXTERNAL_URL_RE.test(path)
7373
}
7474

75+
export function getLocaleForPath(
76+
siteData: SiteData | undefined,
77+
relativePath: string
78+
): string {
79+
return (
80+
Object.keys(siteData?.locales || {}).find(
81+
(key) =>
82+
key !== 'root' &&
83+
!isExternal(key) &&
84+
isActive(relativePath, `/${key}/`, true)
85+
) || 'root'
86+
)
87+
}
88+
7589
/**
7690
* this merges the locales data to the main data by the route
7791
*/
7892
export function resolveSiteDataByRoute(
7993
siteData: SiteData,
8094
relativePath: string
8195
): SiteData {
82-
const localeIndex =
83-
Object.keys(siteData.locales).find(
84-
(key) =>
85-
key !== 'root' &&
86-
!isExternal(key) &&
87-
isActive(relativePath, `/${key}/`, true)
88-
) || 'root'
96+
const localeIndex = getLocaleForPath(siteData, relativePath)
8997

9098
return Object.assign({}, siteData, {
9199
localeIndex,

‎types/shared.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,5 @@ export interface MarkdownEnv {
200200
links?: string[]
201201
includes?: string[]
202202
realPath?: string
203+
localeIndex?: string
203204
}

0 commit comments

Comments
 (0)
Please sign in to comment.