remark-deflist
Advanced tools
Comparing version 0.3.0 to 1.0.0
216
lib/index.js
@@ -1,114 +0,106 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = deflist; | ||
var _unistUtilVisit = _interopRequireDefault(require("unist-util-visit")); | ||
var _mdastUtilToString = _interopRequireDefault(require("mdast-util-to-string")); | ||
var _mdastUtilFromMarkdown = _interopRequireDefault(require("mdast-util-from-markdown")); | ||
var _mdastUtilToMarkdown = _interopRequireDefault(require("mdast-util-to-markdown")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const isSingleDeflist = node => /^[^:].+\n:\s/.test((0, _mdastUtilToString.default)(node)) && node.type === 'paragraph'; | ||
const isSplitDeflist = (node, i, parent) => i > 0 && /^:\s/.test((0, _mdastUtilToString.default)(node)) && !/^:\s/.test((0, _mdastUtilToString.default)(parent.children[i - 1])) && node.type === 'paragraph' && parent.children[i - 1].type === 'paragraph'; | ||
/** | ||
* Detect metadata and add it to the `data` field of a VFile. | ||
*/ | ||
import { visit } from 'unist-util-visit'; | ||
import { toString } from 'mdast-util-to-string'; | ||
import { fromMarkdown } from 'mdast-util-from-markdown'; | ||
import { toMarkdown } from 'mdast-util-to-markdown'; | ||
// Test if deflist is contained in a single paragraph. | ||
const isSingleDeflist = (node) => | ||
// i > 0 && | ||
/^[^:].+\n:\s/.test(toString(node)) && | ||
node.type === 'paragraph'; | ||
// Test if deflist is split between two paragraphs. | ||
const isSplitDeflist = (node, i, parent) => i > 0 && | ||
/^:\s/.test(toString(node)) && | ||
!/^:\s/.test(toString(parent.children[i - 1])) && | ||
node.type === 'paragraph' && | ||
parent.children[i - 1].type === 'paragraph'; | ||
const isdeflist = (node, i, parent) => isSingleDeflist(node) || isSplitDeflist(node, i, parent); | ||
function deflist(options = {}) { | ||
return (tree, file) => { | ||
(0, _unistUtilVisit.default)(tree, ['paragraph'], (node, i, parent) => { | ||
const isdef = isdeflist(node, i, parent); | ||
if (!isdef) { | ||
return; | ||
} | ||
let dd = undefined; | ||
let dt = undefined; | ||
let count = 0; | ||
let start = 0; | ||
if (isSingleDeflist(node)) { | ||
const [title, ...children] = (0, _mdastUtilToMarkdown.default)(node).split(/\n:\s+/); | ||
dt = (0, _mdastUtilFromMarkdown.default)(title).children.flatMap(({ | ||
children | ||
}) => children); | ||
dd = children.map(_mdastUtilFromMarkdown.default).flatMap(({ | ||
children | ||
}) => children).map(({ | ||
children | ||
}) => ({ | ||
type: 'descriptiondetails', | ||
data: { | ||
hName: 'dd' | ||
}, | ||
children | ||
})); | ||
start = i; | ||
count = 1; | ||
} else { | ||
dt = parent.children[i - 1].children; | ||
dd = (0, _mdastUtilToMarkdown.default)(node).replace(/^:\s+/, '').split(/\n:\s+/).map(_mdastUtilFromMarkdown.default).flatMap(({ | ||
children | ||
}) => children).map(({ | ||
children | ||
}) => ({ | ||
type: 'descriptiondetails', | ||
data: { | ||
hName: 'dd' | ||
}, | ||
children | ||
})); | ||
start = i - 1; | ||
count = 2; | ||
} | ||
const child = { | ||
type: 'descriptionlist', | ||
data: { | ||
hName: 'dl' | ||
}, | ||
children: [{ | ||
type: 'descriptionterm', | ||
data: { | ||
hName: 'dt' | ||
}, | ||
children: dt | ||
}, ...dd] | ||
}; | ||
parent.children.splice(start, count, child); | ||
}); | ||
(0, _unistUtilVisit.default)(tree, ['descriptionlist'], (node, i, parent) => { | ||
const start = i; | ||
let count = 1; | ||
let children = node.children; | ||
for (let j = i + 1; j < parent.children.length; j++) { | ||
const next = parent.children[j]; | ||
if (next.type === 'descriptionlist') { | ||
count++; | ||
children = children.concat(next.children); | ||
} else { | ||
break; | ||
} | ||
} | ||
if (count === 1) { | ||
return; | ||
} | ||
node.children = children; | ||
parent.children.splice(start, count, node); | ||
}); | ||
}; | ||
export default function deflist() { | ||
return (tree) => { | ||
visit(tree, ['paragraph'], (node, i, parent) => { | ||
const isdef = isdeflist(node, i, parent); | ||
if (!isdef) { | ||
return; | ||
} | ||
let dd = undefined; | ||
let dt = undefined; | ||
let count = 0; | ||
let start = 0; | ||
if (isSingleDeflist(node)) { | ||
const [title, ...children] = toMarkdown(node).split(/\n:\s+/); | ||
const childs = fromMarkdown(title).children; | ||
dt = childs.flatMap(({ children }) => children); | ||
dd = children | ||
.map((str) => fromMarkdown(str)) | ||
.flatMap(({ children }) => children) | ||
.map(({ children }) => ({ | ||
type: 'descriptiondetails', | ||
data: { | ||
hName: 'dd' | ||
}, | ||
children | ||
})); | ||
start = i; | ||
count = 1; | ||
} | ||
else { | ||
const childs = parent.children[i - 1]; | ||
dt = childs.children; | ||
dd = toMarkdown(node) | ||
.replace(/^:\s+/, '') | ||
.split(/\n:\s+/) | ||
.map((str) => fromMarkdown(str)) | ||
.flatMap(({ children }) => children) | ||
.map(({ children }) => ({ | ||
type: 'descriptiondetails', | ||
data: { | ||
hName: 'dd' | ||
}, | ||
children | ||
})); | ||
start = i - 1; | ||
count = 2; | ||
} | ||
const child = { | ||
type: 'descriptionlist', | ||
data: { | ||
hName: 'dl' | ||
}, | ||
children: [ | ||
{ | ||
type: 'descriptionterm', | ||
data: { | ||
hName: 'dt' | ||
}, | ||
children: dt | ||
}, | ||
...dd | ||
] | ||
}; | ||
parent.children.splice(start, count, child); | ||
}); | ||
// Merge subsequent definition lists into a single list (#10) | ||
visit(tree, ['descriptionlist'], (node, i, parent) => { | ||
const start = i; | ||
let count = 1; | ||
let children = node.children; | ||
for (let j = i + 1; j < parent.children.length; j++) { | ||
const next = parent.children[j]; | ||
if (next.type === 'descriptionlist') { | ||
count++; | ||
children = children.concat(next.children); | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
if (count === 1) { | ||
return; | ||
} | ||
node.children = children; | ||
parent.children.splice(start, count, node); | ||
}); | ||
}; | ||
} | ||
module.exports = exports.default; |
@@ -1,9 +0,21 @@ | ||
# The MIT License (MIT) | ||
# MIT License | ||
Copyright © Alex Shaw | ||
Copyright © Alex Shaw | ||
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: | ||
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 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. | ||
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. |
{ | ||
"name": "remark-deflist", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"description": "Remark plugin for pandoc-style definition lists.", | ||
"author": "Alex Shaw <alex.shaw.as@gmail.com>", | ||
"main": "lib/index.js", | ||
"module": "src/index.js", | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": "./lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib", | ||
"src" | ||
"lib/" | ||
], | ||
@@ -17,18 +18,14 @@ "keywords": [ | ||
], | ||
"repository": { | ||
"url": "https://github.com/Symbitic/remark-plugins/tree/master/packages/remark-deflist", | ||
"type": "git" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"repository": "https://github.com/Symbitic/remark-plugins/tree/master/packages/remark-deflist", | ||
"bugs": "https://github.com/Symbitic/remark-plugins/issues", | ||
"scripts": { | ||
"build": "babel --root-mode upward --ignore \"**/*.spec.js\" --out-dir lib src" | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"mdast-util-from-markdown": "^0.8.4", | ||
"mdast-util-to-markdown": "^0.6.1", | ||
"mdast-util-to-string": "^2.0.0", | ||
"unist-util-visit": "^2.0.3" | ||
} | ||
"mdast-util-from-markdown": "^1.0.0", | ||
"mdast-util-to-markdown": "^1.1.1", | ||
"mdast-util-to-string": "^3.1.0", | ||
"unist-util-visit": "^4.0.0" | ||
}, | ||
"gitHead": "a1bfb2a02cd6c9c2385a732f95ae44f07d03284f" | ||
} |
# remark-deflist | ||
[![CI/CD Status](https://github.com/Symbitic/remark-plugins/workflows/main/badge.svg)](https://github.com/Symbitic/remark-plugins/actions) | ||
[![MIT License](https://img.shields.io/github/license/Symbitic/remark-plugins)](https://github.com/Symbitic/remark-plugins/blob/master/LICENSE.md) | ||
[![stars](https://img.shields.io/github/stars/Symbitic/remark-plugins.svg)](https://github.com/Symbitic/remark-plugins) | ||
[Remark](https://remark.js.org/) plugin for adding support for pandoc-style definition lists to Markdown. | ||
@@ -10,2 +14,13 @@ | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
[npm](https://docs.npmjs.com/cli/install): | ||
```sh | ||
npm install remark-deflist | ||
``` | ||
## Example | ||
@@ -80,20 +95,14 @@ | ||
## Installation | ||
```bash | ||
npm install --save remark-deflist | ||
``` | ||
## Usage | ||
```javascript | ||
import unified from 'unified' | ||
import { unified } from 'unified' | ||
import markdown from 'remark-parse' | ||
import html from 'rehype-stringify' | ||
import remark2rehype from 'remark-rehype' | ||
import deflist from 'remark-deflist' | ||
import meta from 'remark-meta' | ||
unified() | ||
.use(markdown) | ||
.use(deflist) | ||
.use(meta) | ||
.use(remark2rehype) | ||
@@ -105,5 +114,6 @@ .use(html) | ||
[MIT](LICENSE.md) © Alex Shaw | ||
[MIT](LICENSE.md) © Alex Shaw | ||
[pandoc]: https://pandoc.org/MANUAL.html#definition-lists | ||
[PHP Markdown Extra]: https://michelf.ca/projects/php-markdown/extra/#def-list |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
117
Yes
10166
6
108
1
+ Added@types/debug@4.1.12(transitive)
+ Added@types/ms@0.7.34(transitive)
+ Addedcharacter-entities@2.0.2(transitive)
+ Addeddecode-named-character-reference@1.0.2(transitive)
+ Addeddequal@2.0.3(transitive)
+ Addeddiff@5.2.0(transitive)
+ Addedkleur@4.1.5(transitive)
+ Addedlongest-streak@3.1.0(transitive)
+ Addedmdast-util-from-markdown@1.3.1(transitive)
+ Addedmdast-util-phrasing@3.0.1(transitive)
+ Addedmdast-util-to-markdown@1.5.0(transitive)
+ Addedmdast-util-to-string@3.2.0(transitive)
+ Addedmicromark@3.2.0(transitive)
+ Addedmicromark-core-commonmark@1.1.0(transitive)
+ Addedmicromark-factory-destination@1.1.0(transitive)
+ Addedmicromark-factory-label@1.1.0(transitive)
+ Addedmicromark-factory-space@1.1.0(transitive)
+ Addedmicromark-factory-title@1.1.0(transitive)
+ Addedmicromark-factory-whitespace@1.1.0(transitive)
+ Addedmicromark-util-character@1.2.0(transitive)
+ Addedmicromark-util-chunked@1.1.0(transitive)
+ Addedmicromark-util-classify-character@1.1.0(transitive)
+ Addedmicromark-util-combine-extensions@1.1.0(transitive)
+ Addedmicromark-util-decode-numeric-character-reference@1.1.0(transitive)
+ Addedmicromark-util-decode-string@1.1.0(transitive)
+ Addedmicromark-util-encode@1.1.0(transitive)
+ Addedmicromark-util-html-tag-name@1.2.0(transitive)
+ Addedmicromark-util-normalize-identifier@1.1.0(transitive)
+ Addedmicromark-util-resolve-all@1.1.0(transitive)
+ Addedmicromark-util-sanitize-uri@1.2.0(transitive)
+ Addedmicromark-util-subtokenize@1.1.0(transitive)
+ Addedmicromark-util-symbol@1.1.0(transitive)
+ Addedmicromark-util-types@1.1.0(transitive)
+ Addedmri@1.2.0(transitive)
+ Addedsade@1.8.1(transitive)
+ Addedunist-util-is@5.2.1(transitive)
+ Addedunist-util-stringify-position@3.0.3(transitive)
+ Addedunist-util-visit@4.1.2(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
+ Addeduvu@0.5.6(transitive)
+ Addedzwitch@2.0.4(transitive)
- Removedcharacter-entities@1.2.4(transitive)
- Removedcharacter-entities-legacy@1.1.4(transitive)
- Removedcharacter-reference-invalid@1.1.4(transitive)
- Removedis-alphabetical@1.0.4(transitive)
- Removedis-alphanumerical@1.0.4(transitive)
- Removedis-decimal@1.0.4(transitive)
- Removedis-hexadecimal@1.0.4(transitive)
- Removedlongest-streak@2.0.4(transitive)
- Removedmdast-util-from-markdown@0.8.5(transitive)
- Removedmdast-util-to-markdown@0.6.5(transitive)
- Removedmdast-util-to-string@2.0.0(transitive)
- Removedmicromark@2.11.4(transitive)
- Removedparse-entities@2.0.0(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-stringify-position@2.0.3(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
- Removedzwitch@1.0.5(transitive)
Updatedmdast-util-to-string@^3.1.0
Updatedunist-util-visit@^4.0.0