Remove old scripts

This commit is contained in:
Lorenzo Lewis
2022-01-16 22:29:46 +00:00
parent 8afad4c0be
commit 51c0228817
9 changed files with 0 additions and 1624 deletions

1047
dist/index.js vendored

File diff suppressed because it is too large Load Diff

View File

@@ -1,132 +0,0 @@
import * as path from 'path';
// @ts-ignore
import { BindOption } from 'typedoc';
// @ts-ignore
import { Component } from 'typedoc/dist/lib/converter/components';
// @ts-ignore
import { RendererComponent } from 'typedoc/dist/lib/output/components';
// @ts-ignore
import { PageEvent } from 'typedoc/dist/lib/output/events';
import { FrontMatter, Sidebar } from './types';
// @ts-ignore
import { reflectionTitle } from 'typedoc-plugin-markdown/dist/resources/helpers/reflection-title';
export interface FrontMatterVars {
[key: string]: string | number | boolean;
}
/**
* Prepends YAML block to a string
* @param contents - the string to prepend
* @param vars - object of required front matter variables
*/
export const prependYAML = (contents: string, vars: FrontMatterVars) => {
return contents
.replace(/^/, toYAML(vars) + '\n\n')
.replace(/[\r\n]{3,}/g, '\n\n');
};
/**
* Returns the page title as rendered in the document h1(# title)
* @param page
*/
export const getPageTitle = (page: PageEvent) => {
return reflectionTitle.call(page, false);
};
/**
* Converts YAML object to a YAML string
* @param vars
*/
const toYAML = (vars: FrontMatterVars) => {
const yaml = `---
${Object.entries(vars)
.map(
([key, value]) =>
`${key}: ${
typeof value === 'string' ? `"${escapeString(value)}"` : value
}`,
)
.join('\n')}
---`;
return yaml;
};
// prettier-ignore
const escapeString=(str: string) => str.replace(/([^\\])'/g, '$1\\\'');
@Component({ name: 'front-matter' })
export class FrontMatterComponent extends RendererComponent {
@BindOption('out')
out!: string;
@BindOption('sidebar')
sidebar!: Sidebar;
@BindOption('globalsTitle')
globalsTitle!: string;
@BindOption('readmeTitle')
readmeTitle!: string;
@BindOption('entryDocument')
entryDocument!: string;
globalsFile = 'modules.md';
initialize() {
super.initialize();
// @ts-ignore
this.listenTo(this.application.renderer, {
[PageEvent.END]: this.onPageEnd,
});
}
onPageEnd(page: PageEvent) {
if (page.contents) {
page.contents = prependYAML(page.contents, this.getYamlItems(page));
}
}
getYamlItems(page: PageEvent): any {
const pageTitle = this.getTitle(page);
const sidebarLabel = this.getSidebarLabel(page);
let items: FrontMatter = {
title: pageTitle,
};
if (sidebarLabel && sidebarLabel !== pageTitle) {
items = { ...items, sidebar_label: sidebarLabel };
}
return {
...items,
custom_edit_url: null,
hide_title: true,
};
}
getSidebarLabel(page: PageEvent) {
if (!this.sidebar) {
return null;
}
if (page.url === this.entryDocument) {
return page.url === page.project.url
? this.sidebar.indexLabel
: this.sidebar.readmeLabel;
}
if (page.url === this.globalsFile) {
return this.sidebar.indexLabel;
}
return this.sidebar.fullNames ? page.model.getFullName() : page.model.name;
}
getId(page: PageEvent) {
return path.basename(page.url, path.extname(page.url));
}
getTitle(page: PageEvent) {
const readmeTitle = this.readmeTitle || page.project.name;
if (page.url === this.entryDocument && page.url !== page.project.url) {
return readmeTitle;
}
return getPageTitle(page);
}
}

View File

@@ -1,54 +0,0 @@
let core = require("@actions/core");
const { rmdir } = require("fs").promises;
const path = require("path");
const overrideRequire = require("override-require");
if (process.env.DEV) {
core = {
getInput: (variable) => process.env[variable],
setFailed: (message) => console.log(message)
};
}
(async () => {
try {
// Where your docs live, should be the folder containing the crates docs
const originPath = core.getInput("originPath"); // e.g. "/path/to/project/src/";
const sidebarFile = core.getInput("sidebarFile");
// Where you'll save your MD files
const targetPath = core.getInput("targetPath"); // e.g. "/path/to/docusaurus/website/docs/api/js/";
const docusaurusPath = core.getInput("docusaurusPath");
const overrideCondition = (request) => request.startsWith("typedoc");
const resolveRequest = (request) =>
require(path.normalize(
process.cwd().replace("/dist/typedocusaurus", "") + `/${originPath}node_modules/${request}`
));
overrideRequire(overrideCondition, resolveRequest);
const { default: generate } = require("./plugin");
await rmdir(targetPath, { recursive: true });
await generate(docusaurusPath, {
entryPoints: originPath + "src",
out: targetPath,
entryDocument: "index.md",
hideInPageTOC: true,
hideBreadcrumbs: true,
watch: false,
tsconfig: originPath + "tsconfig.json",
sidebar: {
sidebarFile,
},
readme: "none",
});
console.log("Tasks completed!");
} catch (error) {
core.setFailed(error.message);
}
})();

