Skip to content

Commit

Permalink
docs(cn): 翻译 docs/javascript-api/index.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenQingchuan committed Mar 5, 2023
1 parent 7f6401f commit 48784ea
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 111 deletions.
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
npm run build:docs
git add -A
68 changes: 68 additions & 0 deletions docs/.vitepress/colorfulString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable sort-keys */
export type ColorfulOptions =
| 'bold'
| 'italic'
| 'underline'
| 'inverse'
| 'strikethrough'
| 'white'
| 'grey'
| 'black'
| 'blue'
| 'cyan'
| 'green'
| 'magenta'
| 'red'
| 'yellow'
| 'bgWhite'
| 'bgGrey'
| 'bgBlack'
| 'bgBlue'
| 'bgCyan'
| 'bgGreen'
| 'bgMagenta'
| 'bgRed'
| 'bgYellow';

const colorMap: Record<ColorfulOptions, string> = {
bold: '1',
italic: '3',
underline: '4',
inverse: '7',
strikethrough: '9',
white: '37',
grey: '90',
black: '30',
blue: '34',
cyan: '36',
green: '32',
magenta: '35',
red: '31',
yellow: '33',
bgWhite: '47',
bgGrey: '49',
bgBlack: '40',
bgBlue: '44',
bgCyan: '46',
bgGreen: '42',
bgMagenta: '45',
bgRed: '41',
bgYellow: '43'
};

/**
* Transform a string to ascii colorful string.
*
* @param str String to be colored.
* @param options Colorful options.
* @returns Ascii colorful string.
*/
export function colorful(string_: string, options: ColorfulOptions[] = []) {
const colors = options.map(option => colorMap[option]).join(';');

return `\u001B[${colors}m${string_}\u001B[0m`;
}

export const yellowBold = (string_: string) => colorful(string_, ['yellow', 'bold']);
export const redBold = (string_: string) => colorful(string_, ['red', 'bold']);
export const greenBold = (string_: string) => colorful(string_, ['green', 'bold']);
13 changes: 10 additions & 3 deletions docs/.vitepress/verify-anchors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync, writeFileSync } from 'node:fs';
import * as rollupLinks from '../../src/utils/urls';
import { greenBold, yellowBold } from './colorfulString';

const markdownLinkRegExp = /(?<=\[[^\]]*]\()[^)]*/g;
const mermaidLinkRegExp = /(?<=```mermaid[^`]*"#)[^"]+/gm;
Expand Down Expand Up @@ -56,20 +57,26 @@ function verifyAnchorsOnPage(page: string, slugs: Set<string>) {
const [linkPage, anchor] = href.split('#');
if (anchor && !slugsByPage.get(linkPage.slice('../'.length))!.has(anchor)) {
throw new Error(
`Page ${page} references anchor ${anchor} on page ${linkPage} but it cannot be found.`
`Page ${yellowBold(page)} references anchor ${yellowBold(anchor)} on page ${yellowBold(
linkPage
)} but it cannot be found.`
);
}
} else if (!(href.startsWith('https://') || href.startsWith('<https://'))) {
throw new Error(
`Unexpected internal link in ${page}: ${href}. Relative links should be of the form ../page/index.md, absolute links should start with https://.`
`Unexpected internal link in ${yellowBold(page)}: ${greenBold(
href
)}. Relative links should be of the form ../page/index.md, absolute links should start with https://.`
);
}
}
while ((match = mermaidLinkRegExp.exec(text)) !== null) {
const anchor = match[0];
if (!slugs.has(anchor)) {
throw new Error(
`Page ${page} references anchor ${anchor} in a mermaid graph but it cannot be found on this page. Slugs found on this page:\n${[
`Page ${yellowBold(page)} references anchor ${yellowBold(
anchor
)} in a mermaid graph but it cannot be found on this page. Slugs found on this page:\n${[
...slugs
]
.sort()
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/en/slugs-and-pages-by-legacy-slugs.json

Large diffs are not rendered by default.

Loading

0 comments on commit 48784ea

Please sign in to comment.