Skip to content

Commit

Permalink
Support discovering relative links in HTML tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Jun 9, 2024
1 parent 774ac92 commit 59c1ad5
Show file tree
Hide file tree
Showing 7 changed files with 3,357 additions and 2,242 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
sort - documents-first, documents-last, alphabetical-ignoring-documents
searchInDocuments
- #2147, add `referencedProperties` to a `ReferenceType` which is a set of name/comment pairs
- Detect relative links in `<a>` and `<img>`

### Breaking Changes

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@
],
"scripts": {
"test": "mocha --config .config/mocha.fast.json",
"test:cov": "c8 mocha --config .config/mocha.fast.json",
"test:cov": "c8 -r lcov mocha --config .config/mocha.fast.json",
"doc:c": "node bin/typedoc --tsconfig src/test/converter/tsconfig.json",
"doc:cd": "node --inspect-brk bin/typedoc --tsconfig src/test/converter/tsconfig.json",
"doc:c2": "node bin/typedoc --options src/test/converter2 --tsconfig src/test/converter2/tsconfig.json",
"doc:c2d": "node --inspect-brk bin/typedoc --options src/test/converter2 --tsconfig src/test/converter2/tsconfig.json",
"example": "cd example && node ../bin/typedoc",
"test:full": "c8 mocha --config .config/mocha.full.json",
"test:full": "c8 -r lcov -r text-summary mocha --config .config/mocha.full.json",
"test:visual": "ts-node ./src/test/capture-screenshots.ts && ./scripts/compare_screenshots.sh",
"test:visual:accept": "node scripts/accept_visual_regression.js",
"rebuild_specs": "node scripts/rebuild_specs.js",
Expand Down
57 changes: 54 additions & 3 deletions src/lib/converter/comments/textParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
} from "../../internationalization";
import type { CommentDisplayPart } from "../../models";
import type { FileRegistry } from "../../models/FileRegistry";
import { HtmlAttributeParser, ParserState } from "../../utils/html";
import { type Token, TokenSyntaxKind } from "./lexer";

import MarkdownIt from "markdown-it";
Expand All @@ -36,9 +37,6 @@ interface RelativeLink {
/**
* Look for relative links within a piece of text and add them to the {@link FileRegistry}
* so that they can be correctly resolved during rendering.
*
* TODO: We also handle `<a>` and `<img>` tags with relative targets here.
*
*/
export function textContent(
sourcePath: string,
Expand Down Expand Up @@ -99,6 +97,12 @@ export function textContent(
continue;
}

const tagLink = checkTagLink(data);
if (tagLink) {
addRef(tagLink);
continue;
}

++data.pos;
}

Expand Down Expand Up @@ -204,6 +208,53 @@ function checkReference(data: TextParserData): RelativeLink | undefined {
}
}

/**
* Looks for `<a href="./relative">` and `<img src="./relative">`
*/
function checkTagLink(data: TextParserData): RelativeLink | undefined {
const { pos, token } = data;

if (token.text.startsWith("<img ", pos)) {
data.pos += 4;
return checkAttribute(data, "src");
}

if (token.text.startsWith("<a ", pos)) {
data.pos += 3;
return checkAttribute(data, "href");
}
}

function checkAttribute(
data: TextParserData,
attr: string,
): RelativeLink | undefined {
const parser = new HtmlAttributeParser(data.token.text, data.pos);
while (parser.state !== ParserState.END) {
if (
parser.state === ParserState.BeforeAttributeValue &&
parser.currentAttributeName === attr
) {
parser.step();

if (isRelativeLink(parser.currentAttributeValue)) {
data.pos = parser.pos;
return {
pos: parser.currentAttributeValueStart,
end: parser.currentAttributeValueEnd,
target: data.files.register(
data.sourcePath,
parser.currentAttributeValue,
),
};
}
return;
}

parser.step();
}
}

function isRelativeLink(link: string) {
return !/^[a-z]+:\/\/|^\/|^[a-z]:\\/i.test(link);
}
Expand Down
Loading

0 comments on commit 59c1ad5

Please sign in to comment.