View File

@@ -1,120 +0,0 @@
import * as path from 'path';
import {
// @ts-ignore
Application,
// @ts-ignore
MixedDeclarationOption,
// @ts-ignore
ParameterType,
// @ts-ignore
StringDeclarationOption,
// @ts-ignore
TSConfigReader,
// @ts-ignore
TypeDocReader,
// @ts-ignore
} from 'typedoc';
import { PluginOptions, SidebarOptions } from './types';
/**
* Default plugin options
*/
const DEFAULT_PLUGIN_OPTIONS: PluginOptions = {
id: 'default',
docsRoot: 'docs',
out: 'api',
entryDocument: 'index.md',
hideInPageTOC: true,
hideBreadcrumbs: true,
sidebar: {
fullNames: false,
sidebarFile: 'typedoc-sidebar.js',
indexLabel: 'Table of contents',
readmeLabel: 'Readme',
sidebarPath: '',
},
plugin: ['none'],
outputDirectory: '',
siteDir: '',
watch: false,
};
/**
* Merge default with user options
* @param opts
*/
export const getOptions = (
siteDir: string,
opts: Partial<PluginOptions>,
): PluginOptions => {
// base options
let options = {
...DEFAULT_PLUGIN_OPTIONS,
...opts,
};
// sidebar
if (opts.sidebar === null) {
options = { ...options, sidebar: null };
} else {
const sidebar = {
...DEFAULT_PLUGIN_OPTIONS.sidebar,
...opts.sidebar,
} as SidebarOptions;
options = {
...options,
sidebar: {
...sidebar,
sidebarPath: path.resolve(siteDir, sidebar.sidebarFile),
},
};
}
// additional
options = {
...options,
siteDir,
outputDirectory: path.resolve(siteDir, options.docsRoot, options.out),
};
return options;
};
/**
* Add docusaurus options to converter
* @param app
*/
export const addOptions = (app: Application) => {
// configure deault typedoc options
app.options.addReader(new TypeDocReader());
app.options.addReader(new TSConfigReader());
// expose plugin options to typedoc so we can access if required
app.options.addDeclaration({
name: 'id',
} as StringDeclarationOption);
app.options.addDeclaration({
name: 'docsRoot',
} as StringDeclarationOption);
app.options.addDeclaration({
name: 'siteDir',
} as MixedDeclarationOption);
app.options.addDeclaration({
name: 'outputDirectory',
} as StringDeclarationOption);
app.options.addDeclaration({
name: 'globalsTitle',
} as StringDeclarationOption);
app.options.addDeclaration({
name: 'readmeTitle',
} as StringDeclarationOption);
app.options.addDeclaration({
name: 'sidebar',
type: ParameterType.Mixed,
} as MixedDeclarationOption);
};

View File

@@ -1,56 +0,0 @@
// @ts-ignore
import { Application } from 'typedoc';
// @ts-ignore
import { load } from 'typedoc-plugin-markdown';
import { FrontMatterComponent } from './front-matter';
import { addOptions, getOptions } from './options';
import { render } from './render';
import { SidebarComponent } from './sidebar';
import { PluginOptions } from './types';
export default async function generate(
siteDir: string,
opts: Partial<PluginOptions>,
) {
// we need to generate an empty sidebar up-front so it can be resolved from sidebars.js
const options = getOptions(siteDir, opts);
// if (options.sidebar) {
// writeSidebar(options.sidebar, 'module.exports=[];');
// }
// initialize and build app
const app = new Application();
// load the markdown plugin
load(app);
// customise render
app.renderer.render = render;
// add plugin options
addOptions(app);
// bootstrap typedoc app
app.bootstrap(options);
// add frontmatter component to typedoc renderer
// @ts-ignore
app.renderer.addComponent('fm', new FrontMatterComponent(app.renderer));
// add sidebar component to typedoc renderer
// @ts-ignore
app.renderer.addComponent('sidebar', new SidebarComponent(app.renderer));
// return the generated reflections
const project = app.convert();
// if project is undefined typedoc has a problem - error logging will be supplied by typedoc.
if (!project) {
return;
}
// generate or watch app
return app.generateDocs(project, options.outputDirectory);
}

View File

@@ -1,35 +0,0 @@
// @ts-ignore
import { ProjectReflection, UrlMapping } from 'typedoc';
// @ts-ignore
import { RendererEvent } from 'typedoc/dist/lib/output/events';
export async function render(
project: ProjectReflection,
outputDirectory: string,
) {
if (!this.prepareTheme() || !this.prepareOutputDirectory(outputDirectory)) {
return;
}
const output = new RendererEvent(
RendererEvent.BEGIN,
outputDirectory,
project,
);
output.settings = this.application.options.getRawValues();
output.urls = this.theme!.getUrls(project);
this.trigger(output);
if (!output.isDefaultPrevented) {
output.urls?.forEach((mapping: UrlMapping, i) => {
this.renderDocument(output.createPageEvent(mapping));
console.log(
`\rGenerated ${i + 1} of ${output.urls?.length} TypeDoc docs`,
);
});
console.log(`\n`);
this.trigger(RendererEvent.END, output);
}
}

