Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mdast-comment-marker

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-comment-marker - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

20

index.d.ts
/**
* 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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc