Skip to content

Commit

Permalink
fix: Enable anchor linking within tables
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Aug 29, 2023
1 parent c071d51 commit d32f99d
Show file tree
Hide file tree
Showing 32 changed files with 1,448 additions and 912 deletions.
6 changes: 6 additions & 0 deletions .changeset/smart-spoons-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'typedoc-plugin-markdown': patch
---

- UI improvements
- Enable anchor to items inside table
196 changes: 98 additions & 98 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"@changesets/cli": "^2.26.2",
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.5.4",
"@types/node": "^20.5.6",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"@types/node": "^20.5.7",
"@typescript-eslint/eslint-plugin": "^6.5.0",
"@typescript-eslint/parser": "^6.5.0",
"copyfiles": "^2.4.1",
"eslint": "^8.48.0",
"jest": "^29.6.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/typedoc-plugin-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"lint:ts": "eslint ./src --ext .ts",
"lint:markdown": "npm run docs && markdownlint ./out/md",
"prepublishOnly": "npm run lint && npm run build",
"copyJs": "copyfiles --up 1 ./src/**/*.*js ./dist/",
"prebuild": "rm -rf dist && npm run copyJs && npm-run-all task:code",
"prebuild": "rm -rf dist && npm-run-all task:code",
"build": "tsc",
"test": "jest --colors",
"prestubs": "npm run build",
"stubs": "npm-run-all stubs:*",
"stubs:md-1": "typedoc --options ./test/typedoc1.cjs --out ./test/fixtures/md-1",
"stubs:md-2": "typedoc --options ./test/typedoc2.cjs --out ./test/fixtures/md-2",
"stubs:md-3": "typedoc --options ./test/typedoc3.cjs --out ./test/fixtures/md-3",
"stubs:md-4": "typedoc --options ./test/typedoc4.cjs --out ./test/fixtures/md-4",
"stubs:html": "typedoc --options ./test/typedoc-html.cjs --out ./test/fixtures/html",
"build-and-test": "npm run build && npm run test",
"api-docs": "npm run build && typedoc --options ./typedoc.api.js --out ./docs/api",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const typeAndParent = (
context: MarkdownThemeRenderContext,
props: ArrayType | ReferenceType,
) => {
const getUrl = (name: string, url: string) =>
const getLink = (name: string, url: string) =>
link(backTicks(name), context.relativeURL(url));

if (props) {
Expand All @@ -55,27 +55,38 @@ const typeAndParent = (
if (props.reflection instanceof SignatureReflection) {
if (props.reflection.parent?.parent?.url) {
md.push(
getUrl(
getLink(
props.reflection.parent.parent.name,
props.reflection.parent.parent.url,
),
);
if (props.reflection.parent.url) {
if (props.reflection.parent?.url) {
md.push(
getUrl(
getLink(
props.reflection.parent.name,
props.reflection.parent.url,
),
);
}
}
} else {
if (props.reflection.parent?.url) {
md.push(
getUrl(props.reflection.parent.name, props.reflection.parent.url),
);
if (props.reflection.url) {
md.push(getUrl(props.reflection.name, props.reflection.url));
if (props.reflection.parent) {
if (props.reflection.parent.url) {
md.push(
getLink(
props.reflection.parent.name,
props.reflection.parent.url,
),
);
} else {
md.push(backTicks(props.reflection.parent.name));
}
if (props.reflection) {
if (props.reflection.url) {
md.push(getLink(props.reflection.name, props.reflection.url));
} else {
md.push(backTicks(props.reflection.name));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ export function members(
const items = children?.filter((item) => !item.hasOwnDocument);
items?.forEach((item, index) => {
md.push(context.member(item, memberHeadingLevel || headingLevel));
if (index !== items.length - 1) {
if (
index !== items.length - 1 &&
(item.kindOf([
ReflectionKind.Class,
ReflectionKind.Interface,
ReflectionKind.Function,
ReflectionKind.Enum,
ReflectionKind.Variable,
]) ||
context.options.getValue('outputFileStrategy') === 'members')
) {
md.push(horizontalRule());
}
});
Expand Down
66 changes: 43 additions & 23 deletions packages/typedoc-plugin-markdown/src/theme/url-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { TemplateMapping, UrlOption } from './models';

export class UrlBuilder {
urls: UrlMapping[] = [];
anchors: string[] = [];
anchors: Record<string, string[]> = {};

constructor(
public theme: MarkdownTheme,
Expand Down Expand Up @@ -225,7 +225,6 @@ export class UrlBuilder {
: `${kindAlias}.${friendlyName}`;
}

// remove leading underscores
const alias = reflection.getAlias().replace(/^_/, '');

const parentDir = options.parentUrl
Expand Down Expand Up @@ -304,36 +303,39 @@ export class UrlBuilder {
reflection: DeclarationReflection,
container: Reflection,
) {
if (container.url && !reflection.url) {
if (container.url) {
if (!reflection.kindOf(ReflectionKind.TypeLiteral)) {
const anchorPrefix = this.options.getValue('anchorPrefix');
const anchorId = this.getAnchorId(reflection);

if (!this.anchors[container.url]) {
this.anchors[container.url] = [];
}
if (anchorId) {
if (!this.anchors[container.url]) {
this.anchors[container.url] = [];
}

this.anchors[container.url].push(anchorId);
this.anchors[container.url].push(anchorId);

const count = this.anchors[container.url]?.filter(
(id) => id === anchorId,
)?.length;
const count = this.anchors[container.url]?.filter(
(id) => id === anchorId,
)?.length;

const anchorParts = [anchorId];
const anchorParts = [anchorId];

if (count > 1) {
anchorParts.push(`-${count}`);
}
if (count > 1) {
anchorParts.push(`-${count}`);
}

if (anchorPrefix) {
anchorParts.unshift(`${anchorPrefix}`);
}
if (anchorPrefix) {
anchorParts.unshift(`${anchorPrefix}`);
}

reflection.url = container.url + '#' + anchorParts.join('');
reflection.anchor = anchorParts.join('');
reflection.url = container.url + '#' + anchorParts.join('');
reflection.anchor = anchorParts.join('');
}
}
reflection.hasOwnDocument = false;
} else if (reflection.parent) {
}
if (reflection.parent) {
reflection.traverse((child) => {
if (child instanceof DeclarationReflection) {
this.applyAnchorUrl(child, container);
Expand All @@ -346,9 +348,27 @@ export class UrlBuilder {
const preserveAnchorCasing = this.options.getValue(
'preserveAnchorCasing',
) as boolean;
return preserveAnchorCasing
? reflection.name
: reflection.name.toLowerCase();

const anchorName = this.getAnchorName(reflection);

if (anchorName) {
return preserveAnchorCasing ? anchorName : anchorName.toLowerCase();
}

return null;
}

private getAnchorName(reflection: DeclarationReflection) {
const namedAnchors = this.options.getValue('namedAnchors');
const propertiesTableStyle =
this.options.getValue('propertiesFormat') === 'table';

if (!namedAnchors) {
if (reflection.kindOf(ReflectionKind.Property) && propertiesTableStyle) {
return null;
}
}
return reflection.name;
}

private getPartName(part: string, position: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Constructor comments

#### Source

[classes.ts:64](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L64)
[classes.ts:64](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L64)

## Properties

Expand All @@ -46,7 +46,7 @@ Comments for abstractProp

#### Source

[classes.ts:18](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L18)
[classes.ts:18](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L18)

***

Expand All @@ -58,7 +58,7 @@ Comments for privateProp

#### Source

[classes.ts:28](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L28)
[classes.ts:28](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L28)

***

Expand All @@ -70,7 +70,7 @@ Comments for propWithDefault

#### Source

[classes.ts:43](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L43)
[classes.ts:43](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L43)

***

Expand All @@ -82,7 +82,7 @@ Comments for protectedProp

#### Source

[classes.ts:38](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L38)
[classes.ts:38](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L38)

***

Expand All @@ -94,7 +94,7 @@ Comments for abstractProperty

#### Source

[classes.ts:33](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L33)
[classes.ts:33](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L33)

***

Expand All @@ -106,7 +106,7 @@ Comments for staticProp

#### Source

[classes.ts:23](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L23)
[classes.ts:23](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L23)

## Accessors

Expand All @@ -122,7 +122,7 @@ Comments for getter

#### Source

[classes.ts:48](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L48)
[classes.ts:48](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L48)

***

Expand All @@ -144,7 +144,7 @@ Comments for setter

#### Source

[classes.ts:56](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L56)
[classes.ts:56](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L56)

## Methods

Expand All @@ -160,7 +160,7 @@ Comments for abstractMethod

#### Source

[classes.ts:75](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L75)
[classes.ts:75](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L75)

***

Expand All @@ -176,7 +176,7 @@ Comment for publicMethod

#### Source

[classes.ts:87](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L87)
[classes.ts:87](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L87)

***

Expand All @@ -192,7 +192,7 @@ Comment for publicMethod

#### Source

[classes.ts:80](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L80)
[classes.ts:80](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L80)

***

Expand All @@ -208,4 +208,4 @@ Comment for publicMethod

#### Source

[classes.ts:92](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c50784c/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L92)
[classes.ts:92](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/c071d51/packages/typedoc-plugin-markdown/test/stubs/default/classes.ts#L92)
Loading

0 comments on commit d32f99d

Please sign in to comment.