From 951fd7fbb8f6926dd3cd8256da927f00b1786b36 Mon Sep 17 00:00:00 2001 From: postamentovich Date: Tue, 1 Nov 2022 20:18:42 +0600 Subject: [PATCH] feat: add linkifyTlds option --- src/transform/index.ts | 6 ++++++ test/linkify.test.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 test/linkify.test.ts diff --git a/src/transform/index.ts b/src/transform/index.ts index b6fe6dbb..23473f54 100644 --- a/src/transform/index.ts +++ b/src/transform/index.ts @@ -44,6 +44,7 @@ interface OptionsType { needTitle?: boolean; allowHTML?: boolean; linkify?: boolean; + linkifyTlds?: string | string[]; breaks?: boolean; conditionsInCode?: boolean; disableLiquid?: boolean; @@ -68,6 +69,7 @@ function transform(originInput: string, opts: OptionsType = {}): OutputType { needTitle, allowHTML = false, linkify = false, + linkifyTlds, breaks = true, conditionsInCode = false, needToSanitizeHtml = false, @@ -118,6 +120,10 @@ function transform(originInput: string, opts: OptionsType = {}): OutputType { plugins.forEach((plugin) => md.use(plugin, pluginOptions)); + if (linkify && linkifyTlds) { + md.linkify.tlds(linkifyTlds, true); + } + try { let title; let tokens; diff --git a/test/linkify.test.ts b/test/linkify.test.ts new file mode 100644 index 00000000..26e2b41a --- /dev/null +++ b/test/linkify.test.ts @@ -0,0 +1,18 @@ +import transform from '../src/transform'; + +describe('Linkify', () => { + it('should not linkify .cloud tld without linkifyTlds option', () => { + const { + result: {html}, + } = transform('yandex.cloud'); + + expect(html).toBe('

yandex.cloud

\n'); + }); + it('should linkify .cloud tld with linkifyTlds option', () => { + const { + result: {html}, + } = transform('yandex.cloud', {linkifyTlds: 'cloud', linkify: true}); + + expect(html).toBe('

yandex.cloud

\n'); + }); +});