string-strip-html
examples
Table of Contents
Quick Take
import { strict as assert } from "assert";
import stripHtml from "string-strip-html";
assert.equal(
stripHtml(`Some text <b>and</b> text.`).result,
`Some text and text.`
);
assert.equal(
stripHtml(`aaa<div>bbb</div>ccc`).result,
`aaa bbb ccc`
);
assert.equal(
stripHtml(`a <pre><code>void a;</code></pre> b`, {
stripTogetherWithTheirContents: [
"script",
"style",
"xml",
"pre",
],
}).result,
`a b`
);
assert.equal(
stripHtml(`a < b and c > d`).result,
`a < b and c > d`
);
import { strict as assert } from "assert";
import stripHtml from "string-strip-html";
const someHtml = `<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>the title</title>
</head>
<body>
the content
</body>
</html>`;
const headWithHeadTags = stripHtml(someHtml, {
onlyStripTags: ["head"],
stripTogetherWithTheirContents: ["head"],
})
.filteredTagLocations.reduce(
(acc, [from, to]) =>
`${acc}${someHtml.slice(from, to)}`,
""
)
.trim();
assert.equal(
headWithHeadTags,
`<head>
<meta charset="utf-8">
<title>the title</title>
</head>`
);
const headContents = headWithHeadTags
.replace(/<\/?head>/g, "")
.trim();
assert.equal(
headContents,
`<meta charset="utf-8">
<title>the title</title>`
);
Leave Only HTML
import { strict as assert } from "assert";
import stripHtml from "string-strip-html";
const someHtml = `<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Title</h1>
Some text.
</body>
</html>`;
assert.equal(
stripHtml(someHtml).allTagLocations.reduce(
(acc, [from, to]) =>
`${acc}${someHtml.slice(from, to)}`,
""
),
`<!DOCTYPE html><html lang="en" dir="ltr"><head><meta charset="utf-8"><title></title></head><body><h1></h1></body></html>`
);
import { strict as assert } from "assert";
import stripHtml from "string-strip-html";
const someHtml = `<table width="100" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="col1">
cell1
</td>
<td class="col2">
cell2
</td>
</tr>
<tr>
<td class="col3">
cell3
</td>
<td class="col4">
cell4
</td>
</tr>
</table>`;
assert.equal(
stripHtml(someHtml, {
cb: ({
tag,
deleteFrom,
deleteTo,
insert,
rangesArr,
proposedReturn,
}) => {
if (tag.name === "td" && !tag.slashPresent) {
rangesArr.push(proposedReturn);
}
},
}).ranges.reduce(
(acc, [from, to]) =>
`${acc}${someHtml.slice(from, to).trim()}`,
""
),
`<td class="col1"><td class="col2"><td class="col3"><td class="col4">`
);
let resultStr = "";
stripHtml(someHtml, {
cb: ({
tag,
deleteFrom,
deleteTo,
insert,
rangesArr,
proposedReturn,
}) => {
if (tag.name === "td" && !tag.slashPresent) {
resultStr += someHtml
.slice(deleteFrom, deleteTo)
.trim();
}
},
});
assert.equal(
resultStr,
`<td class="col1"><td class="col2"><td class="col3"><td class="col4">`
);
import { strict as assert } from "assert";
import stripHtml from "string-strip-html";
const someHtml = `<table width="100" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="col1">
cell1
</td>
<td class="col2">
cell2
</td>
</tr>
<tr>
<td class="col3">
cell3
</td>
<td class="col4">
cell4
</td>
</tr>
</table>`;
assert.equal(
stripHtml(someHtml, {
onlyStripTags: ["td"],
}).filteredTagLocations.reduce(
(acc, [from, to]) =>
`${acc}${someHtml.slice(from, to)}`,
""
),
`<td class="col1"></td><td class="col2"></td><td class="col3"></td><td class="col4"></td>`
);
Remove All HTML from a String
import { strict as assert } from "assert";
import stripHtml from "string-strip-html";
const someHtml = `<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Title</h1>
Some text.
</body>
</html>`;
assert.equal(
stripHtml(someHtml).result,
`Title\nSome text.`
);
Strip HTML from a Raw JSON String
import { strict as assert } from "assert";
import stripHtml from "string-strip-html";
import traverse from "ast-monkey-traverse";
const stripFromJsonStr = (str) => {
return traverse(JSON.parse(str), (key, val) => {
const current = val !== undefined ? val : key;
if (
typeof val === "string"
) {
return stripHtml(val).result;
}
return current;
});
};
assert.equal(
JSON.stringify(
stripFromJsonStr(
`{"Operator":"<","IsValid":true}`
),
null,
0
),
`{"Operator":"<","IsValid":true}`
);
assert.equal(
JSON.stringify(
stripFromJsonStr(
`{"Operator":"a <div>b</div> c","IsValid":true}`
),
null,
0
),
`{"Operator":"a b c","IsValid":true}`
);
Set the Title Case Using title
Package
import { strict as assert } from "assert";
import title from "title";
import invertRanges from "ranges-invert";
import applyRanges from "ranges-apply";
import stripHtml from "string-strip-html";
const rangesRegex = require("ranges-regex");
function tagAwareTitle(str) {
const whitelist = ["eslint", "readme", "npm"];
const { filteredTagLocations } = stripHtml(str, {
stripTogetherWithTheirContents: ["*"],
});
const inverted = invertRanges(
filteredTagLocations.concat(
whitelist.reduce((acc, curr) => {
const rangesFindings = rangesRegex(
new RegExp(curr, "gi"),
str
);
if (rangesFindings) {
return acc.concat(rangesFindings);
}
return acc;
}, [])
),
str.length
);
if (Array.isArray(inverted) && inverted.length) {
return applyRanges(
str,
inverted.map(([from, to]) => [
from,
to,
title(str.slice(from, to)),
])
);
}
return title(str);
}
assert.equal(
tagAwareTitle(
`This is a title with some <code>code</code> in it`
),
`This Is a Title with Some <code>code</code> In It`
);
assert.equal(
tagAwareTitle(
`<span class="xyz">abc<span> defgh ESLint`
),
`<span class="xyz">abc<span> Defgh ESLint`
);