JSdoc 汉化主题
npm install better-docs-zh
基于 JSDoc3 的 Javascript / Typescript 项目文档工具箱,带有 @category、@component 和 @optional 插件。译者亲测,兼容 JSDoc4。(Fork 自 SoftwareBrothers 的 Better-Docs)
它长这样:
![]() | ![]() | ![]() |
在做汉化的过程中,我做了这些修改:
- 将文档模版中一些文案由英文替换成中文;
- 对一些字体样式和布局进行微调(毕竟中英文文档是有差异的);
- 将通过国外 CDN 加载的第三方库,改为本地文件加载(虽然包体积变大了,但是可以大大减少加载时间,同时也支持离线查看);
- 修改 README.md 文档中的配置示例(安装后,目录名称由 better-docs 变为 better-docs-zh);
- 发布时剔除掉一些不必要的文件和目录;
接下来如果有时间,也会对一些示例和截图做翻译和调整。
示例文档可在此处找到: https://softwarebrothers.github.io/example-design-system/index.html
``sh`
npm install --save-dev better-docs-zh
假设你已经全局安装了 jsdoc:
`sh`
jsdoc your-documented-file.js -t ./node_modules/better-docs-zh
在你项目中的 package.json 文件,添加一个新脚本:
` json`
"script": {
"docs": "jsdoc -c jsdoc.json"
}
在你的 jsdoc.json 文件中,设置模板:
`json`
"opts": {
"template": "node_modules/better-docs-zh"
}
better-docs 有一个插件,允许您从 TypeScript 代码生成文档。
更新您的 jsdoc.json 文件:
`json`
...
"tags": {
"allowUnknownTags": ["optional"] //or true
},
"plugins": [
"node_modules/better-docs-zh/typescript",
"node_modules/better-docs-zh/category",
"node_modules/better-docs-zh/component",
"node_modules/better-docs-zh/load"
],
"source": {
"include": ["src"],
"includePattern": "\\.(jsx|js|ts|tsx)$",
},
...
现在,您可以运行命令 jsdoc 并解析 TypeScript 文件。
它执行 4 个操作:
* 首先,它将所有 .ts 和 .tsx 文件转译为 .js,以便您使用的所有注释都被视为常规的 JSDoc 注释。
此外,它还会:
* 将所有注释了 type 的别名转为 @typedefinterface
* 将所有注释了 的定义转换为 @interfacepublic
* 转换类成员的修饰符,包括:、protected、static
因此,JSDoc 可以自动打印它们。
`javascript
/**
* ActionRequest
* @memberof Action
* @alias ActionRequest
*/
export type ActionRequest = {
/**
* parameters passed in an URL
*/
params: {
/**
* Id of current resource
*/
resourceId: string;
/**
* Id of current record
*/
recordId?: string;
/**
* Name of an action
*/
action: string;
[key: string]: any;
};
}
`
转换为:
`javascript`
/**
* ActionRequest'
* @memberof Action'
* @alias ActionRequest'
* @typedef {object} ActionRequest'
* @property {object} params parameters passed in an URL'
* @property {string} params.resourceId Id of current resource'
* @property {string} [params.recordId] Id of current record'
* @property {string} params.action Name of an action'
* @property {any} params.{...}'
*/
您也可以以类似的方式注释接口:
`javascript`
/**
* JSON representation of an {@link Action}
* @see Action
*/
export default interface ActionJSON {
/**
* Unique action name
*/
name: string;
/**
* Type of an action
*/
actionType: 'record' | 'resource' | Array<'record' | 'resource'>;
/**
* Action icon
*/
icon?: string;
/**
* Action label - visible on the frontend
*/
label: string;
/**
* Guarding message
*/
guard?: string;
/**
* If action should have a filter (for resource actions)
*/
showFilter: boolean;
/**
* Action component. When set to false action will be invoked immediately after clicking it,
* to put in another words: there wont be an action view
*/
component?: string | false | null;
}
或者像这样描述你的类属性:
`javascript
/**
* Class name
*/
class ClassName {
/**
* Some private member which WONT be in jsdoc (because it is private)
*/
private name: string
/**
* Some protected member which will go to the docs
*/
protected somethingIsA: number
/**
* And static member which will goes to the docs.
*/
static someStaticMember: number
public notCommentedWontBeInJSDoc: string
constructor(color: string) {}
}
`
better-docs 还允许您将文档嵌套到侧边栏菜单中的类别和子类别中。
在 jsdoc.json 文件中添加插件 - 更新 plugins 部分:
`json`
...
"tags": {
"allowUnknownTags": ["category"] //or true
},
"plugins": [
"node_modules/better-docs-zh/category"
],
...
然后,您可以在代码中使用 @category 和/或 @subcategory 标记:
`javascript`
/**
* Class description
* @category Category
* @subcategory All
*/
class YourClass {
....
}
Better-docs 还可以自动为您的 React 和 Vue 组件生成文档。您唯一需要做的就是添加一个 @component 标签。它将从您的组件中获取所有属性,并带有一个 @example 标签 - 将生成一个 __实时预览__。
与之前添加插件类似 - 您必须更新 jsdoc.json 文件中的 plugins 部分:
`json`
...
"tags": {
"allowUnknownTags": ["component"] //or true
},
"plugins": [
"node_modules/better-docs-zh/component"
],
...
由于 __component__ 插件使用 parcel 作为打包器,因此您必须全局安装它。为此,请运行:
`shif you use npm
npm install -g parcel-bundler
用法
要在您的 JSDoc 文档中记录组件,只需添加
@component:`jsx
/**
* Some documented component
*
* @component
*/
const Documented = (props) => {
const { text } = props
return (
{text}
)
}Documented.propTypes = {
/**
* Text is a text
*/
text: PropTypes.string.isRequired,
}
export default Documented
`该插件将从您的 PropTypes 中获取信息,并将它们放入数组中。
对于 Vue,它看起来类似:
`vue
`在这种情况下,属性将从
props 中获取。预览
@component 插件还会修改 @example 标签的行为,以便它可以生成实际的 __组件预览__。你要做的就是添加一个 @example 标签,并从中返回组件:React 示例:
`jsx
/**
* Some documented component
*
* @component
* @example
* const text = 'some example text'
* return (
*
* )
*/
const Documented = (props) => {
///...
}
`Vue 示例1:
`vue
`Vue 示例2:
`vue
`您可以在一个组件中放置任意数量的
@example 标签,并像这样“描述”每个标签:`javascript
/**
* @component
* @example Example usage of method1.
* // your example here
*/
`在预览中混合组件
此外,您也可以使用
@component 标签来同时记录多个组件。因此,假设您有 2 个组件,在第二个组件中,您希望将第一个组件用作包装器,如下所示:`javascript
// component-1.js
/**
* Component 1
* @component
*
*/
const Component1 = (props) => {...}// component-2.js
/**
* Component 2
* @component
* @example
* return (
*
*
*
*
* )
*/
const Component2 = (props) => {...}
`包装器组件 [仅限 React]
最有可能的是,您的组件必须在特定的上下文中运行,比如在 redux store provider 中或使用自定义 CSS 库。您可以通过向
jsdoc.json 文件中的 component.wrapper 传递选项来模拟 _(要阅读有关选项的更多信息 - 向下滚动到 __自定义__ 部分)_`json
// jsdoc.json
{
"opts": {...},
"templates": {
"better-docs": {
"name": "Sample Documentation",
"component": {
"wrapper": "./path/to/your/wrapper-component.js",
},
"...": "...",
}
}
}
`包装器组件可以如下所示:
`javascript
// wrapper-component.js
import React from 'react'
import { BrowserRouter } from 'react-router-dom'
import { createStore } from 'redux'
import { Provider } from 'react-redux'const store = createStore(() => ({}), {})
const Component = (props) => {
return (
{props.children}
)
}
export default Component
`样式化 React 示例
Better-docs inserts all examples within an
iframe. This results in the following styling options:1. If you pass styles inline - they will work right away.
2. For
css modules to work with parcel bundler - you have to install postcss-modules package:`
yarn add postcss-modules
`and create a
.postcssrc file:
`json
// .postcssrc
{
"modules": true
}
`3. For styled-components you have to use wrapper component which looks like this:
`jsx
import React from 'react'
import { StyleSheetManager } from 'styled-components'const Component = (props) => {
const { frameContext } = props
return (
{props.children}
)
}
export default Component
`Adding commands to bundle entry file
@component plugin creates an entry file: .entry.js in your _docs_ output folder. Sometimes you might want to add something to it. You can do this by passing: component.entry option, which is an array of strings.So let's say you want to add
babel-polyfill and 'bulma.css' framework to your bundle. You can do it like this:`json
// jsdoc.json
{
"opts": {...},
"templates": {
"better-docs": {
"name": "Sample Documentation",
"component": {
"entry": [
"import 'babel-polyfill';",
"import 'bulma/css/bulma.css';"
]
},
"...": "...",
}
}
}
`Customization
First of all, let me state that better-docs extends the
default template. That is why default template parameters are also handled.[BETA]: You must explicitly set the
search option of the default template to true to enable searchTo customize the better-docs pass
options to templates['better-docs']. section in your jsdoc configuration file.Example configuration file with settings for both
default and better-docs templates:`json
{
"tags": {
"allowUnknownTags": ["category"]
},
"source": {
"include": ["./src"],
"includePattern": ".js$",
"excludePattern": "(node_modules/|docs)"
},
"plugins": [
"plugins/markdown",
"jsdoc-mermaid",
"node_modules/better-docs-zh/category"
],
"opts": {
"encoding": "utf8",
"destination": "docs/",
"readme": "readme.md",
"recurse": true,
"verbose": true,
"tutorials": "./docs-src/tutorials",
"template": "better-docs"
},
"templates": {
"cleverLinks": false,
"monospaceLinks": false,
"search": true,
"default": {
"staticFiles": {
"include": [
"./docs-src/statics"
]
}
},
"better-docs": {
"name": "Sample Documentation",
"logo": "images/logo.png",
"title": "", // HTML title
"css": "style.css",
"trackingCode": "tracking-code-which-will-go-to-the-HEAD",
"hideGenerator": false,
"navLinks": [
{
"label": "Github",
"href": "https://github.com/SoftwareBrothers/admin-bro"
},
{
"label": "Example Application",
"href": "https://admin-bro-example-app-staging.herokuapp.com/admin"
}
]
}
},
"sidebar": {
"global": {
"hideItems": true
}
}
}
`Extras
$3
better-docs also has one extra plugin for handling typescript'like types imports like (it has to be one-liner):
`javascript
/* @typedef {import('./some-other-file').ExportedType} ExportedType /
`It simply removes that from the code so JSDoc wont throw an error. In order to use it add this plugin to your plugins section:
`json
"plugins": [
"node_modules/better-docs-zh/typedef-import"
],
`Setting up for the development
If you want to change the theme locally follow the steps:
1. Clone the repo to the folder where you have the project:
`sh
cd your-project
git clone git@github.com:atypiape/better-docs.git
`or add it as a git submodule:
`sh
git submodule add git@github.com:atypiape/better-docs.git
`2. Install the packages
`sh
cd better-docsnpm install
or
yarn
`3. Within the better-docs folder run the gulp script. It will regenerate documentation every time you change something.
It supports following ENV variables:
*
DOCS_COMMAND - a command in your root repo which you use to generate documentation: i.e. DOCS_COMMAND='jsdoc -c jsdoc.json' or npm run docs if you have docs command defined in package.json file
* DOCS_OUTPUT - where your documentation is generated. It should point to the same folder your jsdoc --destination conf. But make sure that it is relative to the path where you cloned better-docs.
* DOCS - list of folders from your original repo what you want to watch for changes. Separated by comma.`
cd better-docs
DOCS_COMMAND='npm run docs' DOCS=../src//,../config// DOCS_OUTPUT=../docs cd better-docs && gulp
`The script should launch the browser and refresh it whenever you change something in the template or in
DOCS`.If you want to see how to setup jsdoc in your project - take a look at these brief tutorials:
- JSDoc - https://www.youtube.com/watch?v=Yl6WARA3IhQ
- better-docs and Mermaid: https://www.youtube.com/watch?v=UBMYogTzsBk
better-docs is Copyright © 2019 SoftwareBrothers.co. It is free software and may be redistributed under the terms specified in the LICENSE file - MIT.
We're an open, friendly team that helps clients from all over the world to transform their businesses and create astonishing products.
* We are available for hire.
* If you want to work for us - check out the career page.