在 markdown 文档中显示代码之间的差别的 Gitbook 插件javascript
{% diff method="diffJson" %} ```json { "name": "gitbook-plugin-simple-mind-map", "version": "0.2.1", "description": "A gitBook plugin for generating and exporting mind map within markdown" } ``` ```json { "name": "gitbook-plugin-diff", "version": "0.2.1", "description": "A gitbook plugin for showing the differences between the codes within markdown" } ``` {% enddiff %}
{ - "description": "A gitBook plugin for generating and exporting mind map within markdown", - "name": "gitbook-plugin-simple-mind-map", + "description": "A gitbook plugin for showing the differences between the codes within markdown", + "name": "gitbook-plugin-diff", "version": "0.2.1" }
book.json
文件在您的 gitbook
的 book.json
文件中,将 diff
添加到 plugins
列表中.java
这是最简单的示例:node
{ "plugins": ["diff"] }
此外,受支持的配置选项以下:git
"gitbook": { "properties": { "method": { "type": "string", "title": "jsdiff api method", "required": false, "default": "diffLines", "description": "some supported methods such as diffChars or diffWords or diffWordsWithSpace or diffLines or diffTrimmedLines or diffSentences or diffCss or diffJson or diffArrays" }, "options": { "type": "object", "title": "jsdiff api options", "required": false, "description": "some methods may not support options" } } }
markdown
语法diff
插件支持 method
和 options
等选项生成差别代码块.github
这是在 markdown
文件中基本使用语法:web
{% diff %} ``` old code ``` ``` new code ``` {% enddiff %}
下面有一些基本示例:spring
Diff.diffChars(oldStr, newStr[, options])
区分两个文本块,逐个字符比较。docker
返回npm
返回更改对象列表(请参见下文)。
选项
ignoreCase
: true
忽略大小写差别。默认为false
。示例
设置
method="diffChars"
以调用Diff.diffChars(oldStr, newStr[, options])
方法
{% diff method="diffChars" %} ```js cat ``` ```js cap ``` {% enddiff %}
ca - t + p
Diff.diffWords(oldStr, newStr[, options])
区分两个文本块,逐字比较,忽略空格。
返回
返回更改对象列表(请参见下文)。
选项
ignoreCase
:与diffChars
相同。示例
设置
method="diffWords"
来调用Diff.diffWords(oldStr, newStr[, options])
方法
{% diff method="diffWords" %} ```bash beep boop ``` ```bash beep boob blah ``` {% enddiff %}
beep - boop + boob + blah
Diff.diffWordsWithSpace(oldStr, newStr[, options])
区分两个文本块,逐字比较,将空白视为有效。
返回
返回更改对象列表(请参见下文)。
选项
ignoreCase
:与diffWords
相同。示例
设置
method="diffWordsWithSpace"
以调用Diff.diffWordsWithSpace(oldStr, newStr[, options])
方法
{% diff method="diffWordsWithSpace" %} ```bash beep boop ``` ```bash beep boob blah ``` {% enddiff %}
beep - boop + boob blah
Diff.diffLines(oldStr, newStr[, options])
比较两个文本块,逐行比较。
返回
返回更改对象列表(请参见下文)。
选项
ignoreWhitespace
: true
忽略前导和尾随空白。这与diffTrimmedLines
相同newlineIsToken
: true
newlineIsToken
换行符视为单独的标记。这容许换行结构的更改独立于行内容而发生,并照此处理。一般,这是diffLines
更加人性化的形式,而且diffLines
更适合于补丁和其余计算机友好的输出。示例
设置
method="diffLines"
以调用Diff.diffLines(oldStr, newStr[, options])
方法
{% diff method="diffLines",options={"newlineIsToken":true} %} ```bash beep boop the cat is palying with cap what ``` ```bash beep boob blah the cat is palying with cap who ``` {% enddiff %}
- beep boop + beep boob blah the cat is palying with cap - what + who
Diff.diffTrimmedLines(oldStr, newStr[, options])
区分两个文本块,逐行比较,忽略前导和尾随空格。
返回
返回更改对象列表(请参见下文)。
选项
ignoreWhitespace
:与diffLines
相同。newlineIsToken
:同diffLines
。示例
设置
method="diffTrimmedLines"
来调用Diff.diffTrimmedLines(oldStr, newStr[, options])
方法
{% diff method="diffTrimmedLines",options={"newlineIsToken":true} %} ```bash beep boop the cat is palying with cap what ``` ```bash beep boob blah the cat is palying with cat who ``` {% enddiff %}
- beep boop the cat is palying with cap what + beep boob blah the cat is palying with cat who
Diff.diffSentences(oldStr, newStr[, options])
区分两个文本块,逐句比较。
返回
返回更改对象列表(请参见下文)。
示例
设置
method="diffSentences"
来调用Diff.diffSentences(oldStr, newStr[, options])
方法
{% diff method="diffSentences" %} ```bash beep boop the cat is palying with cap what ``` ```bash beep boob blah the cat is palying with cat who ``` {% enddiff %}
- beep boop the cat is palying with cap what + beep boob blah the cat is palying with cat who
Diff.diffCss(oldStr, newStr[, options])
比较两个文本块,比较 CSS 标记。
返回
返回更改对象列表(请参见下文)。
示例
设置
method="diffCss"
来调用Diff.diffCss(oldStr, newStr[, options])
方法
{% diff method="diffCss" %} ```css .markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section h5, .markdown-section h6 { margin-top: 1.275em; margin-bottom: .85em; font-weight: 700; } ``` ```css .markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section h5, .markdown-section h6 { margin-top: 1.5em; margin-bottom: 1em; } ``` {% enddiff %}
.markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section h5, .markdown-section h6 { margin-top: - 1.275em + 1.5em ; margin-bottom: - .85em + 1em ; - font-weight: 700; }
Diff.diffJson(oldObj, newObj[, options])
比较两个 JSON 对象,比较每一个对象上定义的字段。在此比较中,字段的顺序等并不重要。
返回
返回更改对象列表(请参见下文)。
示例
设置
method="diffJson"
来调用Diff.diffJson(oldObj, newObj[, options])
方法
{% diff method="diffJson" %} ```json { "name": "gitbook-plugin-simple-mind-map", "version": "0.2.1", "description": "A gitBook plugin for generating and exporting mind map within markdown" } ``` ```json { "name": "gitbook-plugin-diff", "version": "0.2.1", "description": "A gitbook plugin for showing the differences between the codes within markdown" } ``` {% enddiff %}
{ - "description": "A gitBook plugin for generating and exporting mind map within markdown", - "name": "gitbook-plugin-simple-mind-map", + "description": "A gitbook plugin for showing the differences between the codes within markdown", + "name": "gitbook-plugin-diff", "version": "0.2.1" }
Diff.diffArrays(oldArr, newArr[, options])
比较两个数组,比较每一个项目是否严格相等(===)。
返回
返回更改对象列表(请参见下文)。
选件
comparator
:用于自定义相等性检查的function(left, right)
示例
设置
method="diffArrays"
以调用Diff.diffArrays(oldArr, newArr[, options])
方法
{% diff method="diffArrays" %} ```json [ "Vue", "Python", "Java", "flutter", "springboot", "docker", "React", "小程序" ] ``` ```json [ "Vuejs", "Nodejs", "Java", "flutter", "springboot", "docker", "React" ] ``` {% enddiff %}
[ - Vue - Python + Vuejs + Nodejs Java flutter springboot docker React - 小程序 ]
gitbook
命令gitbook install
.它将自动为您的 gitbook
安装 diff
插件.该步骤仅须要容许一次便可.
gitbook install
或者您能够运行 npm install gitbook-plugin-diff
命令本地安装 gitbook-plugin-diff
插件.
npm install gitbook-plugin-diff
gitbook build
)或服务( gitbook serve
).gitbook serve
https://github.com/snowdreams1006/gitbook-plugin-diff/blob/master/docs/book.json
{ "plugins": ["diff"], "pluginsConfig": { "diff": { "method": "diffJson" } } }
https://github.com/snowdreams1006/gitbook-plugin-diff/blob/master/example/book.json
{ "plugins": ["diff"], "pluginsConfig": { "diff": { "method": "diffJson" } } }
book.json
文件{ "plugins": ["diff"] }
或者您能够将 method
设置为默认方法用于代码之间进行比较方式:
{ "plugins": ["diff"], "pluginsConfig": { "diff": { "method": "diffChars" } } }
或者您能够根据方法将 options
设置为默认选项.
{ "plugins": ["diff"], "pluginsConfig": { "diff": { "method": "diffChars", "options": { "ignoreCase": true } } } }
注意 :若是您的书尚未建立,以上代码段能够用做完整的 book.json
文件.
👤 snowdreams1006
欢迎贡献,问题和功能要求!随时检查问题页面 。
若是这个项目对您有帮助,请给个星星 !
版权全部 ©2019 snowdreams1006 。
该项目是MIT许可的。
若是本文对你有所帮助,请动动小手点一下推荐,不然还请留言指正,若有须要,请关注我的公众号「 雪之梦技术驿站 」