Added gitlab wiki theme

This commit is contained in:
tgreyuk
2021-03-05 11:55:57 +00:00
parent c052ede3f5
commit 31007e8a6a
11 changed files with 266 additions and 6 deletions

View File

@@ -6,12 +6,13 @@
"scripts": {
"lint": "lerna run lint",
"build": "npm-run-all build:*",
"build:1": "cd packages/typedoc-plugin-markdown && npm run build",
"build:2": "cd packages/typedoc-bitbucket-theme && npm run build",
"build:3": "cd packages/typedoc-github-wiki-theme && npm run build",
"build:4": "cd packages/typedoc-hugo-theme && npm run build",
"build:5": "cd packages/vuepress-plugin-typedoc && npm run build",
"build:6": "cd packages/docusaurus-plugin-typedoc && npm run build",
"build:typedoc-plugin-markdown": "cd packages/typedoc-plugin-markdown && npm run build",
"build:typedoc-bitbucket-theme": "cd packages/typedoc-bitbucket-theme && npm run build",
"build:typedoc-github-wiki-theme": "cd packages/typedoc-github-wiki-theme && npm run build",
"build:typedoc-gitlab-wiki-theme": "cd packages/typedoc-gitlab-wiki-theme && npm run build",
"build:typedoc-hugo-theme": "cd packages/typedoc-hugo-theme && npm run build",
"build:vuepress-plugin-typedoc": "cd packages/vuepress-plugin-typedoc && npm run build",
"build:docusaurus-plugin-typedoc": "cd packages/docusaurus-plugin-typedoc && npm run build",
"pretest": "npm run build",
"test": "lerna run test --parallel",
"deploy": "lerna publish from-package",

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Thomas Grey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,29 @@
# typedoc-gitlab-wiki-theme
A [Gitlab Wiki](https://docs.gitlab.com/ee/user/project/wiki/) compatible Markdown theme extended from [typedoc-plugin-markdown](https://github.com/tgreyuk/typedoc-plugin-markdown/tree/master/packages/typedoc-plugin-markdown).
[![npm](https://img.shields.io/npm/v/typedoc-gitlab-wiki-theme.svg)](https://www.npmjs.com/package/typedoc-gitlab-wiki-theme)
## What is does?
- Generates Wiki friendly file names.
- Updates internal urls to a compatible format.
- Writes a `_sidebar.md` file to enable custom sidebar navigation.
## Installation
```shell
npm install typedoc typedoc-plugin-markdown typedoc-gitlab-wiki-theme --save-dev
```
## Usage
**npm script**
```javascript
"docs": "typedoc --theme ./node_modules/typedoc-gitlab-wiki-theme/dist ...options"
```
**shell**
```bash
$ npx typedoc --theme ./node_modules/typedoc-gitlab-wiki-theme/dist ...options
```

View File

@@ -0,0 +1,7 @@
const base = require('../../jest.config.base.js');
module.exports = {
...base,
name: 'typedoc-gitlab-wiki-theme',
displayName: 'typedoc-gitlab-wiki-theme',
};

View File

@@ -0,0 +1,33 @@
{
"name": "typedoc-gitlab-wiki-theme",
"version": "0.0.0",
"description": "An extension of the default theme from typedoc-plugin-markdown to create a compatible file structure for Github Wiki pages.",
"main": "dist/theme.js",
"files": [
"dist/"
],
"bugs": {
"url": "https://github.com/tgreyuk/typedoc-plugin-markdown/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tgreyuk/typedoc-plugin-markdown.git",
"directory": "packages/typedoc-gitlab-wiki-theme"
},
"homepage": "https://github.com/tgreyuk/typedoc-plugin-markdown/tree/master/packages/typedoc-gitlab-wiki-theme",
"scripts": {
"lint": "eslint ./src --ext .ts",
"prepublishOnly": "yarn run lint && yarn run build && yarn run test",
"build": "rm -rf ./dist && tsc && copyfiles --up 1 ./src/**/*.hbs ./dist/",
"test": "jest --colors",
"build-and-test": "yarn run build && yarn run test",
"demo": "rm -rf ./demo && yarn run build && typedoc ../typedoc-plugin-markdown/test/stubs/src/index.ts --tsconfig ../typedoc-plugin-markdown/test/stubs/tsconfig.json --theme ./dist --disableOutputCheck --out ./demo"
},
"author": "Thomas Grey",
"license": "MIT",
"peerDependencies": {
"typedoc": ">=0.20.0",
"typedoc-plugin-markdown": ">=3.6.0"
},
"keywords": []
}

View File

@@ -0,0 +1,51 @@
import * as fs from 'fs';
import { DeclarationReflection, NavigationItem } from 'typedoc';
import MarkdownTheme from 'typedoc-plugin-markdown/dist/theme';
import { GroupPlugin } from 'typedoc/dist/lib/converter/plugins';
import { RendererEvent } from 'typedoc/dist/lib/output/events';
import { Renderer } from 'typedoc/dist/lib/output/renderer';
import { TemplateMapping } from 'typedoc/dist/lib/output/themes/DefaultTheme';
import { UtilsComponent } from './utils';
export default class GitlabTheme extends MarkdownTheme {
constructor(renderer: Renderer, basePath: string) {
super(renderer, basePath);
renderer.application.options.setValue('entryDocument', 'home.md');
renderer.application.options.setValue('hideBreadcrumbs', true);
renderer.application.options.setValue('hidePageTitle', true);
renderer.addComponent('utils', new UtilsComponent(renderer));
this.listenTo(renderer, RendererEvent.END, this.onGitLabRendererEnd);
}
toUrl(mapping: TemplateMapping, reflection: DeclarationReflection) {
return `${mapping.directory}/${GroupPlugin.getKindSingular(
reflection.kind,
)}:-${reflection.name}.md`;
}
onGitLabRendererEnd(renderer: RendererEvent) {
const parseUrl = (url: string) => url.replace(/(.*).md/, '$1');
const navigation: NavigationItem = this.getNavigation(renderer.project);
const navJson: string[] = [
`## [${renderer.project.name}](${parseUrl(this.entryDocument)})\n`,
];
if (navigation.children) {
navigation.children.forEach((navItem) => {
if (navItem.isLabel) {
navJson.push(`\n### ${navItem.title}\n`);
navItem.children?.forEach((navItemChild) => {
const longTitle = navItemChild.title.split('.');
const shortTitle = longTitle[longTitle.length - 1];
navJson.push(`- [${shortTitle}](${parseUrl(navItemChild.url)})`);
});
}
});
}
fs.writeFileSync(
renderer.outputDirectory + '/_sidebar.md',
navJson.join('\n'),
);
}
}

View File

@@ -0,0 +1,19 @@
import {
Component,
ContextAwareRendererComponent,
} from 'typedoc/dist/lib/output/components';
import MarkdownTheme from './theme';
@Component({ name: 'gitlab-wiki-utils' })
export class UtilsComponent extends ContextAwareRendererComponent {
initialize() {
super.initialize();
MarkdownTheme.HANDLEBARS.registerHelper('relativeURL', (url) => {
const relativeUrl = this.getRelativeUrl(
url.replace(/(.*).md/, '$1'),
).replace(/ /g, '-');
return relativeUrl.startsWith('..') ? relativeUrl : './' + relativeUrl;
});
}
}

View File

@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Theme: should getUrls' 1`] = `
Array [
"modules.md",
"modules.md#declarationitema",
"modules.md#declarationitemb",
"modules.md#functionitema",
"modules.md#functionitemb",
"home.md",
"modules.md#declarationitema",
"modules.md#declarationitemb",
"modules.md#functionitema",
"modules.md#functionitemb",
"modules/Namespace:-namespaceA.md",
"classes/Class:-SomeNestedClass.md",
"classes/Class:-SomeNestedClass.md#constructor",
"modules/Namespace:-namespaceB.md",
"enums/Enumeration:-enumItemA.md",
"enums/Enumeration:-enumItemB.md",
"classes/Class:-ClassItemA.md",
"classes/Class:-ClassItemA.md#constructor",
"classes/Class:-ClassItemB.md",
"classes/Class:-ClassItemB.md#constructor",
"interfaces/Interface:-InterfaceItemA.md",
"interfaces/Interface:-InterfaceItemA.md#prop",
"interfaces/Interface:-InterfaceItemB.md",
"interfaces/Interface:-InterfaceItemB.md#prop",
]
`;
exports[`Theme: should write sidebar' 1`] = `
"## [typedoc-plugin-markdown](home)
### Namespaces
- [namespaceA](modules/Namespace:-namespaceA)
- [namespaceB](modules/Namespace:-namespaceB)
### Enumerations
- [enumItemA](enums/Enumeration:-enumItemA)
- [enumItemB](enums/Enumeration:-enumItemB)
### Classes
- [SomeNestedClass](classes/Class:-SomeNestedClass)
- [ClassItemA](classes/Class:-ClassItemA)
- [ClassItemB](classes/Class:-ClassItemB)
### Interfaces
- [InterfaceItemA](interfaces/Interface:-InterfaceItemA)
- [InterfaceItemB](interfaces/Interface:-InterfaceItemB)"
`;

View File

@@ -0,0 +1,33 @@
import * as fs from 'fs';
import MarkdownTheme from 'typedoc-plugin-markdown/dist/theme';
import { TestApp } from 'typedoc-plugin-markdown/test/test-app';
let testApp: TestApp;
beforeAll(async () => {
testApp = new TestApp(['theme.ts']);
await testApp.bootstrap({ theme: './dist', entryDocument: 'home.md' });
});
describe(`Theme:`, () => {
test(`should getUrls'`, async () => {
const urlMappings = testApp.theme.getUrls(testApp.project);
expect(TestApp.getExpectedUrls(urlMappings)).toMatchSnapshot();
});
test(`should write sidebar'`, async () => {
const sidebarFile = fs.readFileSync(testApp.tmpobj.name + '/_sidebar.md');
expect(sidebarFile.toString()).toMatchSnapshot();
});
describe(`(relativeUrls)`, () => {
test(`should convert a url'`, async () => {
expect(
MarkdownTheme.HANDLEBARS.helpers.relativeURL('SomeClass.md'),
).toEqual('../SomeClass');
});
test(`should convert a url with ancor'`, async () => {
expect(
MarkdownTheme.HANDLEBARS.helpers.relativeURL('SomeClass.md#anchor'),
).toEqual('../SomeClass#anchor');
});
});
});

View File

@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"exclude": ["./dist", "./test", "**/*.spec.ts"]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.test.json"
}