{"_quickTake.js":{"title":"Quick Take","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nassert.equal(\n stripHtml(`Some text and text.`).result,\n `Some text and text.`\n);\n\n// prevents accidental string concatenation\nassert.equal(stripHtml(`aaa
and .\n// First, extract head tag-to-head tag, including contents\nconst headWithHeadTags = stripHtml(someHtml, {\n onlyStripTags: [\"head\"],\n stripTogetherWithTheirContents: [\"head\"],\n})\n .filteredTagLocations.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`,\n \"\"\n )\n .trim();\n\nassert.equal(\n headWithHeadTags,\n `\n \n the title\n `\n);\n\nconst headContents = headWithHeadTags.replace(/<\\/?head>/g, \"\").trim();\nassert.equal(\n headContents,\n `\n the title`\n);"},"inline-tags.js":{"title":"Just deletes inline tags","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `This has an unbold word.`;\n\n// default behaviour:\nassert.equal(stripHtml(someHtml).result, `This has an un bold word.`);\n\n// let's tackle inline tags:\nassert.equal(\n stripHtml(someHtml, {\n cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr }) => {\n if ([\"b\", \"strong\"].includes(tag.name)) {\n rangesArr.push(tag.lastOpeningBracketAt, tag.lastClosingBracketAt + 1);\n } else {\n rangesArr.push(deleteFrom, deleteTo, insert);\n }\n },\n }).result,\n `This has an unbold word.`\n);"},"leave-only-html.js":{"title":"Leave only HTML","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `\n\n \n \n \n \n \n Title
\n Some text.\n \n`;\n\nassert.equal(\n stripHtml(someHtml).allTagLocations.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`,\n \"\"\n ),\n ``\n);"},"leave-only-opening-td.js":{"title":"Leave only opening `td` tags","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `\n \n \n cell1\n | \n \n cell2\n | \n
\n \n \n cell3\n | \n \n cell4\n | \n
\n
`;\n\n// the first way\n// -----------------------------------------------------------------------------\n\nassert.equal(\n stripHtml(someHtml, {\n // notice there's no: onlyStripTags: [\"td\"]\n // we operate purely via callback\n cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => {\n if (tag.name === \"td\" && !tag.slashPresent) {\n rangesArr.push(proposedReturn);\n }\n },\n }).ranges.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to).trim()}`,\n \"\"\n ),\n ` | | | `\n);\n\n// the second way:\n// -----------------------------------------------------------------------------\n\nlet resultStr = \"\";\n// notice we don't even assign stripHtml() output to anything - we rely only\n// on the callback, it mutates the \"resultStr\" in the upper scope\nstripHtml(someHtml, {\n // notice there's no: onlyStripTags: [\"td\"]\n // we operate purely via callback\n cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => {\n if (tag.name === \"td\" && !tag.slashPresent) {\n resultStr += someHtml.slice(deleteFrom, deleteTo).trim();\n }\n },\n});\nassert.equal(\n resultStr,\n ` | | | | `\n);"},"leave-only-td.js":{"title":"Leave only `td` tags","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `\n \n \n cell1\n | \n \n cell2\n | \n \n \n \n cell3\n | \n \n cell4\n | \n \n `;\n\nassert.equal(\n stripHtml(someHtml, {\n onlyStripTags: [\"td\"],\n }).filteredTagLocations.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`,\n \"\"\n ),\n ` | | | | | `\n);"},"minimal-ranges.js":{"title":"Minimal example using Ranges","content":"// We strip tags and fix apostrophes\n// that's part of what https://codsen.com/os/detergent/ does\n\nimport { strict as assert } from \"assert\";\nimport { rApply } from \"ranges-apply\";\nimport { stripHtml } from \"string-strip-html\";\nimport { convertAll } from \"string-apostrophes\";\n\nfunction stripAndFixApos(str) {\n if (!str || typeof str !== \"string\") {\n return \"\";\n }\n // Keep in mind, Ranges are array of 2-3 element arrays.\n // But absent Ranges are marked as null, not empty array.\n // It's so that we could test in \"if-else\" easily - null\n // is falsy but empty array is truthy.\n // That's why below we take precautions with \"|| []\".\n return rApply(\n str,\n (stripHtml(str).ranges || []).concat(convertAll(str).ranges || [])\n );\n}\n\n// strips tags and fixes apostrophes:\nassert.equal(\n stripAndFixApos(`Let's Go Larval`),\n `Letβs Go Larval`\n);\n\n// no tags, no apostrophes:\nassert.equal(stripAndFixApos(`zzz`), `zzz`);"},"remove-html.js":{"title":"Remove all HTML from a string","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `\n\n \n \n \n \n \n Title
\n Some text.\n \n`;\n\nassert.equal(stripHtml(someHtml).result, `Title\\nSome text.`);"},"strip-from-json.js":{"title":"Strip HTML from a raw JSON string","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\nimport { traverse } from \"ast-monkey-traverse\";\n\nconst stripFromJsonStr = (str) => {\n return traverse(JSON.parse(str), (key, val) => {\n // if currently an object is traversed, you get both \"key\" and \"val\"\n // if it's array, only \"key\" is present, \"val\" is undefined\n const current = val !== undefined ? val : key;\n if (\n // ensure it's a plain object, not array (monkey will report only \"key\" in\n // arrays and \"val\" will be undefined)\n // also ensure object's value a string, not boolean or number, because we\n // don't strip HTML from booleans or numbers or anything else than strings\n typeof val === \"string\"\n ) {\n // monkey's callback is like Array.map - whatever you return gets written:\n return stripHtml(val).result;\n }\n // default return, do nothing:\n return current;\n });\n};\n\n// nothing to strip, \"<\" is false alarm:\nassert.equal(\n JSON.stringify(stripFromJsonStr(`{\"Operator\":\"<\",\"IsValid\":true}`), null, 0),\n `{\"Operator\":\"<\",\"IsValid\":true}`\n);\n\n// some HTML within one of key values, monkey will skip the boolean:\nassert.equal(\n JSON.stringify(\n stripFromJsonStr(`{\"Operator\":\"a b
c\",\"IsValid\":true}`),\n null,\n 0\n ),\n `{\"Operator\":\"a b c\",\"IsValid\":true}`\n);"},"title-case-with-tag-skipping.js":{"title":"Set the title case using `title` package","content":"// This program will not touch any single tags (
for example)\n// or in case of paired tags, paired tags and content between\n\nimport { strict as assert } from \"assert\";\nimport title from \"title\";\nimport { rInvert } from \"ranges-invert\";\nimport { rApply } from \"ranges-apply\";\nimport { rRegex } from \"ranges-regex\";\nimport { stripHtml } from \"string-strip-html\";\n\nfunction tagAwareTitle(str) {\n const whitelist = [\"eslint\", \"readme\", \"npm\"];\n const { filteredTagLocations } = stripHtml(str, {\n stripTogetherWithTheirContents: [\"*\"],\n });\n const inverted = rInvert(\n filteredTagLocations.concat(\n whitelist.reduce((acc, curr) => {\n const rangesFindings = rRegex(new RegExp(curr, \"gi\"), str);\n if (rangesFindings) {\n return acc.concat(rangesFindings);\n }\n return acc;\n }, [])\n ),\n str.length\n );\n\n if (Array.isArray(inverted) && inverted.length) {\n // take inverted ranges, for example, [[3, 4], [10, 15]]\n // and add third element, replacement, which is same character\n // indexes only processed through \"title\":\n return rApply(\n str,\n inverted.map(([from, to]) => [from, to, title(str.slice(from, to))])\n );\n }\n // otherwise, just apply title() on the whole string:\n return title(str);\n}\n\n// middle:\nassert.equal(\n tagAwareTitle(`This is a title with some code
in it`),\n `This Is a Title with Some code
In It`\n);\n\n// leading:\nassert.equal(\n tagAwareTitle(`abc defgh ESLint`),\n `abc Defgh ESLint`\n);"},"widow-word-removal-from-html.js":{"title":"Widow word removal from text within HTML","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\nimport { removeWidows } from \"string-remove-widows\";\n\nconst someHtml = `The quick brown fox jumps of the lazy dog.`;\n\n// default widow word removal libs are not aware of HTML:\n// -----------------------------------------------------------------------------\n\nassert.equal(\n removeWidows(someHtml).res,\n `The quick brown fox jumps of the lazy dog.
` // π±\n);\n\n// luckily, removeWidows() consumes optional HTML tag locations\nassert.equal(\n removeWidows(someHtml, {\n tagRanges: stripHtml(someHtml)\n // remove the third argument, what to insert (\" \" string in these cases)\n .ranges.map(([from, to]) => [from, to]),\n }).res,\n `The quick brown fox jumps of the lazy dog.
` // β
\n);"}}