eleventy-plugin-unified
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"type": "module", | ||
"name": "eleventy-plugin-unified", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Use the unified ecosystem in Eleventy with remark and rehype.", | ||
@@ -28,3 +28,3 @@ "main": ".eleventy.cjs", | ||
"engines": { | ||
"node": "^12.17 || ^13.7 || >= 14" | ||
"node": ">= 16" | ||
}, | ||
@@ -37,8 +37,11 @@ "files": [ | ||
"rehype-parse": "^8.0.4", | ||
"rehype-retext": "^3.0.2", | ||
"rehype-stringify": "^9.0.3", | ||
"remark-parse": "^10.0.1", | ||
"remark-rehype": "^10.1.0", | ||
"retext-english": "^4.1.0", | ||
"unified": "^10.1.2" | ||
}, | ||
"devDependencies": { | ||
"@11ty/eleventy": "^1.0.2", | ||
"@commitlint/cli": "^17.1.2", | ||
@@ -49,2 +52,3 @@ "@commitlint/config-conventional": "^17.1.0", | ||
"c8": "^7.12.0", | ||
"deepmerge": "^4.2.2", | ||
"eslint": "^8.25.0", | ||
@@ -56,3 +60,7 @@ "eslint-config-prettier": "^8.5.0", | ||
"proxyquire": "^2.1.3", | ||
"semantic-release": "^19.0.5" | ||
"rehype-format": "^4.0.1", | ||
"remark-slug": "^7.0.1", | ||
"retext-repeated-words": "^4.2.0", | ||
"semantic-release": "^19.0.5", | ||
"unist-util-visit": "^4.1.1" | ||
}, | ||
@@ -59,0 +67,0 @@ "release": { |
@@ -7,13 +7,12 @@ # Eleventy Plugin Unified | ||
You can render and transform: | ||
You can render, transform and lint: | ||
- markdown with the [remark](https://github.com/remarkjs/remark) ecosystem. | ||
- html with the [rehype](https://github.com/rehypejs/rehype) ecosystem. | ||
- text with the [retext](https://github.com/retextjs/retext) ecosystem. | ||
[retext](https://github.com/retextjs/retext) is not yet supported, raise an issue if you'd like this. | ||
## Install | ||
```bash | ||
npm install eleventy-plugin-unified | ||
npm install eleventy-plugin-unified remark-slug | ||
``` | ||
@@ -109,2 +108,61 @@ | ||
### Configuring text transforms (retext plugins) | ||
```bash | ||
npm install eleventy-plugin-unified retext-repeated-words vfile-reporter | ||
``` | ||
```javascript | ||
// .eleventy.config.cjs | ||
const EleventyUnifiedPlugin = require("eleventy-plugin-unified"); | ||
module.exports = function (eleventyConfig) { | ||
eleventyConfig.addPlugin(EleventyUnifiedPlugin, [ | ||
reporter: 'vfile-reporter', | ||
textTransforms: [ | ||
'retext-repeated-words' | ||
] | ||
]); | ||
}; | ||
``` | ||
or | ||
```javascript | ||
// .eleventy.config.cjs | ||
const EleventyUnifiedPlugin = require("eleventy-plugin-unified"); | ||
module.exports = function (eleventyConfig) { | ||
eleventyConfig.addPlugin(EleventyUnifiedPlugin, [ | ||
reporter: (file) => { | ||
console.log(file); | ||
}, | ||
textTransforms: [ | ||
'retext-repeated-words' | ||
] | ||
]); | ||
}; | ||
``` | ||
### Configuring text parser language | ||
```bash | ||
npm install eleventy-plugin-unified retext-latin retext-repeated-words vfile-reporter | ||
``` | ||
```javascript | ||
// .eleventy.config.cjs | ||
const EleventyUnifiedPlugin = require("eleventy-plugin-unified"); | ||
module.exports = function (eleventyConfig) { | ||
eleventyConfig.addPlugin(EleventyUnifiedPlugin, [ | ||
textParser: 'retext-latin', | ||
reporter: 'vfile-reporter', | ||
textTransforms: [ | ||
'retext-repeated-words' | ||
] | ||
]); | ||
}; | ||
``` | ||
### Configuring options for a plugin | ||
@@ -111,0 +169,0 @@ |
import { unified } from "unified"; | ||
import parse from "rehype-parse"; | ||
import retext from "rehype-retext"; | ||
import stringify from "rehype-stringify"; | ||
import importPlugins from "./import-plugins.js"; | ||
const importIfPath = async (pathOrName) => { | ||
if (!pathOrName) { | ||
return; | ||
} | ||
if (typeof pathOrName === "function") { | ||
return pathOrName; | ||
} else { | ||
const { default: importedFunction } = await import(pathOrName); | ||
return importedFunction; | ||
} | ||
}; | ||
/** | ||
@@ -10,2 +23,5 @@ * @param {String} html html | ||
* @param {Array.<String>} [pluginOptions.htmlTransforms] | ||
* @param {Array.<String>} [pluginOptions.textTransforms] | ||
* @param {String|Function} [pluginOptions.textParser] | ||
* @param {Function} [pluginOptions.reporter] | ||
* @param {String} [pluginOptions.transformsDirectory] | ||
@@ -18,4 +34,20 @@ * @param {Object} [pluginOptions.pageContext] | ||
html, | ||
{ htmlTransforms, transformsDirectory, pageContext, eleventyConfig } = {} | ||
{ | ||
htmlTransforms, | ||
textTransforms, | ||
textParser = "retext-english", | ||
reporter, | ||
transformsDirectory, | ||
pageContext, | ||
eleventyConfig, | ||
} = {} | ||
) { | ||
const hasTextTransforms = | ||
textTransforms instanceof Array && textTransforms.length > 0; | ||
const retextPlugins = await importPlugins( | ||
textTransforms, | ||
transformsDirectory | ||
); | ||
const retextParser = await importIfPath(textParser); | ||
const retextReporter = await importIfPath(reporter); | ||
const output = await unified() | ||
@@ -26,2 +58,3 @@ // Turn HTML into HTML AST (hast) | ||
.use(await importPlugins(htmlTransforms, transformsDirectory)) | ||
.use(retext, unified().use(retextParser).use(retextPlugins)) | ||
// Turn HTML syntax tree into HTML text | ||
@@ -34,3 +67,14 @@ .use(stringify) | ||
.process(html); | ||
if ( | ||
hasTextTransforms && | ||
typeof retextReporter === "function" && | ||
pageContext?.inputPath && | ||
output?.messages.length > 0 | ||
) { | ||
output.path = pageContext.inputPath; | ||
retextReporter(output); | ||
} | ||
return String(output); | ||
} |
Sorry, the diff of this file is not supported yet
14529
213
242
7
18
+ Addedrehype-retext@^3.0.2
+ Addedretext-english@^4.1.0
+ Added@types/nlcst@1.0.4(transitive)
+ Addedarray-iterate@1.1.42.0.1(transitive)
+ Addedhast-util-embedded@2.0.1(transitive)
+ Addedhast-util-has-property@2.0.1(transitive)
+ Addedhast-util-is-body-ok-link@2.0.0(transitive)
+ Addedhast-util-is-element@2.1.3(transitive)
+ Addedhast-util-phrasing@2.0.2(transitive)
+ Addedhast-util-to-nlcst@2.2.0(transitive)
+ Addedhast-util-to-string@2.0.0(transitive)
+ Addednlcst-to-string@2.0.43.1.1(transitive)
+ Addedparse-english@5.0.0(transitive)
+ Addedparse-latin@5.0.1(transitive)
+ Addedrehype-retext@3.0.2(transitive)
+ Addedretext-english@4.1.0(transitive)
+ Addedunherit@3.0.1(transitive)
+ Addedunist-util-modify-children@2.0.03.1.1(transitive)
+ Addedunist-util-visit-children@1.1.42.0.2(transitive)