Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Any idea how i can convert links to this format (text)[link] #247

Unanswered
niveKKumar asked this question in Q&A
Discussion options

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]

You must be logged in to vote

Replies: 4 comments 2 replies

Comment options

You will need a custom formatter.

  1. Refer to this readme section: https://github.com/html-to-text/node-html-to-text#override-formatting
  2. 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
You must be logged in to vote
1 reply
Comment options

I actually dont understand the custom formatter at all 😅😄

Comment options

 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 ? 😆 
You must be logged in to vote
1 reply
Comment options

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.

Comment options

@niveKKumar did you manage to get it to work? I need this as well

You must be logged in to vote
0 replies
Comment options

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);
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /