Skip to content

Commit 32c25bb

Browse files
author
antalike
committed
Replace domains in content during compilation
1 parent 5b4cd69 commit 32c25bb

6 files changed

Lines changed: 87 additions & 55 deletions

File tree

app/composables/useVariableReplacement.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,23 @@ export const useVariableReplacement = () => {
44
const replaceVariables = (text: string) => {
55
if (!text) return text
66

7-
return text.replace(/{{(\w+)}}/g, (match, key) => {
8-
return (config.public as unknown as Record<string, string>)[key] || match
7+
let newText = text
8+
9+
const domains: Record<string, string> = {
10+
'https://cdn.memtensor.com.cn': config.public.cdnUrl as string,
11+
'https://statics.memtensor.com.cn': config.public.staticCdnUrl as string,
12+
'https://memos-playground.openmem.net': config.public.playgroundUrl as string,
13+
'https://memos-dashboard.openmem.net': config.public.dashboardUrl as string,
14+
'https://memos.openmem.net': config.public.homeDomain as string
15+
}
16+
17+
Object.keys(domains).forEach((key) => {
18+
if (domains[key]) {
19+
newText = newText.split(key).join(domains[key])
20+
}
921
})
22+
23+
return newText
1024
}
1125

1226
return {

envConfig/config.intl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default {
22
env: 'intl',
3-
homeDomain: 'https://memos.memtensor.net/',
4-
openMemUrl: 'https://openmem.net/',
3+
homeDomain: 'https://memos.memtensor.net',
4+
openMemUrl: 'https://openmem.net',
55
githubMemosUrl: 'https://github.com/MemTensor/MemOS',
66
dashboardUrl: 'https://memos-dashboard.memtensor.net',
77
playgroundUrl: 'https://memos-playground.memtensor.net',

envConfig/config.prod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default {
22
env: 'prod',
33
homeDomain: 'https://memos.openmem.net',
4-
openMemUrl: 'https://openmem.net/',
4+
openMemUrl: 'https://openmem.net',
55
githubMemosUrl: 'https://github.com/MemTensor/MemOS',
66
dashboardUrl: 'https://memos-dashboard.openmem.net',
77
playgroundUrl: 'https://memos-playground.openmem.net'

nuxt.config.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import yaml from '@rollup/plugin-yaml'
22
import type { NuxtConfig } from '@nuxt/schema'
33
import pkg from './package.json'
44
import { getCnRoutes, getCnApiReferenceRoutes } from './scripts/extract-routes.mjs'
5-
import remarkVariables from './scripts/remark-variables.mjs'
5+
import remarkReplaceDomains from './scripts/remark-replace-domains.mjs'
66

77
const cnRoutes = getCnRoutes()
88
const cnApiRoutes = getCnApiReferenceRoutes()
@@ -18,9 +18,6 @@ const envConfig = await import(`./envConfig/config.${env}.ts`).then(m => m.defau
1818

1919
const staticCdnUrl = envConfig.staticCdnUrl || 'https://statics.memtensor.com.cn'
2020
const cdnUrl = envConfig.cdnUrl || 'https://cdn.memtensor.com.cn'
21-
const dashboardUrl = envConfig.dashboardUrl || 'https://memos-dashboard.openmem.net'
22-
const playgroundUrl = envConfig.playgroundUrl || 'https://memos-playground.openmem.net'
23-
const landingUrl = envConfig.homeDomain || 'https://memos.openmem.net'
2421

2522
const config: NuxtConfig = {
2623
app: {
@@ -46,10 +43,7 @@ const config: NuxtConfig = {
4643
version: pkg.version,
4744
apiBase: 'https://apigw.memtensor.cn',
4845
staticCdnUrl,
49-
cdnUrl,
50-
dashboardUrl,
51-
playgroundUrl,
52-
landingUrl
46+
cdnUrl
5347
}
5448
},
5549

@@ -87,7 +81,7 @@ const config: NuxtConfig = {
8781
},
8882
build: {
8983
rollupOptions: {
90-
external: ['remark-variables']
84+
external: ['remark-replace-domains']
9185
}
9286
}
9387
},
@@ -105,15 +99,17 @@ const config: NuxtConfig = {
10599
build: {
106100
markdown: {
107101
remarkPlugins: {
108-
'remark-variables': {
109-
instance: remarkVariables,
102+
'remark-replace-domains': {
103+
instance: remarkReplaceDomains,
110104
options: {
111-
variables: {
112-
staticCdnUrl,
113-
cdnUrl,
114-
dashboardUrl,
115-
playgroundUrl,
116-
landingUrl
105+
imageDomains: {
106+
'https://cdn.memtensor.com.cn': cdnUrl,
107+
'https://statics.memtensor.com.cn': staticCdnUrl
108+
},
109+
linkDomains: {
110+
'https://memos-playground.openmem.net': envConfig.playgroundUrl,
111+
'https://memos-dashboard.openmem.net': envConfig.dashboardUrl,
112+
'https://memos.openmem.net': envConfig.homeDomain
117113
}
118114
}
119115
}

scripts/remark-replace-domains.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export default function remarkReplaceDomains(options = {}) {
2+
const imageDomains = options.imageDomains || {}
3+
const linkDomains = options.linkDomains || {}
4+
5+
function escapeRegExp(string) {
6+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
7+
}
8+
9+
return (tree) => {
10+
function visit(node) {
11+
// Handle standard markdown images: ![alt](url)
12+
if (node.type === 'image' && node.url && typeof node.url === 'string') {
13+
Object.keys(imageDomains).forEach((key) => {
14+
if (node.url.startsWith(key)) {
15+
node.url = node.url.replace(key, imageDomains[key])
16+
}
17+
})
18+
}
19+
20+
// Handle standard markdown links: [text](url)
21+
if (node.type === 'link' && node.url && typeof node.url === 'string') {
22+
Object.keys(linkDomains).forEach((key) => {
23+
if (node.url.startsWith(key)) {
24+
node.url = node.url.replace(key, linkDomains[key])
25+
}
26+
})
27+
}
28+
29+
// Handle HTML nodes for both images and links
30+
if (node.type === 'html' && node.value && typeof node.value === 'string') {
31+
// Replace image sources
32+
Object.keys(imageDomains).forEach((key) => {
33+
const regex = new RegExp(`(<img\\s+(?:[^>]*?\\s+)?src=["'])(${escapeRegExp(key)})([^"']*["'][^>]*>)`, 'gi')
34+
node.value = node.value.replace(regex, (match, p1, p2, p3) => {
35+
return `${p1}${imageDomains[key]}${p3}`
36+
})
37+
})
38+
39+
// Replace link hrefs
40+
Object.keys(linkDomains).forEach((key) => {
41+
const regex = new RegExp(`(<a\\s+(?:[^>]*?\\s+)?href=["'])(${escapeRegExp(key)})([^"']*["'][^>]*>)`, 'gi')
42+
node.value = node.value.replace(regex, (match, p1, p2, p3) => {
43+
return `${p1}${linkDomains[key]}${p3}`
44+
})
45+
})
46+
}
47+
48+
if (node.children && Array.isArray(node.children)) {
49+
node.children.forEach(visit)
50+
}
51+
}
52+
53+
visit(tree)
54+
}
55+
}

scripts/remark-variables.mjs

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)