@tomerlichtash/remark-directive-refs
v0.1.0
Published
Extract structured refs and anchors from remark-directive attributes, no rendering
Readme
@tomerlichtash/remark-directive-refs
Extract structured refs and anchors from
remark-directive attributes —
parsing only, no rendering. For aggregate pages (letters, threads) where each
item is a directive carrying its own refs:
:::entry{author="Sam Drifter" title="First letter" refs="kit-vale,jo-reed"}
…
:::import { extractDirectiveRefs, anchorSlug } from '@tomerlichtash/remark-directive-refs';
extractDirectiveRefs(body, {
directive: 'entry',
attr: 'refs', // comma-separated ids (default 'refs')
anchorFrom: ['title', 'author'], // slugged for `anchor`, first non-empty wins
});
// → [{ attrs: { author: 'Sam Drifter', … }, refs: ['kit-vale', 'jo-reed'], anchor: 'First-letter' }]Bodies are parsed with the same remark-directive your render pipeline uses (not a regex), so attribute quoting/escaping behaves identically. Directives without a non-empty refs attribute are skipped.
anchorSlug(text) is the shared slug rule (whitespace → dash, strip all but
letters/numbers/dashes) — import it render-side so backlink #anchors land on
the element ids you emit.
Resolving the extracted ids is a separate concern — see
@tomerlichtash/md-registry.
