Skip to content

Commit

Permalink
feat: macro glossarysidebar (#77)
Browse files Browse the repository at this point in the history
* feat: macro glossarysidebar

* fix: glossarysidebar test

---------

Co-authored-by: Vitalii Perehonchuk <[email protected]>
Co-authored-by: Mykola Myslovskyi <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2024
1 parent b60400a commit c793b47
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/kuma/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import embedInteractiveExample from './macros/embed-interactive-example';
import embedLiveSample from './macros/embed-live-sample';
import experimentalInline from './macros/experimental-inline';
import glossary from './macros/glossary';
import GlossarySidebar from './macros/GlossarySidebar';
import GlossaryDisambiguation from './macros/GlossaryDisambiguation';
import htmlElement from './macros/html-element';
import HttpHeader from './macros/http-header';
Expand Down Expand Up @@ -114,6 +115,7 @@ export function macros(context) {
next,
previous,
previousnext: previousNext,
glossarysidebar: GlossarySidebar,
glossarydisambiguation: GlossaryDisambiguation,
svgattr: SVGAttr,
livesamplelink: LiveSampleLink,
Expand Down
34 changes: 34 additions & 0 deletions lib/kuma/macros/GlossarySidebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const pageToNavItem = ({ path, slug, title }, currentSlug) => ({
title,
path,
isCurrent: slug === currentSlug,
});

export default function GlossarySidebar() {
const {
env: { slug, targetLocale },
registry,
} = this;
const glossaryPages = registry.getChildren('Glossary', false);
const translatedGlossaryPages = glossaryPages.filter(
(item) => item.hasLocalizedContent,
);
function sortByTitle({ title: titleA = '' }, { title: titleB = '' }) {
return titleA.localeCompare(titleB, targetLocale);
}
// TODO: we definitely need it typed at some point
return [
{
title: 'Глосарій',
sections: [
{
expanded: true,
items: translatedGlossaryPages
.map((item) => pageToNavItem(item, slug))
.sort(sortByTitle),
title: 'Терміни',
},
],
},
];
}
167 changes: 167 additions & 0 deletions test/src/kuma/macros/GlossarySidebar.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import test from 'ava';

import { macros } from '../../../../lib/kuma';

test("Macros 'GlossarySidebar' should be present", (t) => {
t.truthy(macros({}).lookup('GlossarySidebar'));

t.timeout(200);
});

test("Macros 'GlossarySidebar' should generate partial navigation for glossary", (t) => {
const GlossarySidebar = macros({
env: {
slug: 'Glossary/Truthy',
},
registry: {
getChildren: () => [
{
hasLocalizedContent: true,
title: 'Хибні значення',
path: '/en-US/docs/Glossary/Falsy',
slug: 'Glossary/Falsy',
},
{
hasLocalizedContent: true,
title: 'Істинні значення',
path: '/en-US/docs/Glossary/Truthy',
slug: 'Glossary/Truthy',
},
{
title: 'Undefined',
path: '/en-US/docs/Glossary/Undefined',
slug: 'Glossary/Undefined',
},
{
title: 'Null',
path: '/en-US/docs/Glossary/Null',
slug: 'Glossary/Null',
},
{
title: 'NaN',
path: '/en-US/docs/Glossary/NaN',
slug: 'Glossary/NaN',
},
{
title: 'Primitive',
path: '/en-US/docs/Glossary/Primitive',
slug: 'Glossary/Primitive',
},
{
title: 'Object',
path: '/en-US/docs/Glossary/Object',
slug: 'Glossary/Object',
},
{
title: 'Array',
path: '/en-US/docs/Glossary/Array',
slug: 'Glossary/Array',
},
{
title: 'Function',
path: '/en-US/docs/Glossary/Function',
slug: 'Glossary/Function',
},
{
title: 'Operator',
path: '/en-US/docs/Glossary/Operator',
slug: 'Glossary/Operator',
},
{
title: 'Statement',
path: '/en-US/docs/Glossary/Statement',
slug: 'Glossary/Statement',
},
{
title: 'Expression',
path: '/en-US/docs/Glossary/Expression',
slug: 'Glossary/Expression',
},
{
title: 'Keyword',
path: '/en-US/docs/Glossary/Keyword',
slug: 'Glossary/Keyword',
},
{
title: 'Property',
path: '/en-US/docs/Glossary/Property',
slug: 'Glossary/Property',
},
{
title: 'Event',
path: '/en-US/docs/Glossary/Event',
slug: 'Glossary/Event',
},
{
title: 'Method',
path: '/en-US/docs/Glossary/Method',
slug: 'Glossary/Method',
},
{
title: 'Interface',
path: '/en-US/docs/Glossary/Interface',
slug: 'Glossary/Interface',
},
{
title: 'Array.prototype.map()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/map',
},
{
title: 'Array.prototype.filter()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/filter',
},
{
title: 'Array.prototype.reduce()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/reduce',
},
{
title: 'Array.prototype.forEach()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/forEach',
},
{
title: 'Array.prototype.every()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/every',
},
{
title: 'Array.prototype.some()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/some',
},
{
title: 'Array.prototype.indexOf()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/indexOf',
},
{
title: 'Array.prototype.lastIndexOf()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf',
},
{
title: 'Array.prototype.includes()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/includes',
},
{
title: 'Array.prototype.find()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/find',
},
{
title: 'Array.prototype.findIndex()',
path: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex',
slug: 'Web/JavaScript/Reference/Global_Objects/Array/findIndex',
},
],
},
}).lookup('GlossarySidebar');

t.snapshot(GlossarySidebar());

t.timeout(200);
});
33 changes: 33 additions & 0 deletions test/src/kuma/macros/snapshots/GlossarySidebar.mjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Snapshot report for `test/src/kuma/macros/GlossarySidebar.mjs`

The actual snapshot is saved in `GlossarySidebar.mjs.snap`.

Generated by [AVA](https://avajs.dev).

## Macros 'GlossarySidebar' should generate partial navigation for glossary

> Snapshot 1
[
{
sections: [
{
expanded: true,
items: [
{
isCurrent: true,
path: '/en-US/docs/Glossary/Truthy',
title: 'Істинні значення',
},
{
isCurrent: false,
path: '/en-US/docs/Glossary/Falsy',
title: 'Хибні значення',
},
],
title: 'Терміни',
},
],
title: 'Глосарій',
},
]
Binary file not shown.

0 comments on commit c793b47

Please sign in to comment.