Skip to content

Commit

Permalink
feat: Added useInlineLinks option
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias committed Dec 13, 2022
1 parent 82cf1e3 commit c318667
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -214,6 +214,18 @@ export interface NodeHtmlMarkdownOptions {
* [2]: /url2
*/
useLinkReferenceDefinitions?: boolean

/**
* Wrap URL text in < > instead of []() syntax.
*
* @example
* The input <a href="https://google.com">https://google.com</a>
* becomes <https://google.com>
* instead of [https://google.com](https://google.com)
*
* @default true
*/
useInlineLinks?: boolean
}
```

Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Expand Up @@ -68,7 +68,9 @@ export const defaultOptions: Readonly<NodeHtmlMarkdownOptions> = Object.freeze({
lineStartEscape: [
/^(\s*?)((?:\+\s)|(?:[=>-])|(?:#{1,6}\s))|(?:(\d+)(\.\s))/gm,
'$1$3\\$2$4'
] as const
] as const,

useInlineLinks: true
});

// endregion
Expand Down Expand Up @@ -268,7 +270,7 @@ export const defaultTranslators: TranslatorConfigObject = {

// Inline link, when possible
// See: https://github.com/crosstype/node-html-markdown/issues/17
if (node.textContent === href) return { content: `<${encodedHref}>` };
if (node.textContent === href && options.useInlineLinks) return { content: `<${encodedHref}>` };

return {
postprocess: ({ content }) => content.replace(/(?:\r?\n)+/g, ' '),
Expand Down
12 changes: 12 additions & 0 deletions src/options.ts
Expand Up @@ -99,6 +99,18 @@ export interface NodeHtmlMarkdownOptions {
* [2]: /url2
*/
useLinkReferenceDefinitions?: boolean

/**
* Wrap URL text in < > instead of []() syntax.
*
* @example
* The input <a href="https://google.com">https://google.com</a>
* becomes <https://google.com>
* instead of [https://google.com](https://google.com)
*
* @default true
*/
useInlineLinks?: boolean
}

// endregion
24 changes: 24 additions & 0 deletions test/options.test.ts
Expand Up @@ -294,4 +294,28 @@ text`);

instance.options.useLinkReferenceDefinitions = originalUseLinkReferenceDefinitions;
});

test(`useInlineLinks`, () => {
const originalUseInlineLinksDefinitions = instance.options.useInlineLinks;

const url = 'http://www.github.com/crosstype';
const html = `Hello:&nbsp;
<a href="${url}">${url}</a> <!-- inline link -->&nbsp;
<a>a<strong>b</strong></a> <!-- This node is treated as text due to no href -->
<a href="${url}/other">link2</a>
<a href="${url}">repeat link</a> Goodbye!
`;

instance.options.useInlineLinks = false;
let res = translate(html);
expect(res).toBe(`Hello: [${url}](${url}) a**b** [link2](${url}/other) [repeat link](${url}) Goodbye!`);

instance.options.useInlineLinks = true;
res = translate(html);
expect(res).toBe(
`Hello: <${url}> a**b** [link2](${url}/other) [repeat link](${url}) Goodbye!`
);

instance.options.useLinkReferenceDefinitions = originalUseInlineLinksDefinitions;
});
});

0 comments on commit c318667

Please sign in to comment.