Skip to content

Commit

Permalink
glossary macro
Browse files Browse the repository at this point in the history
  • Loading branch information
viperehonchuk committed Feb 20, 2024
1 parent 1fd5669 commit bd4d1ae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/plugins/rehype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { injectHeadingSlugsPlugin } from './inject-heading-slugs.ts';
import { injectLinkClassesPlugin } from './inject-link-classes.ts';

const rehypePlugins: RehypePlugins = [
injectLinkClassesPlugin,
injectHeadingSlugsPlugin,
[rehypeAutolinkHeadings, { behavior: 'append' }],
rehypeRaw,
injectLinkClassesPlugin,
injectDtIdsPlugin,
checkReferencedAnchorsPlugin,
];
Expand Down
37 changes: 37 additions & 0 deletions src/plugins/remark/macros/macros/glossary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Html } from 'mdast';
import { SKIP } from 'unist-util-visit';

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

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

function macro(node: MacroNode): Html {
const targetLocale = process.env.TARGET_LOCALE;
let arguments_ = parseArguments(node.parameters);
let string_ = arguments_[1] || arguments_[0];

const basePath = `/${targetLocale}/docs/Glossary/`;
const subPath = arguments_[0].replaceAll(/\s+/g, '_');

return {
type: 'html',
value: `<a href="${basePath + subPath}">${string_}</a>`,
};
}

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

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

Expand All @@ -16,6 +17,7 @@ const MACROS: Record<string, MacroFunction> = {
HTMLElement,
cssxref,
domxref,
glossary,
jsSidebar,
jsxref,
};
Expand Down

0 comments on commit bd4d1ae

Please sign in to comment.