Registry / serialization / md-attr-parser

md-attr-parser

JSON →
library1.3.0jsnpmunverified

The `md-attr-parser` package, currently at stable version 1.3.0, provides a focused utility for extracting custom HTML attributes embedded within Markdown strings. It parses attribute blocks, typically enclosed in curly braces `{}` immediately following a Markdown element, to retrieve an ID, a list of classes, and arbitrary key-value pairs. The library outputs a structured object containing the parsed properties and the exact substring that was consumed. While there's no stated release cadence, as a specialized parser, updates are typically driven by bug fixes or syntax extensions rather than frequent iterations. Its key differentiators include the ability to start parsing from a specific offset within a string, and a configurable `defaultValue` option for attributes that are present but lack an explicit value, allowing for flexible integration into Markdown processing pipelines.

npm install md-attr-parser
INSTALL
IMPORT
SIG · MD-ATTR-PARSER
M
md-attr-parser
serializationjavascriptv1.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseAttr
const parseAttr = require('md-attr-parser');
Primary CommonJS export as shown in documentation.
parseAttr
import parseAttr from 'md-attr-parser';
import { parseAttr } from 'md-attr-parser';
Likely default ESM export due to single main function, though CJS is explicitly documented. Named import might fail.

Demonstrates basic parsing of markdown attributes and advanced usage with an offset and custom default values for valueless keys.

const parseAttr = require('md-attr-parser'); // Example 1: Basic parsing of attributes including id, class, and key-value pairs const result1 = parseAttr('{ width=500px editable=true #unicorn .dangerous .cute }'); console.log('Result 1:', result1); /* Expected output: { prop: { class: ['dangerous', 'cute'], id: 'unicorn', width: '500px', editable: 'true' }, eaten: '{ width=500px editable=true #unicorn .dangerous .cute }' } */ // Example 2: Parsing with a starting offset and custom default value for keys without explicit values const textWithPrefix = 'START_HERE{ width=500px editable }'; const result2 = parseAttr(textWithPrefix, 'START_HERE'.length, { defaultValue: true }); console.log('Result 2:', result2); /* Expected output: { prop: { width: '500px', editable: true }, eaten: '{ width=500px editable }' } */
Debug
Known issues
gotchaThe library is primarily designed for CommonJS (CJS) environments, and direct usage with ESM 'import' statements might require specific build configurations or Node.js flags for interoperability.
fix
If using ESM, try 'import parseAttr from "md-attr-parser"' or ensure your build system correctly transpiles CJS modules. For Node.js, consider checking if an 'exports' field is present in package.json or using dynamic import().
affects: >=1.0.0
gotchaThe parser expects attributes to follow a specific Markdown syntax (e.g., {#id .class key=value}). Malformed attribute strings or unescaped characters within values may lead to incorrect parsing or unexpected results.
fix
Ensure attribute strings adhere strictly to the documented syntax. For complex values or those containing special characters, consider pre-processing or post-processing the input/output to avoid conflicts.
affects: >=1.0.0
gotchaThe 'defaultValue' configuration option only applies to keys without explicit values (e.g., '{ editable }'). It does not affect keys with empty string values (e.g., '{ editable="" }').
fix
Understand the distinction: 'editable' will use 'defaultValue', while 'editable=""' will result in 'editable: ""'. Adjust expectations or pre-process input if specific handling for empty string values is required.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: parseAttr is not a function
Attempting to import a CommonJS module using a named ESM import, or incorrect destructuring in a CJS context.
fix
If in CommonJS, use 'const parseAttr = require("md-attr-parser");'. If in ESM, try 'import parseAttr from "md-attr-parser";' as it's likely a default export.
Output 'prop' object missing expected attributes (e.g., id or class array is empty)
Input markdown attribute string does not conform to the expected syntax for IDs (#id) or classes (.class), or there are general syntax errors within the attribute block.
fix
Verify the input string closely matches the documented syntax: '#id .class key=value'. Ensure no leading/trailing spaces within the attribute block unless intended, and proper quotes for values.
Parsing starts too early or includes unwanted prefix characters in 'eaten' property.
Incorrect usage of the 'offset' parameter, or no offset provided when the attribute block is not at the beginning of the string.
fix
Provide the correct character offset for the beginning of the attribute string. For example, 'parseAttr(fullString, fullString.indexOf('{'));' if the attribute block is '{...}' and its position is unknown.
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
md-attr-parser — npm install md-attr-parser · libregistry