Skip to content

Commit

Permalink
deprecated_inline
Browse files Browse the repository at this point in the history
  • Loading branch information
viperehonchuk committed Feb 20, 2024
1 parent 95939a4 commit 0a42ad0
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 0 deletions.
5 changes: 5 additions & 0 deletions assets/deprecated.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/plugins/remark/macros/get-json-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

const registry = new Map<string, unknown>();

export default function getJsonData(name: string) {
if (registry.has(name)) {
return registry.get(name);
}
const PATH_TO_LOCALIZED_CONTENT = process.env.PATH_TO_LOCALIZED_CONTENT;
if (!PATH_TO_LOCALIZED_CONTENT) {
throw new Error('PATH_TO_LOCALIZED_CONTENT is not defined');
}
const targetLocale = process.env.TARGET_LOCALE;
if (!targetLocale) {
throw new Error('TARGET_LOCALE is not defined');
}
const json = readFileSync(
join(PATH_TO_LOCALIZED_CONTENT, 'files', 'jsondata', `${name}.json`),
'utf8',
);
const data = JSON.parse(json);
registry.set(name, data);
return data;
}
13 changes: 13 additions & 0 deletions src/plugins/remark/macros/localize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from 'zod';

import getJsonData from './get-json-data.ts';
export default function localize(packName: string, key: string) {
const targetLocale = process.env.TARGET_LOCALE;
if (!targetLocale) {
throw new Error('TARGET_LOCALE is not defined');
}
const rawPack = getJsonData(packName);
const pack = localizationPackSchema.parse(rawPack);
return pack[key]![targetLocale];
}
const localizationPackSchema = z.record(z.record(z.string().min(1)));
49 changes: 49 additions & 0 deletions src/plugins/remark/macros/macros/RFC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { Html } from 'mdast';
import { SKIP } from 'unist-util-visit';

import localize from '../localize.ts';
import type { MacroFunction, MacroNode } from '../types.ts';
import { wrappedNumberSchema, wrappedStringSchema } from '../validation.ts';

function parseArguments(
value: string[],
): [number, string | undefined, string | undefined] {
if (value.length === 0 || !value[0]) {
throw new Error('No arguments provided');
}
return [
wrappedNumberSchema.parse(value[0]),
value[1] ? wrappedStringSchema.parse(value[1]) : undefined,
value[2] ? wrappedStringSchema.parse(value[2]) : undefined,
];
}

function macro(node: MacroNode): Html {
let arguments_ = parseArguments(node.parameters);

let link = 'https://datatracker.ietf.org/doc/html/rfc' + arguments_[0];
let text = '';

if (arguments_[1]) {
text = ': ' + arguments_[1];
}

if (arguments_[2]) {
link = link + '#section-' + arguments_[2];
text =
', ' + localize('L10n-Common', 'section') + ' ' + arguments_[2] + text;
}

return {
type: 'html',
value: `<a href="${link}">RFC ${text}</a>`,
};
}

const RFC: MacroFunction = (node, index, parent) => {
const replacement = macro(node);
parent.children[index] = replacement;
return [SKIP, index];
};

export default RFC;
20 changes: 20 additions & 0 deletions src/plugins/remark/macros/macros/deprecated_inline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SKIP } from 'unist-util-visit';

import type { MacroFunction } from '../types.ts';

function macro() {
return {
type: 'html',
value: `<abbr class="status-icon deprecated" title="Нерекомендоване. Не для використання в нових вебсайтах">
<span class="visually-hidden">Нерекомендоване</span>
</abbr>`,
};
}

const deprecated_inline: MacroFunction = (_node, index, parent) => {
const replacement = macro();
parent.children[index] = replacement;
return [SKIP, index];
};

export default deprecated_inline;
4 changes: 4 additions & 0 deletions src/plugins/remark/macros/macros/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import GlossarySidebar from './GlossarySidebar.ts';
import HTMLElement from './HTMLElement.ts';
import HTTPHeader from './HTTPHeader.ts';
import HTTPMethod from './HTTPMethod.ts';
import RFC from './RFC.ts';
import cssxref from './cssxref.ts';
import deprecated_inline from './deprecated_inline.ts';
import domxref from './domxref.ts';
import glossary from './glossary.ts';
import jsSidebar from './jsSidebar/index.ts';
import jsxref from './jsxref.ts';

const MACROS: Record<string, MacroFunction> = {
cssxref,
deprecated_inline,
domxref,
embedlivesample: EmbedLiveSample,
glossary,
Expand All @@ -24,6 +27,7 @@ const MACROS: Record<string, MacroFunction> = {
httpmethod: HTTPMethod,
jssidebar: jsSidebar,
jsxref,
rfc: RFC,
};

export default MACROS;
31 changes: 31 additions & 0 deletions src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,34 @@ blockquote {
transform: rotate(359deg);
}
}

.visually-hidden {
border: 0 !important;
clip: rect(1px, 1px, 1px, 1px) !important;
-webkit-clip-path: inset(50%) !important;
clip-path: inset(50%) !important;
height: 1px !important;
margin: -1px !important;
overflow: hidden !important;
padding: 0 !important;
position: absolute !important;
white-space: nowrap !important;
width: 1px !important;
}

.status-icon {
--size: var(--icon-size, 1rem);

background-color: var(--color-ui-typo);
display: inline-block;
flex-shrink: 0;
height: var(--size);
mask-position: center;
mask-repeat: no-repeat;
vertical-align: middle;
width: var(--size);

&.deprecated {
mask-image: url('../assets/deprecated.svg');
}
}

0 comments on commit 0a42ad0

Please sign in to comment.