Skip to content

Commit

Permalink
Change to JS again
Browse files Browse the repository at this point in the history
  • Loading branch information
jlvandenhout committed May 25, 2024
1 parent bc00018 commit 5fdab3a
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 351 deletions.
57 changes: 6 additions & 51 deletions src/plugins/docs/index.ts → src/plugins/docs/index.mjs
Original file line number Diff line number Diff line change
@@ -1,56 +1,14 @@
import type { LoadContext, Plugin } from '@docusaurus/types';
import docsPlugin, {
validateOptions as docsValidateOptions,
PropVersionMetadata,
} from '@docusaurus/plugin-content-docs';
import fs from 'fs/promises';
import path from 'path';
import {
Options as DocsOptions,
PluginOptions as DocsPluginOptions,
LoadedContent as DocsLoadedContent,
DocMetadata as DocsDocMetadata,
} from '@docusaurus/plugin-content-docs';
import {
OptionValidationContext as DocsOptionValidationContext,
Validate,
} from '@docusaurus/types';

export type DocMetadata = DocsDocMetadata & {
bannerContent?: string;
};

export type LoadedContent = DocsLoadedContent & {
bannerContent?: string;
};

export type PluginOptions = DocsPluginOptions & {
globalSidebars: string[];
bannerPath?: string;
};

export type Options = Partial<PluginOptions>;

export type OptionValidationContext = {
// Docusaurus does not export their schemas for us to use,
// so we just pass validate function on to the docs plugin validation.
validate: Validate<DocsOptions, DocsPluginOptions>;
options: PluginOptions;
};

declare module '@docusaurus/plugin-content-docs' {
export function validateOptions(
arg: DocsOptionValidationContext<DocsOptions, DocsPluginOptions>,
): DocsPluginOptions;
}
export default async function pluginDocs(
context: LoadContext,
options: PluginOptions,
): Promise<Plugin<LoadedContent>> {
export default async function pluginDocs(context, options) {
// Destructure to separate the Docusaurus docs plugin options
// and initialize the Docusaurus docs plugin to wrap.
const { bannerPath, globalSidebars, ...docsOptions } = options;
const plugin = await docsPlugin(context, docsOptions);
const plugin = await docsPlugin.default(context, docsOptions);

return {
...plugin,
Expand Down Expand Up @@ -92,9 +50,9 @@ export default async function pluginDocs(
const { bannerContent, ...docsContent } = content;
const globalSidebarEntries = [];

const createData = async (name: string, data: string) => {
const createData = async (name, data) => {
// Hook into the `createData` call to extract the sidebars we need.
const versionMetadata = JSON.parse(data) as PropVersionMetadata;
const versionMetadata = JSON.parse(data);
if (versionMetadata.docsSidebars) {
// We can do this, because all `createData` calls are assured
// to resolve before `setGlobalData` is called.
Expand All @@ -117,7 +75,7 @@ export default async function pluginDocs(
);
};

const setGlobalData = (data: object) => {
const setGlobalData = (data) => {
actions.setGlobalData({
...data,
globalSidebars: Object.fromEntries(globalSidebarEntries),
Expand All @@ -137,10 +95,7 @@ export default async function pluginDocs(
};
}

export function validateOptions({
validate,
options,
}: OptionValidationContext): PluginOptions {
export function validateOptions({ validate, options }) {
const { bannerPath, globalSidebars = [], ...docsOptions } = options;
return {
...docsValidateOptions({ validate, options: docsOptions }),
Expand Down
38 changes: 38 additions & 0 deletions src/plugins/docs/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Options as DocsOptions,
PluginOptions as DocsPluginOptions,
LoadedContent as DocsLoadedContent,
DocMetadata as DocsDocMetadata,
} from '@docusaurus/plugin-content-docs';
import {
OptionValidationContext as DocsOptionValidationContext,
Validate,
} from '@docusaurus/types';

export type DocMetadata = DocsDocMetadata & {
bannerContent?: string;
};

export type LoadedContent = DocsLoadedContent & {
bannerContent?: string;
};

export type PluginOptions = DocsPluginOptions & {
globalSidebars: string[];
bannerPath?: string;
};

export type Options = Partial<PluginOptions>;

export type OptionValidationContext = {
// Docusaurus does not export their schemas for us to use,
// so we just pass validate function on to the docs plugin validation.
validate: Validate<DocsOptions, DocsPluginOptions>;
options: PluginOptions;
};

declare module '@docusaurus/plugin-content-docs' {
export function validateOptions(
arg: DocsOptionValidationContext<DocsOptions, DocsPluginOptions>,
): DocsPluginOptions;
}
19 changes: 19 additions & 0 deletions src/plugins/tutorial/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function pluginTutorial(options) {
return {
name: '@iota-wiki/plugin-tutorial',
async contentLoaded({ actions }) {
actions.setGlobalData(options);
},
};
}

export function validateOptions({ options: userOptions }) {
const id = userOptions.title.normalize().toLowerCase().replace(/\W/, '-');

const defaultOptions = {
id,
route: id,
};

return Object.assign(defaultOptions, userOptions);
}
45 changes: 0 additions & 45 deletions src/plugins/tutorial/index.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/plugins/tutorial/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface Options {
title: string;
description: string;
tags: string[];
source?: string;
preview?: string;
}

export interface DefaultOptions {
id: string;
route: string;
}

export type UserOptions = Options & Partial<DefaultOptions>;
export type NormalizedOptions = Options & DefaultOptions;
5 changes: 3 additions & 2 deletions src/theme/DocBanner/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';
import { useDoc } from '@docusaurus/theme-common/internal';
import { DocMetadata } from '../../plugins/docs';
import ReactMarkdown from 'react-markdown';
import type { DocMetadata } from '../../plugins/docs/types.d.ts';
import './styles.css';

export default function DocBanner() {
const ReactMarkdown = require('react-markdown');

const { bannerContent } = useDoc().metadata as DocMetadata;

if (!bannerContent) return null;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const path = require('path');
const defaultSettings = require('../common/defaultContentPlugin');
const fs = require('fs');
const pluginDocs = require('../plugins/docs');

/**
* Merges multiple configuration objects into one object.
Expand Down Expand Up @@ -113,6 +112,7 @@ async function globStatic(pattern, cwd = __dirname) {
* @param {import('@docusaurus/plugin-content-docs').Options} options
*/
async function create_doc_plugin({ ...options }) {
const pluginDocs = (await import('../plugins/docs/index.mjs')).default;
const setting = await defaultSettings();

// Check if options has a rehypePlugin array
Expand Down
2 changes: 2 additions & 0 deletions src/utils/pluginConfigGenerators.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function findMainVersion(plugin, badge = MAIN_BADGE) {
*/
function generatePluginConfig(pluginConfig, basePath) {
return pluginConfig.flatMap((doc) => {
const mainVersion = findMainVersion(doc);

return doc.versions.map((version) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { label, badges, ...rest } = version;
Expand Down
Loading

0 comments on commit 5fdab3a

Please sign in to comment.