mdast-comment-marker
Advanced tools
Comparing version 2.0.0 to 2.1.0
/** | ||
* Parse a comment marker. | ||
* @param {unknown} node | ||
* @param {unknown} value | ||
* @returns {Marker|null} | ||
*/ | ||
export function commentMarker(node: unknown): Marker | null | ||
export function commentMarker(value: unknown): Marker | null | ||
export type MarkerParameterValue = string | number | boolean | ||
export type Root = import('mdast').Root | ||
export type Content = import('mdast').Content | ||
export type HTML = import('mdast').HTML | ||
export type MDXFlowExpression = | ||
import('mdast-util-mdx-expression').MDXFlowExpression | ||
export type MDXTextExpression = | ||
import('mdast-util-mdx-expression').MDXTextExpression | ||
export type Node = Root | Content | ||
export type MarkerParameters = { | ||
[x: string]: MarkerParameterValue | ||
} | ||
export type HtmlNode = { | ||
type: 'html' | ||
value: string | ||
} | ||
export type CommentNode = { | ||
export type Mdx1CommentNode = { | ||
type: 'comment' | ||
@@ -23,3 +27,3 @@ value: string | ||
parameters: MarkerParameters | null | ||
node: HtmlNode | CommentNode | ||
node: HTML | Mdx1CommentNode | MDXFlowExpression | MDXTextExpression | ||
} |
89
index.js
/** | ||
* @typedef {string|number|boolean} MarkerParameterValue | ||
* @typedef {import('mdast').Root} Root | ||
* @typedef {import('mdast').Content} Content | ||
* @typedef {import('mdast').HTML} HTML | ||
* @typedef {import('mdast-util-mdx-expression').MDXFlowExpression} MDXFlowExpression | ||
* @typedef {import('mdast-util-mdx-expression').MDXTextExpression} MDXTextExpression | ||
* @typedef {Root|Content} Node | ||
* @typedef {Object.<string, MarkerParameterValue>} MarkerParameters | ||
* | ||
* @typedef HtmlNode | ||
* @property {'html'} type | ||
* @property {string} value | ||
* | ||
* @typedef CommentNode | ||
* @typedef Mdx1CommentNode | ||
* @property {'comment'} type | ||
@@ -17,8 +19,10 @@ * @property {string} value | ||
* @property {MarkerParameters|null} parameters | ||
* @property {HtmlNode|CommentNode} node | ||
* @property {HTML|Mdx1CommentNode|MDXFlowExpression|MDXTextExpression} node | ||
*/ | ||
var commentExpression = /\s*([a-zA-Z\d-]+)(\s+([\s\S]*))?\s*/ | ||
var markerExpression = new RegExp( | ||
const commentExpression = /\s*([a-zA-Z\d-]+)(\s+([\s\S]*))?\s*/ | ||
const esCommentExpression = new RegExp( | ||
'(\\s*\\/\\*' + commentExpression.source + '\\*\\/\\s*)' | ||
) | ||
const markerExpression = new RegExp( | ||
'(\\s*<!--' + commentExpression.source + '-->\\s*)' | ||
@@ -29,38 +33,41 @@ ) | ||
* Parse a comment marker. | ||
* @param {unknown} node | ||
* @param {unknown} value | ||
* @returns {Marker|null} | ||
*/ | ||
export function commentMarker(node) { | ||
/** @type {RegExpMatchArray} */ | ||
var match | ||
/** @type {number} */ | ||
var offset | ||
/** @type {MarkerParameters} */ | ||
var parameters | ||
export function commentMarker(value) { | ||
if ( | ||
node && | ||
typeof node === 'object' && | ||
// @ts-ignore hush | ||
(node.type === 'html' || node.type === 'comment') | ||
isNode(value) && | ||
(value.type === 'html' || | ||
// @ts-expect-error: MDX@1 | ||
value.type === 'comment' || | ||
value.type === 'mdxFlowExpression' || | ||
value.type === 'mdxTextExpression') | ||
) { | ||
// @ts-ignore hush | ||
match = node.value.match( | ||
// @ts-ignore hush | ||
node.type === 'comment' ? commentExpression : markerExpression | ||
) | ||
let offset = 2 | ||
/** @type {RegExpMatchArray|null|undefined} */ | ||
let match | ||
// @ts-ignore hush | ||
if (match && match[0].length === node.value.length) { | ||
// @ts-ignore hush | ||
offset = node.type === 'comment' ? 1 : 2 | ||
parameters = parseParameters(match[offset + 1] || '') | ||
// @ts-expect-error: MDX@1 | ||
if (value.type === 'comment') { | ||
// @ts-expect-error: MDX@1 | ||
match = value.value.match(commentExpression) | ||
offset = 1 | ||
} else if (value.type === 'html') { | ||
match = value.value.match(markerExpression) | ||
} else if ( | ||
value.type === 'mdxFlowExpression' || | ||
value.type === 'mdxTextExpression' | ||
) { | ||
match = value.value.match(esCommentExpression) | ||
} | ||
if (match && match[0].length === value.value.length) { | ||
const parameters = parseParameters(match[offset + 1] || '') | ||
if (parameters) { | ||
return { | ||
name: match[offset], | ||
attributes: match[offset + 2] || '', | ||
attributes: (match[offset + 2] || '').trim(), | ||
parameters, | ||
// @ts-ignore hush | ||
node | ||
node: value | ||
} | ||
@@ -82,3 +89,3 @@ } | ||
/** @type {MarkerParameters} */ | ||
var parameters = {} | ||
const parameters = {} | ||
@@ -104,3 +111,3 @@ return value | ||
/** @type {MarkerParameterValue} */ | ||
var value = $2 || $3 || $4 || '' | ||
let value = $2 || $3 || $4 || '' | ||
@@ -120,1 +127,9 @@ if (value === 'true' || value === '') { | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is Node} | ||
*/ | ||
function isNode(value) { | ||
return Boolean(value && typeof value === 'object' && 'type' in value) | ||
} |
{ | ||
"name": "mdast-comment-marker", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "mdast utility to parse a comment marker", | ||
@@ -35,8 +35,8 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/mdast": "^3.0.10", | ||
"@types/tape": "^4.0.0", | ||
"@types/unist": "^2.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
@@ -46,3 +46,3 @@ "tape": "^5.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.44.0" | ||
}, | ||
@@ -55,3 +55,3 @@ "scripts": { | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test": "npm run format && npm run test-coverage" | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
@@ -69,4 +69,3 @@ "prettier": { | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
"unicorn/prefer-switch": "off" | ||
} | ||
@@ -83,3 +82,6 @@ }, | ||
"strict": true | ||
}, | ||
"dependencies": { | ||
"mdast-util-mdx-expression": "^1.1.0" | ||
} | ||
} |
@@ -44,6 +44,6 @@ # mdast-comment-marker | ||
// Also supports MDX@1 comment nodes. | ||
// Also supports MDX@2 expressions: | ||
console.log(commentMarker({ | ||
type: 'comment', | ||
value: 'bar' | ||
type: 'mdxFlowExpression', | ||
value: '/* lint disable heading-style */' | ||
})); | ||
@@ -55,17 +55,27 @@ ``` | ||
```js | ||
{ name: 'foo', | ||
{ | ||
name: 'foo', | ||
attributes: '', | ||
parameters: {}, | ||
node: { type: 'html', value: '<!--foo-->' } } | ||
{ name: 'foo', | ||
attributes: 'bar baz=12.4 qux="test test" quux=\'false\'', | ||
node: { type: 'html', value: '<!--foo-->' } | ||
} | ||
{ | ||
name: 'foo', | ||
attributes: `bar baz=12.4 qux="test test" quux='false'`, | ||
parameters: { bar: true, baz: 12.4, qux: 'test test', quux: false }, | ||
node: | ||
{ type: 'html', | ||
value: '<!--foo bar baz=12.4 qux="test test" quux=\'false\'-->' } } | ||
node: { | ||
type: 'html', | ||
value: `<!--foo bar baz=12.4 qux="test test" quux='false'-->` | ||
} | ||
} | ||
null | ||
{ name: 'bar', | ||
attributes: '', | ||
parameters: {}, | ||
node: { type: 'comment', value: 'bar' } } | ||
{ | ||
name: 'lint', | ||
attributes: 'disable heading-style', | ||
parameters: { disable: true, 'heading-style': true }, | ||
node: { | ||
type: 'mdxFlowExpression', | ||
value: '/* lint disable heading-style */' | ||
} | ||
} | ||
``` | ||
@@ -72,0 +82,0 @@ |
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
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
11783
145
184
0
1
+ Added@types/debug@4.1.12(transitive)
+ Added@types/estree@1.0.6(transitive)
+ Added@types/estree-jsx@1.0.5(transitive)
+ Added@types/hast@2.3.10(transitive)
+ Added@types/mdast@3.0.15(transitive)
+ Added@types/ms@0.7.34(transitive)
+ Added@types/unist@2.0.11(transitive)
+ Addedcharacter-entities@2.0.2(transitive)
+ Addeddebug@4.3.7(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-mdx-expression@1.3.2(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)
+ Addedms@2.1.3(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)