diff --git a/package.json b/package.json index 129342b..0e1351a 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@types/unist": "^2.0.6", "@webdoky/yari-ports": "^3.5.0", "acorn-import-assertions": "^1.8.0", + "classnames": "~2.3.2", "cyrillic-to-translit-js": "^3.2.1", "github-slugger": "^1.4.0", "gray-matter": "^4.0.3", diff --git a/src/registry/utils/is-url-ukrainian.ts b/src/registry/utils/is-url-ukrainian.ts new file mode 100644 index 0000000..353c937 --- /dev/null +++ b/src/registry/utils/is-url-ukrainian.ts @@ -0,0 +1,9 @@ +const UKRAINIAN_URL_REGEXPS = [ + /^https?:\/\/uk\.javascript\.info/, + /^https?:\/\/uk\.wikipedia\.org/, + /^https?:\/\/\w+\.google\.com\/.+\?hl=uk/, +]; + +export default function isUrlUkrainian(url: string): boolean { + return UKRAINIAN_URL_REGEXPS.some((regexp) => regexp.test(url)); +} diff --git a/src/registry/utils/plugins/external-links.ts b/src/registry/utils/plugins/external-links.ts index 4b564a1..caa2f2e 100644 --- a/src/registry/utils/plugins/external-links.ts +++ b/src/registry/utils/plugins/external-links.ts @@ -1,8 +1,11 @@ +import classNames from 'classnames'; import { visit } from 'unist-util-visit'; import { parse } from 'space-separated-tokens'; import isAbsoluteUrl from 'is-absolute-url'; import { extend } from 'lodash-es'; +import isUrlUkrainian from '../is-url-ukrainian'; + const defaultTarget = '_blank'; const defaultRel = ['nofollow', 'noopener', 'noreferrer']; const defaultProtocols = ['http', 'https']; @@ -47,12 +50,16 @@ const externalLinks = (options: Options = {}) => { node.properties.target = target || defaultTarget; } - if (className) { - node.properties.className = - (node.properties.className - ? `${node.properties.className} ` - : '') + className; - } + const isUkrainian = isUrlUkrainian(url); + + node.properties.className = classNames( + node.properties.className, + className, + { + 'wd-link__foreign': !isUkrainian, + 'wd-link__ukrainian': isUkrainian, + }, + ); if (rel !== false) { node.properties.rel = (rel || defaultRel).toString().concat(); diff --git a/yarn.lock b/yarn.lock index 42dc9be..2532153 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1981,6 +1981,11 @@ cidr-regex@^3.1.1: dependencies: ip-regex "^4.1.0" +classnames@~2.3.2: + version "2.3.2" + resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.3.2.tgz#351d813bf0137fcc6a76a16b88208d2560a0d924" + integrity sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw== + clean-stack@^2.0.0: version "2.2.0" resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-2.2.0.tgz#ee8472dbb129e727b31e8a10a427dee9dfe4008b"