View File

@@ -1,111 +0,0 @@
import * as fs from 'fs';
import * as path from 'path';
// @ts-ignore
import { BindOption } from 'typedoc';
// @ts-ignore
import { Component } from 'typedoc/dist/lib/converter/components';
// @ts-ignore
import { RendererComponent } from 'typedoc/dist/lib/output/components';
// @ts-ignore
import { RendererEvent } from 'typedoc/dist/lib/output/events';
import { SidebarItem, SidebarOptions } from './types';
@Component({ name: 'sidebar' })
export class SidebarComponent extends RendererComponent {
@BindOption('sidebar')
sidebar!: SidebarOptions;
@BindOption('siteDir')
siteDir!: string;
@BindOption('out')
out!: string;
initialize() {
// @ts-ignore
this.listenTo(this.application.renderer, {
[RendererEvent.BEGIN]: this.onRendererBegin,
});
}
async onRendererBegin(renderer: RendererEvent) {
// @ts-ignore
const navigation = this.application.renderer.theme?.getNavigation(
renderer.project,
);
const out = this.out.match(/(?:.*)en\/(.*)/)![1];
// map the navigation object to a Docuaurus sidebar format
const sidebarItems = navigation?.children
? navigation.children.map((navigationItem) => {
if (navigationItem.isLabel) {
const sidebarCategoryItems = navigationItem.children
? navigationItem.children.map((navItem) => {
const url = this.getUrlKey(out, navItem.url);
if (navItem.children && navItem.children.length > 0) {
const sidebarCategoryChildren = navItem.children.map(
(childGroup) =>
this.getSidebarCategory(
childGroup.title,
childGroup.children
? childGroup.children.map((childItem) =>
this.getUrlKey(out, childItem.url),
)
: [],
),
);
return this.getSidebarCategory(navItem.title, [
url,
...sidebarCategoryChildren,
]);
}
return url;
})
: [];
return this.getSidebarCategory(
navigationItem.title,
sidebarCategoryItems,
);
}
return this.getUrlKey(out, navigationItem.url);
})
: [];
const sidebarPath = this.sidebar.sidebarPath;
fs.writeFileSync(sidebarPath, JSON.stringify(sidebarItems, null, 2));
// @ts-ignore
this.application.logger.success(
`TypeDoc sidebar written to ${sidebarPath}`,
);
}
/**
* returns a sidebar category node
*/
getSidebarCategory(title: string, items: SidebarItem[]) {
return {
items,
type: 'category',
label: title,
};
}
/**
* returns the url key for relevant doc
*/
getUrlKey(out: string, url: string) {
const urlKey = url.replace('.md', '');
return out ? out + '/' + urlKey : urlKey;
}
}
/**
* Write content to sidebar file
*/
export const writeSidebar = (sidebar: SidebarOptions, content: string) => {
if (!fs.existsSync(path.dirname(sidebar.sidebarPath))) {
fs.mkdirSync(path.dirname(sidebar.sidebarPath));
}
fs.writeFileSync(sidebar.sidebarPath, content);
};

View File

@@ -1,46 +0,0 @@
export interface PluginOptions {
id: string;
docsRoot: string;
out: string;
sidebar: SidebarOptions | null;
readmeTitle?: string;
globalsTitle?: string;
plugin: string[];
readme?: string;
disableOutputCheck?: boolean;
entryPoints?: string[];
entryDocument: string;
hideInPageTOC: boolean;
hideBreadcrumbs: boolean;
siteDir: string;
outputDirectory: string;
watch: boolean;
}
export interface FrontMatter {
id?: string;
title: string;
slug?: string;
sidebar_label?: string;
hide_title?: boolean;
}
export interface SidebarOptions {
fullNames?: boolean;
sidebarFile: string;
sidebarPath: string;
indexLabel?: string;
readmeLabel?: string;
}
export interface Sidebar {
[sidebarId: string]: SidebarItem[];
}
export interface SidebarCategory {
type: string;
label: string;
items: SidebarItem[];
}
export type SidebarItem = SidebarCategory | string;

View File

@@ -1,23 +0,0 @@
import * as fs from 'fs';
import * as path from 'path';
// @ts-ignore
import { Application } from 'typedoc';
import { PluginOptions } from './types';
/**
* Calls TypeDoc's `convertAndWatch` and force trigger sidebars refresh.
*/
export const convertAndWatch = (app: Application, options: PluginOptions) => {
const sidebarsJsPath = path.resolve(options.siteDir, 'sidebars.js');
app.convertAndWatch(async (project) => {
if (options.sidebar) {
// remove typedoc sidebar from require cache
delete require.cache[options.sidebar.sidebarPath];
// force trigger a sidebars.js refresh
const sidebarJsContent = fs.readFileSync(sidebarsJsPath);
fs.writeFileSync(sidebarsJsPath, sidebarJsContent);
}
await app.generateDocs(project, options.outputDirectory);
});
};