Skip to content

Commit

Permalink
[gem-book] Fixed self language detect without i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Jan 8, 2024
1 parent 8337376 commit 47f49c3
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/gem-book/docs/zh/002-guide/001-sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ src/docs/
```

> [!TIP]
> 用文件名进行排序的好处是你能在 IDE 中预览排序后的结果,可以使用 [`--reverse`](./004-metadata.md#dir) 进行降序显示侧边栏
> 用文件名进行排序的好处是你在 IDE 中看到的文档顺序基本就是网站侧边栏中的顺序,可以使用 [`reverse`](./004-metadata.md#dir) 进行降序显示侧边栏。
> 另外,在文档中使用链接时允许不带这些权重数字,但这样会失去 IDE 支持,所以不建议这么做。
默认情况下,这个数字不会显示在 URL 中,如果你的文件名本来就有相同格式的前缀,可以使用 [`--display-rank`](./003-cli.md#--display-rank) 选项显示它们。
2 changes: 1 addition & 1 deletion packages/gem-book/docs/zh/002-guide/003-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ npx gem-book docs \

#### `--json`

只输出 `<gem-book>` 使用的 json 渲染数据,默认为 `gem-book.json`
只输出 `<gem-book>` 使用的 `json` 渲染数据,默认输出 `gem-book.json`,使用 [`--output`](#-o,---output-path) 自定义

#### `--config <path>`

Expand Down
2 changes: 1 addition & 1 deletion packages/gem-book/docs/zh/003-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ yarn add gem-book

## `<gbp-include>`

动态加载显示 MarkDown 片段:
动态加载 Markdown 片段:

<gbp-include src="./002-guide/007-extension.md" range="[!NOTE]->"></gbp-include>

Expand Down
2 changes: 1 addition & 1 deletion packages/gem-book/gem-book.cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"i18n": true,
"homeMode": true,
"sourceBranch": "docs",
"plugin": ["raw", "docsearch", "api", "sandpack", "code-group", "import"],
"plugin": ["raw", "docsearch", "api", "sandpack", "code-group", "import", "include", "var", "example"],
"template": "docs/template.html",
"ga": "G-PZYZ441YD3",
"debug": true,
Expand Down
7 changes: 6 additions & 1 deletion packages/gem-book/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ program
}

const filePath = path.relative(dir, filePathWithDir);
const fullPath = path.join(docsRootDir, filePath);

if (!isDirConfigFile(filePath) && !isMdFile(filePath)) {
devServerEventTarget.dispatchEvent(
Expand All @@ -428,7 +429,11 @@ program
return updateBookConfig();
}

const { content, metadataChanged } = getMdFile(path.join(docsRootDir, filePath), bookConfig.displayRank);
if (cliConfig.debug) {
checkRelativeLink(fullPath, docsRootDir);
}

const { content, metadataChanged } = getMdFile(fullPath, bookConfig.displayRank);
devServerEventTarget.dispatchEvent(
Object.assign(new Event(UPDATE_EVENT), {
detail: { filePath, content },
Expand Down
4 changes: 2 additions & 2 deletions packages/gem-book/src/element/elements/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const parser = new DOMParser();
// https://github.com/w3c/csswg-drafts/issues/9712
const style = createCSSSheet(css`
:not(gbp-var):not(:defined) {
display: contents;
display: block;
margin-block: 2rem;
color: transparent;
/* maybe browser limit */
font-size: 0;
Expand All @@ -40,7 +41,6 @@ const style = createCSSSheet(css`
display: block;
content: 'The element is not defined';
padding: 2rem;
margin-block: 2rem;
text-align: center;
color: ${theme.textColor};
background: ${theme.borderColor};
Expand Down
5 changes: 4 additions & 1 deletion packages/gem-book/src/element/elements/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Main } from './main';
/**
* 获取资源的远端 GitHub raw 地址,如果使用 `DEV_MODE`,则返回本机服务的 URL
*
* - 优先使用 markdown 文件
* - 支持相对路径
* - `/docs/readme.md` 和 `docs/readme.md` 等效
*/
Expand All @@ -29,7 +30,9 @@ function getRemoteURL(originSrc = '', dev = GemBookPluginElement.devMode) {
let src = originSrc.startsWith('/') ? originSrc : `/${originSrc}`;
if (originSrc.startsWith('.')) {
const absPath = new URL(originSrc, `${location.origin}${originLink}`).pathname;
const link = links?.find(({ originLink }) => originLink === absPath);
const link = links?.find(({ originLink, link, userFullPath }) =>
[originLink, link, userFullPath].some((path) => path === absPath || `${path}.md` === absPath),
);
if (link) return getURL(joinPath(lang, link.originLink), link.hash);
src = new URL(originSrc, `${location.origin}${joinPath(sourceDir, lang, originLink)}`).pathname;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/gem-book/src/element/helper/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ export const fallbackLanguage = document.documentElement.lang;
export const selfI18n = new I18n<typeof resources.en>({
fallbackLanguage: fallbackLanguage in resources ? fallbackLanguage : 'en',
resources,
// 没有启动 i18n 时由 `html[lang]` 定义语言
currentLanguage: fallbackLanguage,
});

0 comments on commit 47f49c3

Please sign in to comment.