-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f94839
commit 9fa3d49
Showing
8 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/plugins/remark/macros/macros/GlossaryDisambiguation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/plugins/remark/macros/macros/GlossaryDisambiguation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
13 changes: 13 additions & 0 deletions
13
src/plugins/remark/macros/macros/__snapshots__/GlossaryDisambiguation.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |