mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 05:50:13 +01:00
Initial implementation of markdownlint-micromark package, micromark.mjs helpers, and tests.
This commit is contained in:
parent
2e7b7b9079
commit
366a498150
14 changed files with 2885 additions and 2 deletions
|
|
@ -9,6 +9,7 @@ demo/*
|
|||
!demo/markdownlint-browser.js
|
||||
doc-build
|
||||
example
|
||||
micromark
|
||||
npm-debug.log
|
||||
schema/*.js
|
||||
scripts
|
||||
|
|
|
|||
78
lib/micromark.mjs
Normal file
78
lib/micromark.mjs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import assert from "node:assert/strict";
|
||||
// eslint-disable-next-line n/no-unpublished-import
|
||||
import { parse, postprocess, preprocess } from "../micromark/exports.mjs";
|
||||
|
||||
/**
|
||||
* Markdown token.
|
||||
*
|
||||
* @typedef {Object} Token
|
||||
* @property {string} type Token type.
|
||||
* @property {number} startLine Start line (1-based).
|
||||
* @property {number} startColumn Start column (1-based).
|
||||
* @property {number} endLine End line (1-based).
|
||||
* @property {number} endColumn End column (1-based).
|
||||
* @property {string} text Token text.
|
||||
* @property {Token[]} tokens Child tokens.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parses a Markdown document and returns tokens.
|
||||
*
|
||||
* @param {string} markdown Markdown document.
|
||||
* @returns {Token[]} Markdown tokens.
|
||||
*/
|
||||
function micromarkParse(markdown) {
|
||||
|
||||
// Use micromark to parse document into Events
|
||||
const encoding = undefined;
|
||||
const eol = true;
|
||||
const options = undefined;
|
||||
const chunks = preprocess()(markdown, encoding, eol);
|
||||
const parseContext = parse(options).document().write(chunks);
|
||||
const events = postprocess(parseContext);
|
||||
|
||||
// Create Token objects
|
||||
const document = [];
|
||||
let current = {
|
||||
"tokens": document
|
||||
};
|
||||
const history = [ current ];
|
||||
for (const event of events) {
|
||||
const [ kind, token, context ] = event;
|
||||
const { type, start, end, _container } = token;
|
||||
const { "column": startColumn, "line": startLine } = start;
|
||||
const { "column": endColumn, "line": endLine } = end;
|
||||
// sliceSerialize throws when called for a _container
|
||||
const text = _container ? null : context.sliceSerialize(token);
|
||||
if (kind === "enter") {
|
||||
const previous = current;
|
||||
history.push(previous);
|
||||
current = {
|
||||
type,
|
||||
startLine,
|
||||
startColumn,
|
||||
endLine,
|
||||
endColumn,
|
||||
text,
|
||||
"tokens": []
|
||||
};
|
||||
previous.tokens.push(current);
|
||||
} else if (kind === "exit") {
|
||||
assert.equal(type, current.type);
|
||||
assert.equal(startLine, current.startLine);
|
||||
assert.equal(startColumn, current.startColumn);
|
||||
assert.equal(endLine, current.endLine);
|
||||
assert.equal(endColumn, current.endColumn);
|
||||
assert.equal(text, current.text);
|
||||
current = history.pop();
|
||||
assert.ok(current, "Empty history");
|
||||
}
|
||||
}
|
||||
|
||||
// Return document
|
||||
return document;
|
||||
}
|
||||
|
||||
export {
|
||||
micromarkParse as parse
|
||||
};
|
||||
2
micromark/.npmrc
Normal file
2
micromark/.npmrc
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ignore-scripts=true
|
||||
package-lock=false
|
||||
21
micromark/LICENSE
Normal file
21
micromark/LICENSE
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2023 David Anson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
10
micromark/README.md
Normal file
10
micromark/README.md
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# markdownlint-micromark
|
||||
|
||||
> A trivial package that re-exports some [`micromark`][micromark] functionality
|
||||
> as a CommonJS module
|
||||
|
||||
This package is unlikely to be of any use beyond a specific scenario used by
|
||||
[`markdownlint`][markdownlint].
|
||||
|
||||
[markdownlint]: https://github.com/DavidAnson/markdownlint
|
||||
[micromark]: https://github.com/micromark/micromark
|
||||
5
micromark/exports.mjs
Normal file
5
micromark/exports.mjs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
/* eslint-disable n/file-extension-in-import */
|
||||
|
||||
export { parse } from "micromark/lib/parse";
|
||||
export { postprocess } from "micromark/lib/postprocess";
|
||||
export { preprocess } from "micromark/lib/preprocess";
|
||||
20
micromark/package.json
Normal file
20
micromark/package.json
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "markdownlint-micromark",
|
||||
"version": "0.1.0",
|
||||
"description": "A trivial package that re-exports some micromark functionality as a CommonJS module",
|
||||
"exports": "./exports.mjs",
|
||||
"author": "David Anson (https://dlaa.me/)",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/DavidAnson/markdownlint",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/DavidAnson/markdownlint.git"
|
||||
},
|
||||
"bugs": "https://github.com/DavidAnson/markdownlint/issues",
|
||||
"engines": {
|
||||
"node": ">=14.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"micromark": "3.1.0"
|
||||
}
|
||||
}
|
||||
|
|
@ -51,11 +51,11 @@
|
|||
"lint-test-repos": "ava --timeout=5m test/markdownlint-test-repos.js",
|
||||
"serial-config-docs": "npm run build-config && npm run build-docs",
|
||||
"serial-declaration-demo": "npm run build-declaration && npm-run-all --continue-on-error --parallel build-demo test-declaration",
|
||||
"test": "ava test/markdownlint-test.js test/markdownlint-test-config.js test/markdownlint-test-custom-rules.js test/markdownlint-test-helpers.js test/markdownlint-test-result-object.js test/markdownlint-test-scenarios.js",
|
||||
"test": "ava test/markdownlint-test.js test/markdownlint-test-config.js test/markdownlint-test-custom-rules.js test/markdownlint-test-helpers.js test/markdownlint-test-micromark.mjs test/markdownlint-test-result-object.js test/markdownlint-test-scenarios.js",
|
||||
"test-cover": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 npm test",
|
||||
"test-declaration": "cd example/typescript && tsc && node type-check.js",
|
||||
"test-extra": "ava --timeout=5m test/markdownlint-test-extra-parse.js test/markdownlint-test-extra-type.js",
|
||||
"update-snapshots": "ava --update-snapshots test/markdownlint-test-scenarios.js",
|
||||
"update-snapshots": "ava --update-snapshots test/markdownlint-test-micromark.mjs test/markdownlint-test-scenarios.js",
|
||||
"upgrade": "npx --yes npm-check-updates --upgrade"
|
||||
},
|
||||
"engines": {
|
||||
|
|
@ -81,6 +81,7 @@
|
|||
"markdown-it-sup": "1.0.0",
|
||||
"markdown-it-texmath": "1.0.0",
|
||||
"markdownlint-rule-helpers": "0.18.0",
|
||||
"micromark": "3.1.0",
|
||||
"npm-run-all": "4.1.5",
|
||||
"strip-json-comments": "5.0.0",
|
||||
"terser-webpack-plugin": "5.3.6",
|
||||
|
|
|
|||
46
test/every-markdown-syntax.md
Normal file
46
test/every-markdown-syntax.md
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
Every Markdown Syntax
|
||||
=====================
|
||||
|
||||
## Level 2 ATX Heading
|
||||
|
||||
### Level 3 Closed ATX Heading ###
|
||||
|
||||
---
|
||||
|
||||
Text *emphasized* **strong** ___emphasized+strong___.
|
||||
Text `code` <strike>html</strike> <https://example.com/page>.
|
||||
Text [link](https://example.com/page) [link][] [link] ![image][link].
|
||||
|
||||
Hard
|
||||
line break
|
||||
|
||||
[link]: https://example.com/page "Title"
|
||||
|
||||
> Block quote
|
||||
> > Nested
|
||||
|
||||
- Unordered
|
||||
- List
|
||||
- Items
|
||||
Indented
|
||||
|
||||
Content
|
||||
|
||||
1. Ordered
|
||||
2. List
|
||||
1. Items
|
||||
Indented
|
||||
|
||||
Content
|
||||
|
||||
```markdown options
|
||||
Fenced code block
|
||||
```
|
||||
|
||||
Indented code block
|
||||
|
||||
<p>
|
||||
HTML block
|
||||
</p>
|
||||
|
||||
<!-- markdownlint-disable-file -->
|
||||
10
test/markdownlint-test-micromark.mjs
Normal file
10
test/markdownlint-test-micromark.mjs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import fs from "node:fs/promises";
|
||||
import test from "ava";
|
||||
import { parse } from "../lib/micromark.mjs";
|
||||
|
||||
test("parse", async(t) => {
|
||||
t.plan(1);
|
||||
const content = await fs.readFile("./test/every-markdown-syntax.md", "utf8");
|
||||
const document = parse(content);
|
||||
t.snapshot(document, "Unexpected tokens");
|
||||
});
|
||||
2634
test/snapshots/markdownlint-test-micromark.mjs.md
Normal file
2634
test/snapshots/markdownlint-test-micromark.mjs.md
Normal file
File diff suppressed because it is too large
Load diff
BIN
test/snapshots/markdownlint-test-micromark.mjs.snap
Normal file
BIN
test/snapshots/markdownlint-test-micromark.mjs.snap
Normal file
Binary file not shown.
|
|
@ -10236,6 +10236,61 @@ Generated by [AVA](https://avajs.dev).
|
|||
`,
|
||||
}
|
||||
|
||||
## every-markdown-syntax.md
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
{
|
||||
errors: [],
|
||||
fixed: `Every Markdown Syntax␊
|
||||
=====================␊
|
||||
␊
|
||||
## Level 2 ATX Heading␊
|
||||
␊
|
||||
### Level 3 Closed ATX Heading ###␊
|
||||
␊
|
||||
---␊
|
||||
␊
|
||||
Text *emphasized* **strong** ___emphasized+strong___.␊
|
||||
Text \`code\` <strike>html</strike> <https://example.com/page>.␊
|
||||
Text [link](https://example.com/page) [link][] [link] ![image][link].␊
|
||||
␊
|
||||
Hard ␊
|
||||
line break␊
|
||||
␊
|
||||
[link]: https://example.com/page "Title"␊
|
||||
␊
|
||||
> Block quote␊
|
||||
> > Nested␊
|
||||
␊
|
||||
- Unordered␊
|
||||
- List␊
|
||||
- Items␊
|
||||
Indented␊
|
||||
␊
|
||||
Content␊
|
||||
␊
|
||||
1. Ordered␊
|
||||
2. List␊
|
||||
1. Items␊
|
||||
Indented␊
|
||||
␊
|
||||
Content␊
|
||||
␊
|
||||
\`\`\`markdown options␊
|
||||
Fenced code block␊
|
||||
\`\`\`␊
|
||||
␊
|
||||
Indented code block␊
|
||||
␊
|
||||
<p>␊
|
||||
HTML block␊
|
||||
</p>␊
|
||||
␊
|
||||
<!-- markdownlint-disable-file -->␊
|
||||
`,
|
||||
}
|
||||
|
||||
## fenced-code-in-list.md
|
||||
|
||||
> Snapshot 1
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue