Skip to content

Commit

Permalink
GlossaryDisambiguation macro
Browse files Browse the repository at this point in the history
  • Loading branch information
viperehonchuk committed Feb 9, 2024
1 parent 3f94839 commit 9fa3d49
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/plugins/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export async function initRegistry() {
// console.log(markdownFile);
matter(markdownFile);
// console.log(markdownFile.data.matter);
const data = rawPageSchema.parse(markdownFile.data.matter);
const data = {
...rawPageSchema.parse(markdownFile.data.matter),
filePath: filePath,
};
registry.set(data.slug, data);
},
);
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/registry/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export const rawPageSchema = z.object({
title: z.string().min(1),
});

export type RawPage = z.infer<typeof rawPageSchema>;
export interface RawPage extends z.infer<typeof rawPageSchema> {
filePath: string;
}
2 changes: 0 additions & 2 deletions src/plugins/remark/macros/create-macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ export default function createMacro(
}
return applyMacro;
}

type M = ReturnType<ReturnType<typeof createMacro>>;
4 changes: 3 additions & 1 deletion src/plugins/remark/macros/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { type AstroFile } from '../validate-astro-file.ts';
import brokenMacroToHtml from './broken-macro-to-html.js';
import processHtml from './in-html/process.js';
import macroToHtml from './macro-to-html.js';
import GlossaryDisambiguation from './macros/GlossaryDisambiguation.ts';
import GlossarySidebar from './macros/GlossarySidebar.ts';
import jsSidebar from './macros/jsSidebar/index.ts';
import makeMacroTree from './make-macro-tree.js';
Expand All @@ -19,8 +20,9 @@ export default async function expandMacros(tree: Root, file: AstroFile) {
});
makeMacroTree(tree);

jsSidebar(tree, file);
GlossaryDisambiguation(tree, file);
GlossarySidebar(tree, file);
jsSidebar(tree, file);

unmakeMacroTree(tree, macroToHtml, brokenMacroToHtml);
}
82 changes: 82 additions & 0 deletions src/plugins/remark/macros/macros/GlossaryDisambiguation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { Root } from 'mdast';
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';

import GlossaryDisambiguation from './GlossaryDisambiguation.ts';

vi.mock('../../../utils/read-basic-markdown.ts', () => ({
default: vi.fn().mockReturnValue({
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: '{{SomeMacro}}',
},
],
},
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'Це опис',
},
],
},
],
}),
}));

vi.mock('../../../registry/get-children.ts', () => ({
default: vi.fn().mockReturnValue([
{
filePath: 'src/pages/uk/docs/Glossary/Baseline/Typography.md',
slug: 'Glossary/Baseline/Typography',
title: 'Базова лінія',
},
{
filePath: 'src/pages/uk/docs/Glossary/Baseline/Compatibility.md',
slug: 'Glossary/Baseline/Compatibility',
title: 'База',
},
]),
}));

describe('GlossaryDisambiguation', () => {
const OLD_ENV = process.env;

beforeEach(() => {
vi.resetModules(); // Most important - it clears the cache
process.env = {
TARGET_LOCALE: 'uk',
};
});

afterAll(() => {
process.env = OLD_ENV; // Restore old environment
});
it('should replace the node with an HTML element', async () => {
const tree = {
type: 'root',
children: [
{
type: 'macro',
name: 'GlossaryDisambiguation',
parameters: [],
},
],
};
const file = {
data: {
astro: {
frontmatter: {
slug: 'Glossary/Baseline',
},
},
},
} as any;
GlossaryDisambiguation(tree as Root, file);
expect(tree).toMatchSnapshot();
});
});
49 changes: 49 additions & 0 deletions src/plugins/remark/macros/macros/GlossaryDisambiguation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { Html, Root } from 'mdast';
import { toString } from 'mdast-util-to-string';
import { SKIP } from 'unist-util-visit';

import getChildren from '../../../registry/get-children.ts';
import readBasicMarkdown from '../../../utils/read-basic-markdown.ts';
import type { AstroFile } from '../../validate-astro-file.ts';
import createMacro from '../create-macro.ts';

function macro(_tree: Root, file: AstroFile): Html {
const currentSlug = file.data.astro.frontmatter.slug;
const targetLocale = process.env.TARGET_LOCALE;
const children = getChildren(currentSlug);
children.sort((a, b) => a.title.localeCompare(b.title, targetLocale));
const items = children.map((child) => {
const tree = readBasicMarkdown(child.filePath);
const descriptionNode = tree.children.find((node) => {
if (node.type !== 'paragraph') {
return false;
}
const text = toString(node);
return !text.startsWith('{{');
});
if (!descriptionNode) {
throw new Error('No description found');
}
const description = toString(descriptionNode);
return [child.slug, child.title, description];
});
const definitionsHtmlItems = items.map(
([slug, title, description]) =>
`<dt><a href="/${targetLocale}/docs/${slug}/">${title}</a></dt><dd>${description}</dd>`,
);
return {
type: 'html',
value: `<dl>${definitionsHtmlItems.join('')}</dl>`,
};
}

const GlossaryDisambiguation = createMacro(
'GlossaryDisambiguation',
(_node, index, parent, tree, file) => {
const replacement = macro(tree, file);
parent.children[index] = replacement;
return [SKIP, index];
},
);

export default GlossaryDisambiguation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`GlossaryDisambiguation > should replace the node with an HTML element 1`] = `
{
"children": [
{
"type": "html",
"value": "<dl><dt><a href="/uk/docs/Glossary/Baseline/Compatibility/">База</a></dt><dd>Це опис</dd><dt><a href="/uk/docs/Glossary/Baseline/Typography/">Базова лінія</a></dt><dd>Це опис</dd></dl>",
},
],
"type": "root",
}
`;
10 changes: 10 additions & 0 deletions src/plugins/utils/read-basic-markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import remarkParse from 'remark-parse';
import { readSync } from 'to-vfile';
import { unified } from 'unified';

const basicProcessor = unified().use(remarkParse);

export default function readBasicMarkdown(filePath: string) {
const sourceFile = readSync(filePath);
return basicProcessor.parse(sourceFile);
}

0 comments on commit 9fa3d49

Please sign in to comment.