Skip to content

Commit

Permalink
feat(build): add localeIndex to md.env (#3862)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed May 3, 2024
1 parent ed6ada7 commit 0cbb469
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/node/markdown/plugins/containers.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions src/node/plugins/localSearchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 15 additions & 7 deletions src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,28 @@ 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
*/
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,
Expand Down
1 change: 1 addition & 0 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,5 @@ export interface MarkdownEnv {
links?: string[]
includes?: string[]
realPath?: string
localeIndex?: string
}

0 comments on commit 0cbb469

Please sign in to comment.