Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autoDetectPageTitle parameter #33

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# typedoc-plugin-pages
# typedoc-plugin-loopingz-pages

The project is a fork of [typedoc-plugin-pages](https://github.com/mipatterson/typedoc-plugin-pages)
The original author seems to be busy, so creating the fork

> A TypeDoc plugin that lets you integrate your own pages into the documentation output

Expand Down
7 changes: 6 additions & 1 deletion docs-source/configuration/configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ If you are configuring TypeDoc via the `typedoc.json` configuration file, you ca
"theme": "pages-plugin",
"pages": {
"groups": [],
"output": "pages"
"output": "pages",
"autoDetectPageTitle": true
}
}
```

```
autoDetectPageTitle: boolean // Use title from first line of Markdown files
```

## 2. Use a dedicated `pagesconfig.json` file

If you are not using a `typedoc.json` configuration file, or if you would like to separate your plugin configuration from the TypeDoc configuration, you can define your plugin configuration in a `pagesconfig.json` file in the directory where TypeDoc will be executed from:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "typedoc-plugin-pages",
"name": "typedoc-plugin-loopingz-pages",
"version": "0.0.1",
"description": "A TypeDoc plugin that lets you integrate your own pages into the documentation output",
"main": "dist/plugin/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/options/models/child-page-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
export interface ChildPageDefinition {
output?: string;
source: string;
title: string;
title?: string;
}
8 changes: 8 additions & 0 deletions src/options/models/plugin-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,18 @@ export interface PluginOptions {
*/
replaceGlobalsPage?: boolean;

/**
* Use first line title '# Title' if markdown starts with '# '
*
* This defaults to false
*/
autoDetectPageTitle?: boolean;

/**
* Root directory where all page source files live
*
* By default this will point to the directory that TypeDoc is run from.
*/
source?: string;

}
4 changes: 2 additions & 2 deletions src/options/option-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ChildPageDefinition, PageDefinition, PageGroupDefinition, PluginOptions
import { defaultOptionAndValidateIsArray, defaultOptionAndValidateIsBoolean, defaultOptionAndValidateIsString,
validateOptionIsArray, validateOptionIsString } from "./validation-utilities";
import * as Constants from "../constants";
import { join } from "path";
import { basename, join } from "path";
import { ensurePathHasExtension, getFilename } from "../utilities/path-utilities";

/**
Expand Down Expand Up @@ -78,8 +78,8 @@ export class OptionValidator {

private _validatePage(definition: PageDefinition|ChildPageDefinition, sourcePrefix: string, isChild: boolean): void {
try {
validateOptionIsString(definition, "title");
validateOptionIsString(definition, "source");
defaultOptionAndValidateIsString(definition, "title", basename(definition.source));
defaultOptionAndValidateIsString(definition, "output", getFilename(definition.source, true)); // Use source file name as default output file name


Expand Down
4 changes: 2 additions & 2 deletions src/pages/models/base-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module Models
*/

import { join } from "path";
import { basename, join } from "path";
import { ChildPageDefinition, PageDefinition, PageGroupDefinition, PageSectionDefinition } from "../../options/models/";

// TODO: Document this
Expand All @@ -12,7 +12,7 @@ export abstract class BaseItem {
protected _url: string;

constructor(definition: PageDefinition|ChildPageDefinition|PageSectionDefinition|PageGroupDefinition, urlPrefix: string) {
this._title = definition.title;
this._title = definition.title || basename(definition.source);
this._url = join(urlPrefix, definition.output);
}

Expand Down
8 changes: 8 additions & 0 deletions src/pages/models/page-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export abstract class PageBase extends BaseItem {
}
}

public computeTitle(): void {
const content = this.contents;
if (content.startsWith("# ")) {
this._title = content.substr(2, content.indexOf("\n")-2);
this._contents = content.substr(content.indexOf("\n")+1);
}
}

public abstract get parent(): Page|PageGroup;

public get source(): string {
Expand Down
15 changes: 12 additions & 3 deletions src/pages/page-dictionary-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import { getFilename } from "../utilities/path-utilities";
import { ChildPage, Page, PageDictionary, PageGroup, PageSection } from "./models/";

// TODO: document this
export class PageDictionaryFactory {
export class PageDictionaryFactory {
private options: PluginOptions;

public buildDictionary(options: PluginOptions): PageDictionary {
this.options = options;
const groups: PageGroup[] = [];

for (const group of options.groups) {
Expand Down Expand Up @@ -50,7 +53,10 @@ export class PageDictionaryFactory {

private _parsePage(definition: PageDefinition, parent: PageGroup): void {
const page = new Page(definition, parent);

if (this.options.autoDetectPageTitle) {
page.computeTitle();
}

// Get directory name for any child pages or sub-sections
const subDirectory = join(parent.url, getFilename(definition.output, true));

Expand All @@ -70,6 +76,9 @@ export class PageDictionaryFactory {
}

private _parseChildPage(definition: ChildPageDefinition, urlPrefix: string, parent: Page): void {
new ChildPage(definition, urlPrefix, parent);
const page = new ChildPage(definition, urlPrefix, parent);
if (this.options.autoDetectPageTitle) {
page.computeTitle();
}
}
}
12 changes: 12 additions & 0 deletions test/unit/models/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ describe("Page", () => {

expect(() => sut.contents).toThrow();
})

test("returns the contents of the file with title substitution", () => {
const readFileSyncMock = jest.fn();
readFileSyncMock.mockReturnValue("# Title\nfile contents");
(fs as any).readFileSync = readFileSyncMock;

sut.computeTitle();
expect(sut.contents).toBe("file contents");
console.log(sut.title);
expect(sut.title).toBe("Title");
expect(readFileSyncMock).toHaveBeenCalledWith(definition.source, "utf8");
});
});

describe("url", () => {
Expand Down