Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
text-annotator-v2
Advanced tools
A JavaScript library for annotating plain text in the HTML
The annotation process is:
text-annotator-v2 can be used in the browser or the Node.js server.
text-annotator-v2 evolved from text-annotator. See Comparing text-annotator-v2 and text-annotator.
npm install --save text-annotator-v2
import TextAnnotator from 'text-annotator-v2'
<script src="public/js/text-annotator-v2.min.js"></script>
/*
below is the HTML
<div id="content">
<p><i>JavaScript</i> is the <b>world's most popular programming language</b>.</p>
<p><i>JavaScript</i> is the programming language of the Web. JavaScript is easy to learn.</p>
</div>
*/
// create an instance of TextAnnotator by passing the html to be annotated
var annotator = new TextAnnotator(document.getElementById('content').innerHTML)
// search for text "JavaScript is the programming language of the Web." within the HTML
var annotationIndex = annotator.search('JavaScript is the programming language of the Web.')
// annotate the text if finding it
if (annotationIndex !== -1) {
document.getElementById('content').innerHTML = annotator.annotate(annotationIndex)
/*
<div id="content">
<p><i>JavaScript</i> is the <b>world's most popular programming language</b>.</p>
<p><i><span class="annotation annotation-0">JavaScript</span></i><span class="annotation annotation-0"> is the programming language of the Web.</span> JavaScript is easy to learn.</p>
</div>
*/
}
// search for all occurances of "JavaScript" in the HTML
var annotationIndexes = annotator.searchAll('JavaScript')
// annotate all the found occurances of 'Javascript' given their indexes
if (annotationIndexes.length) {
document.getElementById('content').innerHTML = annotator.annotateAll(annotationIndexes)
/*
<div id="content">
<p><i><span class="annotation annotation-1">JavaScript</span></i> is the <b>world's most popular programming language</b>.</p>
<p><i><span class="annotation annotation-0"><span class="annotation annotation-2">JavaScript</span></span></i><span class="annotation annotation-0"> is the programming language of the Web.</span> <span class="annotation annotation-3">JavaScript</span> is easy to learn.</p>
</div>
*/
}
// unannotate all the previously annotated text
document.getElementById('content').innerHTML = annotator.unannotate(annotationIndex)
document.getElementById('content').innerHTML = annotator.unannotateAll(annotationIndexes)
/*
<div id="content">
<p><i>JavaScript</i> is the <b>world's most popular programming language</b>.</p>
<p><i>JavaScript</i> is the programming language of the Web. JavaScript is easy to learn.</p>
</div>
*/
Two demo are also available on https://zealous-meitner-84f091.netlify.app/ and https://zealous-meitner-84f091.netlify.app/demo.html where you can check the source code and UI.
Param | Type | Description |
---|---|---|
html | string | The HTML string within which a piece of text can be annotated |
Prop | Type | Description |
---|---|---|
trim | boolean | Whether to trim the piece of text to be annotated. Default is true. |
caseSensitive | boolean | Whether to consider case in search. Default is false. |
prefix | string | A string BEFORE the piece of text to be annotated. Default is ''. |
postfix | string | A string AFTER the piece of text to be annotated. Default is ''. |
Prop | Type | Description |
---|---|---|
tagName | string | The name of the annotation tag. Default is span so that the tag is <span ...>. |
baseClassName | string | The base class name of the annotation tag. Default is annotation so that the tag is <span class="annotation" ...>. |
classPattern | string | The pattern of the class used as the ID of the annotation. Default is annotation- so that the tag is <span class="annotation annotation-[annotationIndex]" ...>. |
<div>This is an <i>apple</i></div>
If we want to annotate an apple, text-annotator will only use a single annotation tag pair
<div>This is <...>an <i>apple</i></...></div>
In contrast, text-annotator-v2 will use two annotation tag pairs
<div>This is <...>an </...><i><...>apple</...></i></div>
It seems text-annotator can provide a simpler solution in this case. However, text-annotator-v2 aims to give a correct solution in all cases. For instance, if an apple is surrounding by a pair of block tags, such as p, instead of i, annotating an apple using a single non-block tag pair such as span or mark will break the html structure. Another example where more than one pair of annotatation tags is needed for annotation is:
<p>These are apples. We like eating them.</p><p>Today we ate ten!</p>
If we want to lightlight We like eating them.Today we ate then!, we have to use 2 pairs of annotation tags as follows:
<p>These are apples. <...>We like eating them.</...></p><p><...>Today we ate ten!</...></p>
FAQs
A JavaScript library for locating and annotating plain text in HTML
The npm package text-annotator-v2 receives a total of 52 weekly downloads. As such, text-annotator-v2 popularity was classified as not popular.
We found that text-annotator-v2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.