
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
string-style-html
Advanced tools
Converts a string and a simple 'style'-instructions string,
to HTML code that has styling tags inserted.
It also HTML-encodes any &
, <
, and >
characters in the string,
so they don't interfere with any inserted style tags.
This functionality is used by both
vsm-autocomplete
and
vsm-box
.
This package provides a function f(str, style, extraContent)
.
All arguments are Strings, and the last two are optional.
If style
is not given, or a not String, or ''
,
then it returns str
unchanged.
f('abcd', '')
returns 'abcd'
.If style
is a String that contains a '<'
, then it is supposed to hold
a styled version of str
, with HTML-tags already inserted.
In this case it returns style
.
f('abcd', 'a<i>bc</i>d')
returns 'a<i>bc</i>d'
.Else it returns str
, with HTML styling tags inserted:
style
is the single character 'i'
, 'b'
, 's'
, or 'u'
, then
it applies the italic, bold, subscript, or superscript style resp.,
to the entire string.
f('abcd', 'i')
returns '<i>abcd</i>'
.style
can also include a range '{startIndex}-{stopIndex}' that says
which part of the string should be styled.
E.g. 'i0-3'
applies italic to the first three characters of str
.stopIndex
is the location just before where
styling stops.
f('abcd', 'i1-3')
returns 'a<i>bc</i>d'
.style
can also include a single index for a one-character range.
f('abcd', 'i2')
returns 'ab<i>c</i>d'
.style
can include multiple styling instructions, separated by a ;
.f('abcd', 'i1;u2-4')
returns 'a<i>b</i><sup>cd</sup>'
.It html-encodes <
, >
, and &
(also if no style
is given).
f('<b&d>', 'b2')
returns '<b<b>&</b>d>'
.If given an extraContent
argument,
it adds that extra content to inserted opening-tags.
f('abc', 'i', 'style="pointer-events: none;"')
returns
'<i style="pointer-events: none;">abc</i>'
.const stringStyleHtml = require('string-style-html');
console.dir( stringStyleHtml('cdc2', 'i') );
// => '<i>cdc2</i>'.
console.dir( stringStyleHtml('Ca2+', 'u2-4') );
// => 'Ca<sup>2+</sup>'.
console.dir( stringStyleHtml('HCO3-', 's3;u4') );
// =>'HCO<sub>3</sub><sup>-</sup>'.
console.dir( stringStyleHtml('abc', 'ab<span style="color: #f00;">c</span>') );
// =>'ab<span style="color: #f00;">c</span>'.
This project is licensed under the AGPL license - see LICENSE.md.
FAQs
Converts a string and a style-instructions string to HTML
We found that string-style-html demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.