unified-doc-util-mark
unified-doc hast utility to mark text nodes.
Install
npm install unified-doc-util-mark
Use
Given a hast
tree parsed from some HTML content:
; // html: '<blockquote><strong>some</strong>\ncontent</blockquote>';const hast = type: 'root' children: type: 'element' tagName: 'blockquote' children: type: 'element' tagName: 'strong' children: type: 'text' value: 'some' type: 'text' value: '\ncontent' ; const marks = id: 'a' start: 1 end: 4 classNames: 'a' 'b' ; ;
API
mark(hast, marks)
Interface
Accepts a hast
tree and applies marks
to overlapping text nodes. Returns a new tree.
mark
also supports overlapping marks
, and applying custom properties to marked nodes (classNames
, style
, dataset
).
const marks = id: 'a' start: 3 end: 8 classNames: 'a' 'b' dataset: category: 'A' id: 'b' start: 6 end: 10 style: background: 'red' ; ;
Related interfaces
A Mark
is an object that contains at least the id
, start
and end
properties, which provides positional information on segments of text nodes that can be marked. Marks are useful in various document features:
- highlight/mark segments of text (e.g. search results).
- serve as positional anchors to identify segments of text in a document.
mark
nodes can be visually customized by specifying the optional classNames
, style
and dataset
properties. All other data maybe organized under the data
property.
The start
and end
properties of a Mark
indicate text offset values relative to the textContent
of the provided hast
tree. The mark algorithm uses these offsets to determine how to insert mark
nodes in the tree only within the specified offset range, while preserving the semantic structure of the tree.
The following pseudocode helps visualize this behavior:
const html = '<blockquote><strong>some</strong>\ncontent</blockquote>'jjconst textContent = 'some\ncontent';const textNodes = 'some' '\ncontent';const textOffsets = start: 0 end: 4 // "[some]\ncontent" start: 4 end: 12 // "some[\ncontent]"; const marks = id: 'a' classNames: 'a' 'b' start: 3 end: 8 id: 'b' style: background: 'red' start: 6 end: 10 ;const markedTextSegments = value: 'som' // "[som]e" text node textOffset: start: 0 end: 3 markIds: // not marked value: 'e' // "som[e]" text node textOffset: start: 3 end: 4 markIds: 'a' // marked by 'a' value: '\nc' // "[\nc]ontent" text node textOffset: start: 4 end: 6 markIds: 'a' // marked by 'a' value: 'on' // "\nc[on]tent" text node textOffset: start: 6 end: 8 markIds: 'a' 'b' // marked by 'a' + 'b' value: 'te' // "\ncon[te]nt" text node textOffset: start: 8 end: 10 markIds: 'b' // marked by 'b' value: 'nt' // "\nconte[nt]" text node textOffset: start: 10 end: 12 markIds: // not marked ; // marked hast tree based on markedTextSegments computed aboveconst marked = ;