Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keep href value in link

See original GitHub issue

Is it possibile with the current api to keep both the content and the href value in the <a> tag?


stripHtml(`<a href="">twitter:loretparisi&nbsp;&oslash;</a>`).result
" twitter:loretparisi ø"

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

reveltcommented, Jun 1, 2021

@paulthemagno Let me explain. When we need to mark multiple amendments on string, we need a way to mark “delete from this index to this index”, or “delete from this to this but also insert this”. Solution — Ranges. It’s just array of arrays: [[0, 1], [3,4, “add”]] — means delete from index 0 to index 1; then replace slice between index 3 and 4 with “add”.

I suspect what is happening, since callback API is consistent, it had to serve something for “insert”, so it gives you an empty string, meaning: replace [deleteFrom, deleteTo] with empty string. If third element in a range array is missing, null or an empty string, it’s the same — deletion.

Conceptually, there’s no way to get twitter:loretparisi&nbsp;&oslash; because program only processes the HTML tags, opening <a> separately from </a>

In theory, one way to “get” twitter:loretparisi&nbsp;&oslash; tied to the tag pair would be to use a parser (like this), traverse (for example using this), push each tag location to let’s say same Ranges (using this), then apply them onto a string (using this). I’m doing exact same thing in stristri, — it’s also an HTML stripping tool but based off a tokenizer — 164 lines of code.

But that above might be overkill, try the callback, replacing tag-by-tag.

It depends what you need to achieve, I can draw up more examples if it helps. Also, have a look at existing examples.

reveltcommented, May 28, 2021

seems doable, using opts.cb:

// Retain href and link label

import { strict as assert } from "assert";
import { stripHtml } from "../dist/string-strip-html.esm.js";

const someHtml = `<a href="">twitter:loretparisi&nbsp;&oslash;</a>`;

  stripHtml(someHtml, {
    skipHtmlDecoding: true,

  stripHtml(someHtml, {
    skipHtmlDecoding: true,
    cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => {
      let temp;
      if ( === "a" &&
        tag.attributes &&
        tag.attributes.some((attr) => {
          if ( === "href") {
            temp = attr.value;
            return true;
      ) {
        rangesArr.push([deleteFrom, deleteTo, `${temp} ${insert}`]);
      } else {
  ` twitter:loretparisi&nbsp;&oslash;`

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is an empty href valid? - html - Stack Overflow
The href attribute on a and area elements must have a value that is a valid URL potentially surrounded by spaces. A valid...
Read more >
HTML A Href Attribute: A Quick And Simple Guide »
All Attributes of the anchor Element​​ Defines the title of a link, which appears to the user as a tooltip. Specifies the linked...
Read more >
The HTML a href Attribute Explained with Examples
The href attribute can be used to link to local files or files on the internet. The <a href> attribute is supported by...
Read more >
HTML a href Attribute - W3Schools
The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag...
Read more >
<a>: The Anchor element - HTML: HyperText Markup Language
The <a> HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found