简体   繁体   English

为 remark-cli 配置 remark-lint-no-undefined-references 插件/规则“允许”选项?

[英]Configure remark-lint-no-undefined-references plugin/rule 'Allow' option for remark-cli?

Original Question原问题

How do I correctly " configure " the ( unified , remark , remark-lint ) remark-lint-no-undefined-references plugin/rule "allow" option for use with the remark-cli ?我如何正确地“配置”(统一评论remark-lintremark-lint-no-undefined-references插件/规则“允许”选项以与remark-cli一起使用?

My goal is to configure the rule to ignore the Azure DevOps Wiki's non-standard table of contents tag, [[_TOC_]] .我的目标是配置规则以忽略Azure DevOps Wiki 的非标准目录标签[[_TOC_]] My simplified setup entails:我的简化设置需要:

  1. All packages globally installed .全局安装的所有包。 Probably not relevant.可能不相关。
  2. A parent folder in which I have:我有一个父文件夹:
    • A package.json file.一个package.json文件。
    • A Test folder containing just the one Test.md file whose only content is this one line [[_TOC_]] .一个Test文件夹,仅包含一个Test.md文件,其唯一内容是这一行[[_TOC_]]
  3. From a command prompt whose working folder is the aforementioned parent folder, I execute:在工作文件夹为上述父文件夹的命令提示符下,我执行:
    • remark Test

Default Operation默认操作

The default operation, ie just the plugin/rule specified in the package.json file, returns the expected warning.默认操作,即仅在package.json文件中指定的插件/规则,返回预期的警告。 This is, presumably , due to the non-standard tag, [[_TOC_]] .大概是由于非标准标签[[_TOC_]]的。 This is the warning I wish to turn off.这是我想关闭的警告。

package.json (default) package.json(默认)

{
    "remarkConfig": {
        "plugins": [
            "remark-lint-no-undefined-references"
    ]
    }
}

Execution and Expected Warning (default)执行和预期警告(默认)

C:\..>remark Test
test\Test.md
  1:1-1:10  warning  Found reference to undefined definition  no-undefined-references  remark-lint
   1:2-1:9  warning  Found reference to undefined definition  no-undefined-references  remark-lint

‼ 2 warnings

What I've tried我试过的

I've tried to adapt remark-lint-no-undefined-references API example and Configuration, 'rules can be configured in one standard way' to my package.json file.我已尝试将remark-lint-no-undefined-references API 示例配置“规则可以以一种标准方式配置”修改为我的package.json文件。 After much trial and error, I've ended up with this seemingly valid json:经过反复试验,我得到了这个看似有效的 json:

package.json package.json

{
    "remarkConfig": {
        "plugins": [
            "remark-lint-no-undefined-references", [1, {
                "allow": ["[[_TOC_]]"]
            }]
        ]
    }
}

The Online JSON Viewer and JSONLint indicate it's valid JSON. However, remark-cli yields this error.在线 JSON 查看器JSONLint表明它是有效的 JSON。但是, remark-cli会产生此错误。 Other variations yielded different errors.其他变化产生不同的错误。 I am stumped.我很难过。

C:\..>
Test\Test.md
  1:1  error  Error: Cannot parse file `package.json`
Expected preset, not `1`
    at Error (file:///C:/Users/scott.welker/AppData/Roaming/npm/node_modules/remark-cli/node_modules/fault/index.js:39:12)
    at file:///C:/Users/scott.welker/AppData/Roaming/npm/node_modules/remark-cli/node_modules/unified-engine/lib/find-up.js:190:15
    at done (file:///C:/Users/scott.welker/AppData/Roaming/npm/node_modules/remark-cli/node_modules/trough/index.js:145:7)

× 1 error

Update 03/14/2022 2022 年 3 月 14 日更新

I've made some progress thanks to a bit of help on GitHub Issue (#210) .感谢GitHub 问题 (#210) 上的一些帮助,我取得了一些进展。 However, that was not the right avenue and it is closed.但是,那不是正确的途径,并且已关闭。 My issue remains.我的问题仍然存在。

A Few Things are Apparent有几件事是显而易见的

  1. My initially inferred JSON, package.json , is incorrect.我最初推断的 JSON, package.json是不正确的。 See Hacked my JSON below.请参阅下面的Hacked my JSON
  2. I failed to appreciate how the non-standard Azure DevOps Wiki table of contents tag, [[_TOC_]] , is interpreted.我没能理解非标准的 Azure DevOps Wiki 目录标签[[_TOC_]]是如何被解释的。 See Tag Interpretation (console-log) following.请参阅下面的标签解释(控制台日志)
  3. My inferred package.json seemingly remains incorrect.我推断package.json似乎仍然不正确。 See My JSON Must Still be Wrong below.请参阅下面的我的 JSON 一定是错误的。

Hacked my JSON黑了我的 JSON

To overcome ...Error: Cannot parse file 'package.json', Expected preset, not '1' I hacked my file so that I now have the following.为了克服...Error: Cannot parse file 'package.json', Expected preset, not '1'我破解了我的文件,所以我现在有了以下内容。 This change overcomes the error and enables me to continue but it is still seemingly incorrect.此更改克服了错误并使我能够继续,但它似乎仍然不正确。 See My JSON Must Still be Wrong .请参阅我的 JSON 一定还是错的。

package.json file package.json 文件

{
    "remarkConfig": {
        "plugins": [
            "remark-lint-no-undefined-references", {
                "allow": ["_TOC_", "[_TOC_]"]
            }
        ]
    }
}

Tag Interpretation (console.log)标记解释 (console.log)

After hacking my JSON, I added a recommended console.log ( ..\npm\node_modules\remark-lint-no-undefined-references\index.js ) of id .在破解我的 JSON 后,我添加了推荐的 console.log ( ..\npm\node_modules\remark-lint-no-undefined-references\index.js ) of id This reveals that the linting interprets the table of contents tag as two separate bits of concerning markdown, _TOC_ and [_TOC_] .这表明 linting 将目录标签解释为有关 markdown、 _TOC_[_TOC_]的两个独立位。 I did not appreciate this.我不喜欢这个。 However, the findings below suggest this may not be a problem.但是,以下调查结果表明这可能不是问题。 See My JSON Must Still be Wrong .请参阅我的 JSON 一定还是错的。

remark-cli备注-cli

C:\..>remark Test
Id:  _TOC_
Id:  [_TOC_]
Test\Test.md
  1:1-1:10  warning  Found reference to undefined definition  no-undefined-references  remark-lint
   1:2-1:9  warning  Found reference to undefined definition  no-undefined-references  remark-lint

‼ 2 warnings

My JSON Must Still be Wrong我的JSON肯定还是错的

Referring to another location in the source here (line 126) , when I replace that const allow definition with this hard-coded declaration, const allow = new Set(["_TOC_", "[_TOC_]"]) , I get the desired behavior.这里引用源代码中的另一个位置(第 126 行) ,当我用这个硬编码声明替换const allow定义时, const allow = new Set(["_TOC_", "[_TOC_]"]) ,我得到了想要的行为。 Eg:例如:

remark-cli备注-cli

C:\...>remark Test
Test\Test.md: no issues found

Next Steps:下一步:

  1. See whether I can forgo the package.json and instead use pure javascript .看看我是否可以放弃package.json而改用纯javascript Either my JSON is incorrect or it isn't being interpreted correctly.要么我的 JSON 不正确,要么没有被正确解释。
  2. Continue to futz with the JSON. What I've inferred may well be wrong.继续对 JSON 进行 futz。我的推断很可能是错误的。
    • The following guess also fails to suppress the unwanted warnings:以下猜测也无法抑制不需要的警告:

package.json package.json

{
    "remarkConfig": {
        "plugins": [
            "remark-lint-no-undefined-references", [{
                "allow": ["_TOC_", "[_TOC_]"]
            }]
        ]
    }
}

Update 03/28/2022 2022 年 3 月 28 日更新

CAUTION!警告!
While the setup in this update is valid, the Test.js example code is flawed.虽然此更新中的设置有效,但Test.js示例代码存在缺陷。 See 03/30/2022 Update below.请参阅下面的03/30/2022 更新

Following up on " Next Step, see whether I can forgo the package.json and instead use pure javascript . "跟进“下一步,看看我是否可以放弃package.json而改用纯javascript

I seem to have made it work except it suppresses all warnings, not just the few (two) that I am targeting.我似乎已经让它工作了,除了它抑制了所有警告,而不仅仅是我针对的少数(两个)警告。 More development and testing is needed.需要更多的开发和测试。

New Setup新设置

  1. A new parent Node.js application folder in which I have:我有一个新的父应用程序文件夹 Node.js:
    • Opted to re-install all packages, and some new ones, but not globally (this time):选择重新安装所有软件包和一些新软件包,但不是全局(这次):
      • npm install to-vfile
      • npm install vfile-reporter
      • npm install remark
      • npm install remark-lint
      • npm install remark-lint-no-undefined-references
    • The node_modules folder. node_modules文件夹。 Created and populated by npm installs (dependencies).由 npm 次安装(依赖项)创建和填充。
    • A package.json file.一个package.json文件。 Created and populated by npm installs.由 npm 次安装创建和填充。
    • A package-lock.json file.一个package-lock.json文件。 Created and populated by npm installs.由 npm 次安装创建和填充。
    • A copy of the same Test folder containing just the one Test.md file whose only content is this one line [[_TOC_]] .同一Test文件夹的副本,仅包含一个Test.md文件,其唯一内容是这一行[[_TOC_]]
  2. From a command prompt whose working folder is the aforementioned parent folder, I execute my new node/ javascript code:在工作文件夹为上述父文件夹的命令提示符下,我执行我的新节点/ javascript代码:
    • node Test.js See following. node Test.js见下文。 Code comments illustrate the unexpected suppression of all warnings, even those not targeted.代码注释说明了所有警告的意外抑制,即使是那些没有针对性的警告。

Test.js测试.js

WARNING!警告! This code is incorrect.此代码不正确。 See the 03/30/2022 Update below.请参阅下面的03/30/2022 更新

import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintNoUndefinedReferences from 'remark-lint-no-undefined-references'
import {read} from 'to-vfile'

main()

async function main() {
  const file = await remark()
    .use(remarkLint)
    // This useage results in the expected warnings, '...Found reference to undefined definition...'
    .use(remarkLintNoUndefinedReferences)
    
    // This usage suppresses the warnings, as desired. However, note the next usage.
    //.use(remarkLintNoUndefinedReferences[ { allow: [ '_TOC_', '[_TOC_]' ] }])

    // This usage also suppresses the warning but it shoud not have done so.
    //.use(remarkLintNoUndefinedReferences[ { allow: [ '...', '…' ] }])

    // This usage (and related tests) seem to illustrate that anything in the allowed array suppresses all warnings. 
    //.use(remarkLintNoUndefinedReferences[ { allow: [ '' ] }])
    .process(await read('.\\Test\\Test.md'))

  console.error(reporter(file))
}

Update 03/30/2022 2022 年 3 月 30 日更新

Here is a corrected Test.js file where my invalid usages are commented out, marked WRONG , and the two correct usages are marked GOOD .这是一个更正后的Test.js文件,其中我的无效用法被注释掉,标记为WRONG ,两个正确的用法标记为GOOD The final usage corrects mistakes made in my 03/28/2022 update.最终用法纠正了我在 03/28/2022 更新中犯的错误。 I now have a working javascript version.我现在有一个可用的javascript版本。 I just need to adapt this known working version to the remark-cli's package.json file.我只需要将这个已知的工作版本调整为remark-cli 的package.json文件。 Getting very close.越来越近了。

I arrived at this CORRECT , working usage through trial and error.我通过反复试验得出了这个CORRECT的工作用法。 My trial and error was aided by adding console.log() statements to the ..\remark-lint-no-undefined-references\index.js source and tweaking my javascript code as guided by repeated re-reading of the remark-lint Configure section.通过将console.log()语句添加到..\remark-lint-no-undefined-references\index.js源并根据重复重新阅读remark-lint的指导调整我的 javascript 代码,我的试错得到了帮助配置部分。

Test.js测试.js

import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintNoUndefinedReferences from 'remark-lint-no-undefined-references'
import {read} from 'to-vfile'

main()

async function main() {
  const file = await remark()
    .use(remarkLint)
  
    // WRONG: This usage seems to suppress the warnings, as desired. However, note the next usage.
    //.use(remarkLintNoUndefinedReferences[ { allow: [ '_TOC_', '[_TOC_]' ] }])

    // WRONG: This usage also seems to supresses the warnings but, it shoud not have done so.
    //.use(remarkLintNoUndefinedReferences[ { allow: [ '...', '…' ] }])

    // WRONG: This usage (and related tests) seem to illustrate that anything in the allowed array suppresses all warnings. 
    //.use(remarkLintNoUndefinedReferences[ { allow: [ '' ] }])

    // GOOD: This usage results in the expected warnings, '...Found reference to undefined definition...'
    //.use(remarkLintNoUndefinedReferences)

    // GOOD: This usage seems to be correct!!
    .use(remarkLintNoUndefinedReferences, [1, { allow: [ '_TOC_', '[_TOC_]' ] }])
    .process(await read('.\\Test\\Test.md'))

  console.error(reporter(file))
}

Execution and Output执行和 Output

C:\..>node Test.js
DEBUG remarkTest: allow contains 2 items.

DEBUG remarkTest Id: '_TOC_'
DEBUG remarkTest Id: '[_TOC_]'
.\Test\Test.md: no issues found

The package.json below correctly " configures " the remark-lint-no-undefined-references plugin/rule "allow" option for use with remark-cli .下面的package.json正确地“配置”了与remark-cli一起使用的remark-lint-no-undefined-references插件/规则“允许”选项

package.json package.json

I arrived at this by working from my 03/30/2022 update and futzing with varying syntax illustrated in the Unified Engine, Configuration, Schema section.我从 03/30/2022 开始工作,并使用统一引擎、配置、模式部分中说明的不同语法来实现这一目标。

Again, my goal is to configure the rule to ignore the Azure DevOps Wiki's non-standard table of contents tag, [[_TOC_]] .同样,我的目标是配置规则以忽略Azure DevOps Wiki 的非标准目录标签[[_TOC_]]

{
    "remarkConfig": {
        "plugins": {
            "remark-lint-no-undefined-references": { "allow":[ "_TOC_", "[_TOC_]" ] }
        }
    },
    "dependencies": {
        "remark": "^14.0.2",
        "remark-lint": "^9.1.1",
        "remark-lint-no-undefined-references": "^4.1.1"
    }
}

Execution and Output执行和 Output

Note: my debug console.log(...) remains in place.注意:我的调试console.log(...)仍然存在。

C:\..>remark Test
DEBUG remarkCLITest: allow contains 2 items.

Test\Test.md: no issues found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用备注将 Markdown 解析为 json - How to parse markdown to json using remark 运行 onCreateNode 生命周期时出现 gatsby-remark-relative-images 错误 - gatsby-remark-relative-images error while running the onCreateNode lifecycle 安装插件后无法在Jenkins'Global Configure'中看到Node选项 - Unable to see Node option in Jenkins 'Global Configure' after installing the plugin Vue CLI 3 - 构建不会因 lint 错误而失败 - Vue CLI 3 - Build not failing on lint errors CLI引用错误的npm版本 - CLI references wrong npm version 如何在 Angular CLI 中配置代理 - How to configure a proxy in Angular CLI 使用 npm start 传入 CLI 选项 - Pass in CLI option with npm start 错误 Firebase cli 云函数部署。 npm 错误! 在functions@lint 脚本中失败 - Error Firebase cli cloud function deploy. npm ERR! Failed at the functions@ lint script “express-session 已弃用未定义的重新保存选项;提供重新保存选项 node_modules/admin-bro-expressjs/plugin.js:138:14” - "express-session deprecated undefined resave option; provide resave option node_modules/admin-bro-expressjs/plugin.js:138:14" 配置AWS凭证以同时使用CLI和SDK - Configure AWS credentials to work with both the CLI and SDKs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM