-
-
Notifications
You must be signed in to change notification settings - Fork 223
Any idea how i can convert links to this format (text)[link] #247
-
Hey I would liket to use this lib to convet html strings in discord format, so I am required to convert it not to textblock [link], but my textblock with () around => (textblock)[link]
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 4 comments 2 replies
-
You will need a custom formatter.
- Refer to this readme section: https://github.com/html-to-text/node-html-to-text#override-formatting
- Take the original formatter as a base and modify it for your needs: https://github.com/html-to-text/node-html-to-text/blob/master/lib/formatter.js#L170-L205
Beta Was this translation helpful? Give feedback.
All reactions
-
I actually dont understand the custom formatter at all 😅😄
Beta Was this translation helpful? Give feedback.
All reactions
-
discord: function formatAnchor(elem, walk, builder, formatOptions) {
function getHref() {
if (formatOptions.ignoreHref) {
return "";
}
if (!elem.attribs || !elem.attribs.href) {
return "";
}
let href = elem.attribs.href.replace(/^mailto:/, "");
if (formatOptions.noAnchorUrl && href[0] === "#") {
return "";
}
href = formatOptions.baseUrl && href[0] === "/" ? formatOptions.baseUrl + href : href;
return he.decode(href, (builder as any).options.decodeOptions);
}
const href = getHref();
if (!href) {
walk(elem.children, builder);
} else {
builder.pushWordTransform((str) => {
if (str) {
}
return hyperlink(str, href);
});
walk(elem.children, builder);
builder.popWordTransform();
}
},
```
Tried it but it seems to hyperlink every word I have no clue how the rest with of theses funtions with the builder works
I just want to output it as (text)[url]
Cant be so hard right ? 😆
Beta Was this translation helpful? Give feedback.
All reactions
-
Hint: you do not need builder.pushWordTransform for this.
In the existing formatter it is only used to peek into contained text and check whether it is the same as the url.
Beta Was this translation helpful? Give feedback.
All reactions
-
@niveKKumar did you manage to get it to work? I need this as well
Beta Was this translation helpful? Give feedback.
All reactions
-
for future ref, this works:
import { htmlToText, FormatCallback } from 'html-to-text'; const formatAnchor: FormatCallback = (elem, walk, builder, formatOptions) => { function getHref() { if (formatOptions.ignoreHref) { return ''; } if (!elem.attribs || !elem.attribs.href) { return ''; } const href: string = elem.attribs.href.replace(/^mailto:/, ''); if (formatOptions.noAnchorUrl && href[0] === '#') { return ''; } if (formatOptions.baseUrl) { return formatOptions.baseUrl + href; } return href; } const href = getHref(); builder.addInline('['); walk(elem.children, builder); builder.addInline(']'); const openLinkBracket = formatOptions.linkBrackets ? formatOptions.linkBrackets[0] : ''; const closeLinkBracket = formatOptions.linkBrackets ? formatOptions.linkBrackets[1] : ''; builder.addInline(`${openLinkBracket}${href}${closeLinkBracket}`, { noWordTransform: true }); }; const html = ` <div>ABC</div> <a href="/employment-practices#SAIA" data-hash="#Section-Employmentpractises" class="link-has-image home-page-menu"> <div class="nav-icon"> <img src="/-/media/mom/files/images/home-page/icon-employment-practices.png" alt="Employment practices"> </div> <span>Employment practices</span> <span class="nav-desc"> Leave, public holidays, employment rights and conditions, schemes, claims, skills and training. </span> </a> `; const text = htmlToText(html, { formatters: { formatAnchor }, selectors: [ { selector: 'img', format: 'skip' }, { selector: 'svg', format: 'skip' }, { selector: 'div', format: 'inline' }, { selector: 'a', format: 'formatAnchor', options: { baseUrl: 'https://example.com', linkBrackets: ['(', ')'] } } ], wordwrap: false }); // ABC [ Employment practices Leave, public holidays, employment rights and conditions, schemes, claims, skills and training. ](https://example.com/employment-practices#SAIA) console.log(text);
Beta Was this translation helpful? Give feedback.