Skip to content

Commit 5e6b00e

Browse files
author
Artyom Podymov
committed
use custom rehypeExternalLinks plugin
1 parent 767b194 commit 5e6b00e

File tree

4 files changed

+16
-22
lines changed

4 files changed

+16
-22
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
"unified": "<rootDir>/redisinsight/__mocks__/unified.js",
8080
"remark-parse": "<rootDir>/redisinsight/__mocks__/remarkParse.js",
8181
"remark-rehype": "<rootDir>/redisinsight/__mocks__/remarkRehype.js",
82-
"rehype-external-links": "<rootDir>/redisinsight/__mocks__/rehypeExternalLinks.js",
8382
"rehype-stringify": "<rootDir>/redisinsight/__mocks__/rehypeStringify.js",
8483
"unist-util-visit": "<rootDir>/redisinsight/__mocks__/unistUtilsVisit.js"
8584
},
@@ -252,7 +251,6 @@
252251
"react-virtualized": "^9.22.2",
253252
"remark-parse": "^10.0.1",
254253
"remark-rehype": "^10.0.1",
255-
"rehype-external-links": "^1.0.0",
256254
"rehype-stringify": "^9.0.2",
257255
"unified": "^10.1.1",
258256
"unist-util-visit": "^4.1.0"

redisinsight/__mocks__/rehypeExternalLinks.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

redisinsight/ui/src/pages/workbench/components/enablament-area/EnablementArea/utils/formatter/MarkdownToJsxString.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { unified } from 'unified'
22
import remarkParse from 'remark-parse'
33
import remarkRehype from 'remark-rehype'
4-
import rehypeExternalLinks from 'rehype-external-links'
54
import rehypeStringify from 'rehype-stringify'
65
import { visit } from 'unist-util-visit'
6+
import { IS_ABSOLUTE_PATH } from 'uiSrc/constants/regex'
77

88
import { IFormatter } from './formatter.interfaces'
99

@@ -14,7 +14,7 @@ class MarkdownToJsxString implements IFormatter {
1414
.use(remarkParse)
1515
.use(MarkdownToJsxString.remarkRedisCode)
1616
.use(remarkRehype, { allowDangerousHtml: true }) // Pass raw HTML strings through.
17-
.use(rehypeExternalLinks, { target: '_blank', rel: ['nofollow'] }) // Set External links
17+
.use(MarkdownToJsxString.rehypeExternalLinks) // Set External links
1818
.use(rehypeStringify, { allowDangerousHtml: true }) // Serialize the raw HTML strings
1919
.process(data)
2020
.then((file) => {
@@ -38,6 +38,20 @@ class MarkdownToJsxString implements IFormatter {
3838
})
3939
}
4040
}
41+
42+
private static rehypeExternalLinks(): (tree: Node) => void {
43+
return (tree: any) => {
44+
visit(tree, 'element', (node) => {
45+
if (node.tagName === 'a' && node.properties && typeof node.properties.href === 'string') {
46+
const url = node.properties.href
47+
if (IS_ABSOLUTE_PATH.test(url)) {
48+
node.properties.rel = ['nofollow', 'noopener', 'noreferrer']
49+
node.properties.target = '_blank'
50+
}
51+
}
52+
})
53+
}
54+
}
4155
}
4256

4357
export default MarkdownToJsxString

yarn.lock

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7957,11 +7957,6 @@ is-absolute-url@^3.0.3:
79577957
resolved "https://registry.yarnpkg.com/is-absolute-url/-/is-absolute-url-3.0.3.tgz#96c6a22b6a23929b11ea0afb1836c36ad4a5d698"
79587958
integrity sha512-opmNIX7uFnS96NtPmhWQgQx6/NYFgsUXYMllcfzwWKUMwfo8kku1TvE6hkNcH+Q1ts5cMVrsY7j0bxXQDciu9Q==
79597959

7960-
is-absolute-url@^4.0.0:
7961-
version "4.0.1"
7962-
resolved "https://registry.yarnpkg.com/is-absolute-url/-/is-absolute-url-4.0.1.tgz#16e4d487d4fded05cfe0685e53ec86804a5e94dc"
7963-
integrity sha512-/51/TKE88Lmm7Gc4/8btclNXWS+g50wXhYJq8HWIBAGUBnoAdRu1aXeh364t/O7wXDAcTJDP8PNuNKWUDWie+A==
7964-
79657960
is-accessor-descriptor@^0.1.6:
79667961
version "0.1.6"
79677962
resolved "https://registry.yarnpkg.com/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz#a9e12cb3ae8d876727eeef3843f8a0897b5c98d6"
@@ -12337,18 +12332,6 @@ regjsparser@^0.6.4:
1233712332
dependencies:
1233812333
jsesc "~0.5.0"
1233912334

12340-
rehype-external-links@^1.0.0:
12341-
version "1.0.0"
12342-
resolved "https://registry.yarnpkg.com/rehype-external-links/-/rehype-external-links-1.0.0.tgz#f8ade4e91c20123a3ce71b7d6cf80e2c2920eb9d"
12343-
integrity sha512-IHXDsz2ZSseOc2TYNqH9zWOfCeb0UAwdwMuGvQCvoxMyUNU7IC1KZ94zllAqaV1O1rnXPt8nLQWtyMNPZ0HEkw==
12344-
dependencies:
12345-
"@types/hast" "^2.0.0"
12346-
extend "^3.0.0"
12347-
is-absolute-url "^4.0.0"
12348-
space-separated-tokens "^2.0.0"
12349-
unified "^10.0.0"
12350-
unist-util-visit "^4.0.0"
12351-
1235212335
rehype-raw@^5.0.0:
1235312336
version "5.1.0"
1235412337
resolved "https://registry.yarnpkg.com/rehype-raw/-/rehype-raw-5.1.0.tgz#66d5e8d7188ada2d31bc137bc19a1000cf2c6b7e"

0 commit comments

Comments
 (0)