Files
xrpl-dev-portal/content/@theme/plugins/index-pages.js
mDuo13 817ea3732f Markdown syntax migration script
Script replacing include_code with code-snippet

Migration script: partials, some variables

Add variables to conversion script

draft repo-link component

Complete repo-link component

Migration script: handle github links

Draft include_svg→inline-svg (non-functional)

Currently doesn't work due to image path issues.
Also, captions and custom classes (for inlining) not implemented yet.

Conversion script: refactor & add code-page-name

Custom code-page-name component works around Markdoc limitation where
vars can't be used in `inline code` sections.

Migrate script: Handle more code includes correctly

Migration script: tabs and tabbed code samples

Child pages macro & conversion script

Adapted from 70cffa67ed

Migration script: update with some partial fixes

Migration script: callouts→admonitions

Fix auto-generation of index pages

Migration script: fix SVG migration

Migration scripting: fix code block prefixes & indentation

- Use the Redocly 0.66 feature for code block prefixes
- Update the script for converting indented code blocks to fences with
  Roman's latest fixes (now uses 4 spaces per level, for consistency)
2024-01-31 16:07:14 -08:00

64 lines
2.2 KiB
JavaScript

// @ts-check
import { readSharedData } from '@redocly/portal/dist/server/utils/shared-data.js'; // TODO: export function from root package
const INDEX_PAGE_INFO_DATA_KEY = 'index-page-items';
export function indexPages() {
/** @type {import("@redocly/portal/dist/server/plugins/types").PluginInstance } */
const instance = {
// hook that gets executed after all routes were created
async afterRoutesCreated(contentProvider, actions) {
// get all the routes that are ind pages
const indexRoutes = actions.getAllRoutes().filter(route => route.metadata?.indexPage);
for (const route of indexRoutes) {
// @ts-ignore this uses some internals, we will expose them in nicer way in the future releases
const sidebarId = actions.routesSharedData.get(route.slug)?.['sidebar']; // TODO: implement a helper function for this
/** @type {any} */
const sidebar = await readSharedData(sidebarId, actions.outdir);
if (!sidebar) {
console.log('[warn] Index route used with no sidebar', route.fsPath);
continue;
}
const item = findItemDeep(sidebar.items, route.fsPath);
const childrenPaths = (item.items || []).map(item => item.fsPath).filter(Boolean);
const childRoutes = childrenPaths.map(fsPath => actions.getRouteByFsPath(fsPath));
const childRoutesData = await Promise.all(
childRoutes.map(async route => {
const { parsed } = contentProvider.loadContent(route.fsPath, 'frontmatter');
return {
...parsed?.data,
slug: route.slug,
title: await route.getNavText(),
};
})
);
const sharedDataId = await actions.createSharedData(
route.slug + '_' + INDEX_PAGE_INFO_DATA_KEY,
childRoutesData
);
actions.addRouteSharedData(route.slug, INDEX_PAGE_INFO_DATA_KEY, sharedDataId);
}
},
};
return instance;
}
function findItemDeep(items, fsPath) {
for (const item of items) {
if (item.fsPath === fsPath) {
return item;
}
if (item.items) {
const found = findItemDeep(item.items, fsPath);
if (found) {
return found;
}
}
}
}