Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Nov 29, 2024
1 parent acf19e0 commit c08d97e
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 86 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ demo/markdownlint-browser.js
demo/markdownlint-browser.min.js
demo/micromark-browser.js
demo/micromark-browser.dev.js
demo/micromark-html-browser.js
demo/micromark-html-browser.dev.js
node_modules
!test/node_modules
npm-debug.log
Expand Down
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
.vscode
coverage
demo/*
!demo/markdownlint-browser.cjs
doc-build
eslint.config.mjs
example
micromark
npm-debug.log
schema/*.mjs
scripts
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,12 +798,10 @@ function applyFixes(input, errors) { ... }
Invoking `applyFixes` with the results of a call to lint can be done like so:

```javascript
const { "sync": markdownlintSync, applyFixes } = require("markdownlint");
import markdownlint from "../lib/markdownlint.mjs";

function fixMarkdownlintViolations(content) {
const fixResults = markdownlintSync({ strings: { content } });
return applyFixes(content, fixResults.content);
}
const fixResults = markdownlint.sync({ "strings": { "content": original } });
const fixed = markdownlint.applyFixes(original, fixResults.content);
```

## Usage
Expand Down Expand Up @@ -916,7 +914,6 @@ Then reference `markdownlint` and `micromark` scripts:

```html
<script src="demo/micromark-browser.js"></script>
<script src="demo/micromark-html-browser.js"></script>
<script src="demo/markdownlint-browser.min.js"></script>
```

Expand Down
1 change: 0 additions & 1 deletion demo/default.htm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
</div>
<script src="markdown-it.min.js"></script>
<script src="micromark-browser.js"></script>
<script src="micromark-html-browser.js"></script>
<script src="markdownlint-browser.min.js"></script>
<script src="default.js"></script>
</body>
Expand Down
13 changes: 6 additions & 7 deletions demo/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var markdownit = globalThis.markdownit;
var markdownlint = globalThis.markdownlint.default.library;
var micromark = globalThis.micromarkBrowser;
var micromarkHtml = globalThis.micromarkHtmlBrowser;

// DOM elements
var markdown = document.getElementById("markdown");
Expand Down Expand Up @@ -70,15 +69,15 @@
const compileOptions = {
"allowDangerousHtml": true,
"htmlExtensions": [
micromarkHtml.directiveHtml({ "*": handleDirective }),
micromarkHtml.gfmAutolinkLiteralHtml(),
micromarkHtml.gfmFootnoteHtml(),
micromarkHtml.gfmTableHtml(),
micromarkHtml.mathHtml()
micromark.directiveHtml({ "*": handleDirective }),
micromark.gfmAutolinkLiteralHtml(),
micromark.gfmFootnoteHtml(),
micromark.gfmTableHtml(),
micromark.mathHtml()
]
};
try {
return micromarkHtml.compile(compileOptions)(events);
return micromark.compile(compileOptions)(events);
} catch (error) {
return `[Exception: "${error}"]`;
}
Expand Down
8 changes: 0 additions & 8 deletions demo/micromark-exports-html.mjs

This file was deleted.

12 changes: 6 additions & 6 deletions demo/micromark-exports.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-check

export { directive } from "micromark-extension-directive";
export { gfmAutolinkLiteral } from "micromark-extension-gfm-autolink-literal";
export { gfmFootnote } from "micromark-extension-gfm-footnote";
export { gfmTable } from "micromark-extension-gfm-table";
export { math } from "micromark-extension-math";
export { parse, postprocess, preprocess } from "micromark";
export { compile, parse, postprocess, preprocess } from "micromark";
export { directive, directiveHtml } from "micromark-extension-directive";
export { gfmAutolinkLiteral, gfmAutolinkLiteralHtml } from "micromark-extension-gfm-autolink-literal";
export { gfmFootnote, gfmFootnoteHtml } from "micromark-extension-gfm-footnote";
export { gfmTable, gfmTableHtml } from "micromark-extension-gfm-table";
export { math, mathHtml } from "micromark-extension-math";
31 changes: 1 addition & 30 deletions demo/micromark-webpack.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import webpack from "webpack";
import TerserPlugin from "terser-webpack-plugin";
import { __dirname, importWithTypeJson } from "../test/esm-helpers.mjs";
const { homepage, name, version } = await importWithTypeJson("../package.json");

const htmlEntry = "./micromark-exports-html.mjs";
const htmlName = "micromarkHtmlBrowser";
const { homepage, name, version } = await importWithTypeJson(import.meta, "../package.json");

const base = {
"entry": "./micromark-exports.mjs",
Expand Down Expand Up @@ -69,31 +66,5 @@ export default [
...web.output,
"filename": "micromark-browser.dev.js"
}
},
{
...web,
...production,
"entry": htmlEntry,
"output": {
...web.output,
"library": {
...web.output.library,
"name": htmlName
},
"filename": "micromark-html-browser.js"
}
},
{
...web,
...development,
"entry": htmlEntry,
"output": {
...web.output,
"library": {
...web.output.library,
"name": htmlName
},
"filename": "micromark-html-browser.dev.js"
}
}
];
2 changes: 1 addition & 1 deletion demo/webpack.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const require = createRequire(import.meta.url);
import webpack from "webpack";
import TerserPlugin from "terser-webpack-plugin";
import { __dirname, importWithTypeJson } from "../test/esm-helpers.mjs";
const libraryPackageJson = await importWithTypeJson("../package.json");
const libraryPackageJson = await importWithTypeJson(import.meta, "../package.json");
const nodeModulePrefixRe = /^node:/u;

// eslint-disable-next-line jsdoc/require-jsdoc
Expand Down
14 changes: 5 additions & 9 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export default [
"demo/markdownlint-browser.min.js",
"demo/micromark-browser.js",
"demo/micromark-browser.dev.js",
"demo/micromark-html-browser.js",
"demo/micromark-html-browser.dev.js",
"example/typescript/type-check.js",
"test-repos/**"
]
Expand Down Expand Up @@ -83,7 +81,6 @@ export default [
"unicorn/no-null": "off",
"unicorn/no-useless-undefined": "off",
"unicorn/prefer-at": "off",
"unicorn/prefer-module": "off",
"unicorn/prefer-string-raw": "off",
"unicorn/prefer-string-replace-all": "off",
"unicorn/prefer-string-slice": "off",
Expand Down Expand Up @@ -115,8 +112,7 @@ export default [
},
{
"files": [
"demo/*.js",
"demo/*.cjs"
"demo/default.js"
],
"languageOptions": {
"globals": {
Expand All @@ -132,6 +128,7 @@ export default [
"no-invalid-this": "off",
"no-shadow": "off",
"no-var": "off",
"unicorn/prefer-module": "off",
"unicorn/prefer-query-selector": "off"
}
},
Expand All @@ -145,13 +142,12 @@ export default [
"rules": {
"n/no-missing-require": "off",
"no-console": "off",
"no-invalid-this": "off",
"no-shadow": "off"
"no-invalid-this": "off"
}
},
{
"files": [
"example/*.mjs"
"example/standalone.mjs"
],
"rules": {
"no-console": "off",
Expand All @@ -167,7 +163,7 @@ export default [
"sourceType": "commonjs"
},
"rules": {
"jsdoc/valid-types": "off"
"unicorn/prefer-module": "off"
}
}
];
6 changes: 6 additions & 0 deletions example/standalone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ markdownlint(options, function callback(err, result) {
console.dir(result, { "colors": true, "depth": null });
}
});

// Fixes all supported violations in Markdown content
const original = "# Heading";
const fixResults = markdownlint.sync({ "strings": { "content": original } });
const fixed = markdownlint.applyFixes(original, fixResults.content);
console.log(fixed);
3 changes: 1 addition & 2 deletions lib/markdownlint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const dynamicRequire = createRequire(import.meta.url);
import * as os from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import { getMarkdownItTokens } from "./markdownit.cjs";
import { parse as micromarkParse } from "../helpers/micromark-parse.mjs";
import { version } from "./constants.mjs";
import rules from "./rules.mjs";
Expand Down Expand Up @@ -505,7 +504,7 @@ function lintContent(
// Parse content into lines and get markdown-it tokens
const lines = content.split(helpers.newLineRe);
const markdownitTokens = needMarkdownItTokens ?
getMarkdownItTokens(markdownItPlugins, preClearedContent, lines) :
dynamicRequire("./markdownit.cjs").getMarkdownItTokens(markdownItPlugins, preClearedContent, lines) :
[];
// Create (frozen) parameters for rules
/** @type {MarkdownParsers} */
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.36.1",
"description": "A Node.js style checker and lint tool for Markdown/CommonMark files.",
"type": "module",
"main": "./lib/markdownlint.mjs",
"exports": {
".": "./lib/markdownlint.mjs",
"./helpers": "./helpers/helpers.cjs",
Expand Down
2 changes: 1 addition & 1 deletion schema/build-config-example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from "node:fs";
import path from "node:path";
import yaml from "js-yaml";
import { __dirname, importWithTypeJson } from "../test/esm-helpers.mjs";
const configSchema = await importWithTypeJson("../schema/markdownlint-config-schema.json");
const configSchema = await importWithTypeJson(import.meta, "../schema/markdownlint-config-schema.json");

const configExample = {};
for (const rule in configSchema.properties) {
Expand Down
29 changes: 24 additions & 5 deletions test/esm-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@ import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

// Shims import.meta.filename on Node 18
/**
* Gets the file name of the current module.
* Shims import.meta.filename for Node 18.
*
* @param {Object} meta ESM import.meta object.
* @returns {string} File name.
*/
// eslint-disable-next-line no-underscore-dangle
export const __filename = (meta) => fileURLToPath(meta.url);

// Shims import.meta.dirname on Node 18
/**
* Gets the directory name of the current module.
* Shims import.meta.dirname for Node 18.
*
* @param {Object} meta ESM import.meta object.
* @returns {string} Directory name.
*/
// eslint-disable-next-line no-underscore-dangle
export const __dirname = (meta) => path.dirname(__filename(meta));

// Avoids "ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time"
export const importWithTypeJson = async(file) => (
/**
* Imports a file as JSON.
* Avoids "ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time".
*
* @param {Object} meta ESM import.meta object.
* @param {string} file JSON file to import.
* @returns {Promise<Object>} JSON object.
*/
export const importWithTypeJson = async(meta, file) => (
// @ts-ignore
JSON.parse(await fs.readFile(path.resolve(__dirname(import.meta), file)))
JSON.parse(await fs.readFile(path.resolve(__dirname(meta), file)))
);
2 changes: 1 addition & 1 deletion test/markdownlint-test-custom-rules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import markdownlint from "../lib/markdownlint.mjs";
import customRules from "./rules/rules.cjs";
import { newLineRe } from "../helpers/helpers.cjs";
import { __filename, importWithTypeJson } from "./esm-helpers.mjs";
const packageJson = await importWithTypeJson("../package.json");
const packageJson = await importWithTypeJson(import.meta, "../package.json");
const { homepage, version } = packageJson;

test("customRulesV0", (t) => new Promise((resolve) => {
Expand Down
2 changes: 1 addition & 1 deletion test/markdownlint-test-result-object.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import test from "ava";
import markdownlint from "../lib/markdownlint.mjs";
import { importWithTypeJson } from "./esm-helpers.mjs";
const packageJson = await importWithTypeJson("../package.json");
const packageJson = await importWithTypeJson(import.meta, "../package.json");
const { homepage, version } = packageJson;

test("resultObjectToStringNotEnumerable", (t) => new Promise((resolve) => {
Expand Down
6 changes: 3 additions & 3 deletions test/markdownlint-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import * as constants from "../lib/constants.mjs";
import rules from "../lib/rules.mjs";
import customRules from "./rules/rules.cjs";
import { __dirname, importWithTypeJson } from "./esm-helpers.mjs";
const packageJson = await importWithTypeJson("../package.json");
const configSchema = await importWithTypeJson("../schema/markdownlint-config-schema.json");
const configSchemaStrict = await importWithTypeJson("../schema/markdownlint-config-schema-strict.json");
const packageJson = await importWithTypeJson(import.meta, "../package.json");
const configSchema = await importWithTypeJson(import.meta, "../schema/markdownlint-config-schema.json");
const configSchemaStrict = await importWithTypeJson(import.meta, "../schema/markdownlint-config-schema-strict.json");

const deprecatedRuleNames = new Set(constants.deprecatedRuleNames);
const ajvOptions = {
Expand Down

0 comments on commit c08d97e

Please sign in to comment.