From 9359a21020ed9bdae055db3470a25283d124e5bb Mon Sep 17 00:00:00 2001 From: Vitalii Perehonchuk Date: Sat, 11 Mar 2023 07:56:26 +0200 Subject: [PATCH 1/2] feat: mark ukrainian links differently --- package.json | 1 + src/registry/utils/is-url-ukrainian.ts | 9 +++++++++ src/registry/utils/plugins/external-links.ts | 20 ++++++++++++++------ yarn.lock | 5 +++++ 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/registry/utils/is-url-ukrainian.ts diff --git a/package.json b/package.json index a0794f9..58a685e 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@rollup/plugin-node-resolve": "^13.3.0", "@webdoky/yari-ports": "^3.1.1", "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 f697d97..c714872 100644 --- a/src/registry/utils/plugins/external-links.ts +++ b/src/registry/utils/plugins/external-links.ts @@ -1,7 +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 lodash from 'lodash'; + +import isUrlUkrainian from '../is-url-ukrainian'; + const { extend } = lodash; const defaultTarget = '_blank'; @@ -48,12 +52,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-foreign': !isUkrainian, + 'wd-ukrainian': isUkrainian, + }, + ); if (rel !== false) { node.properties.rel = (rel || defaultRel).toString().concat(); diff --git a/yarn.lock b/yarn.lock index e0b4232..7b4cc1a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1969,6 +1969,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" From 5a4f9e2a856f18e9605919e8de4f864ceabfd722 Mon Sep 17 00:00:00 2001 From: Vitalii Perehonchuk Date: Sat, 11 Mar 2023 12:11:59 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Mykola Myslovskyi --- src/registry/utils/plugins/external-links.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/registry/utils/plugins/external-links.ts b/src/registry/utils/plugins/external-links.ts index ea2e191..caa2f2e 100644 --- a/src/registry/utils/plugins/external-links.ts +++ b/src/registry/utils/plugins/external-links.ts @@ -56,8 +56,8 @@ const externalLinks = (options: Options = {}) => { node.properties.className, className, { - 'wd-foreign': !isUkrainian, - 'wd-ukrainian': isUkrainian, + 'wd-link__foreign': !isUkrainian, + 'wd-link__ukrainian': isUkrainian, }, );