Skip to content

Commit c7f0c43

Browse files
feat(plugin-external-link-icon): add locales option (#636)
Co-authored-by: meteorlxy <[email protected]>
1 parent f8481eb commit c7f0c43

File tree

17 files changed

+186
-42
lines changed

17 files changed

+186
-42
lines changed

docs/reference/plugin/external-link-icon.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@ This plugin has been integrated into the default theme.
1212
npm i -D @vuepress/plugin-external-link-icon@next
1313
```
1414

15+
## Options
16+
17+
### locales
18+
19+
- Type: `Record<string, { openInNewWindow: string }>`
20+
21+
- Details:
22+
23+
The a11y text of the external link icon in different locales.
24+
25+
If this option is not specified, it will fallback to default text.
26+
27+
- Example:
28+
29+
```js
30+
module.exports = {
31+
plugins: [
32+
[
33+
'@vuepress/plugin-external-link-icon',
34+
{
35+
locales: {
36+
'/': {
37+
openInNewWindow: 'open in new window',
38+
},
39+
'/zh/': {
40+
openInNewWindow: '在新窗口打开',
41+
},
42+
},
43+
},
44+
],
45+
],
46+
}
47+
```
48+
49+
- Also see:
50+
- [Guide > I18n](../../guide/i18n.md)
51+
1552
## Frontmatter
1653

1754
### externalLinkIcon

docs/zh/reference/plugin/external-link-icon.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@
1212
npm i -D @vuepress/plugin-external-link-icon@next
1313
```
1414

15+
## 配置项
16+
17+
### locales
18+
19+
- 类型: `Record<string, { openInNewWindow: string }>`
20+
21+
- 详情:
22+
23+
外部链接图标在不同 locales 下的 A11y 文字。
24+
25+
如果没有指定该配置项,它会降级使用默认文字。
26+
27+
- 示例:
28+
29+
```js
30+
module.exports = {
31+
plugins: [
32+
[
33+
'@vuepress/plugin-external-link-icon',
34+
{
35+
locales: {
36+
'/': {
37+
openInNewWindow: 'open in new window',
38+
},
39+
'/zh/': {
40+
openInNewWindow: '在新窗口打开',
41+
},
42+
},
43+
},
44+
],
45+
],
46+
}
47+
```
48+
49+
- 参考:
50+
- [指南 > 多语言支持](../../guide/i18n.md)
51+
1552
## Frontmatter
1653

1754
### externalLinkIcon
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { defineClientAppEnhance } from '@vuepress/client'
2+
import { h } from 'vue'
3+
import type { ExternalLinkIconLocales } from '../shared'
24
import { ExternalLinkIcon } from './components/ExternalLinkIcon'
35

6+
declare const __EXTERNAL_LINK_ICON_LOCALES__: ExternalLinkIconLocales
7+
8+
const locales = __EXTERNAL_LINK_ICON_LOCALES__
9+
410
export default defineClientAppEnhance(({ app }) => {
5-
app.component('ExternalLinkIcon', ExternalLinkIcon)
11+
// wrap the `<ExternalLinkIcon />` component with plugin options
12+
app.component('ExternalLinkIcon', h(ExternalLinkIcon, { locales }))
613
})

packages/@vuepress/plugin-external-link-icon/src/client/components/ExternalLinkIcon.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { h } from 'vue'
2-
import type { FunctionalComponent } from 'vue'
1+
import { useRouteLocale } from '@vuepress/client'
2+
import { computed, defineComponent, h } from 'vue'
3+
import type { PropType } from 'vue'
4+
import type { ExternalLinkIconLocales } from '../../shared'
35

46
import '../styles/vars.css'
57
import '../styles/external-link-icon.css'
@@ -30,7 +32,35 @@ const svg = h(
3032
]
3133
)
3234

33-
export const ExternalLinkIcon: FunctionalComponent = (_, { slots }) =>
34-
h('span', [svg, slots.default?.()])
35+
export const ExternalLinkIcon = defineComponent({
36+
name: 'ExternalLinkIcon',
3537

36-
ExternalLinkIcon.displayName = 'ExternalLinkIcon'
38+
props: {
39+
locales: {
40+
type: Object as PropType<ExternalLinkIconLocales>,
41+
required: false,
42+
default: () => ({}),
43+
},
44+
},
45+
46+
setup(props) {
47+
const routeLocale = useRouteLocale()
48+
const locale = computed(
49+
() =>
50+
props.locales[routeLocale.value] ?? {
51+
openInNewWindow: 'open in new window',
52+
}
53+
)
54+
return () =>
55+
h('span', [
56+
svg,
57+
h(
58+
'span',
59+
{
60+
class: 'external-link-icon-sr-only',
61+
},
62+
locale.value.openInNewWindow
63+
),
64+
])
65+
},
66+
})

packages/@vuepress/plugin-external-link-icon/src/client/styles/external-link-icon.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@
55
vertical-align: middle;
66
top: -1px;
77
}
8+
9+
.external-link-icon-sr-only {
10+
position: absolute;
11+
width: 1px;
12+
height: 1px;
13+
padding: 0;
14+
margin: -1px;
15+
overflow: hidden;
16+
clip: rect(0, 0, 0, 0);
17+
white-space: nowrap;
18+
border-width: 0;
19+
user-select: none;
20+
}

packages/@vuepress/plugin-external-link-icon/src/node/externalLinkIconPlugin.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import type { Plugin } from '@vuepress/core'
22
import type { MarkdownEnv } from '@vuepress/markdown'
33
import { path } from '@vuepress/utils'
4+
import type { ExternalLinkIconLocales } from '../shared'
45

5-
export type ExternalLinkIconPluginOptions = Record<never, never>
6+
/**
7+
* Options for @vuepress/plugin-external-link-icon
8+
*/
9+
export type ExternalLinkIconPluginOptions = {
10+
locales?: ExternalLinkIconLocales
11+
}
612

7-
export const externalLinkIconPlugin: Plugin<ExternalLinkIconPluginOptions> = {
13+
export const externalLinkIconPlugin: Plugin<ExternalLinkIconPluginOptions> = ({
14+
locales = {},
15+
}) => ({
816
name: '@vuepress/plugin-external-link-icon',
917

18+
define: {
19+
__EXTERNAL_LINK_ICON_LOCALES__: locales,
20+
},
21+
1022
clientAppEnhanceFiles: path.resolve(
1123
__dirname,
1224
'../client/clientAppEnhance.js'
@@ -44,4 +56,4 @@ export const externalLinkIconPlugin: Plugin<ExternalLinkIconPluginOptions> = {
4456
return result
4557
}
4658
},
47-
}
59+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './types'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { LocaleConfig } from '@vuepress/shared'
2+
3+
export type ExternalLinkIconLocales = LocaleConfig<{
4+
openInNewWindow: string
5+
}>

packages/@vuepress/plugin-external-link-icon/tsconfig.cjs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"rootDir": "./src",
66
"outDir": "./lib"
77
},
8-
"include": ["./src/node"]
8+
"include": ["./src/node", "./src/shared"]
99
}

packages/@vuepress/plugin-external-link-icon/tsconfig.esm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"rootDir": "./src",
66
"outDir": "./lib"
77
},
8-
"include": ["./src/client"]
8+
"include": ["./src/client", "./src/shared"]
99
}

0 commit comments

Comments
 (0)