Skip to content

Commit

Permalink
fix(jsdoc): @link for external url broken
Browse files Browse the repository at this point in the history
fix #305
  • Loading branch information
vogloblinsky committed Feb 28, 2018
1 parent 491f567 commit 6775ef9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 35 deletions.
101 changes: 77 additions & 24 deletions src/app/engines/html-engine-helpers/parse-description.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { extractLeadingText, splitLinkText } from '../../../utils/link-parser';
import { DependenciesEngine } from '../dependencies.engine';

export class ParseDescriptionHelper implements IHtmlEngineHelper {
constructor(private dependenciesEngine: DependenciesEngine) {

}
constructor(private dependenciesEngine: DependenciesEngine) {}

public helperFunc(context: any, description: string, depth: number) {
let tagRegExpLight = new RegExp('\\{@link\\s+((?:.|\n)+?)\\}', 'i');
Expand All @@ -15,12 +13,12 @@ export class ParseDescriptionHelper implements IHtmlEngineHelper {
let previousString;
let tagInfo = [];

tagRegExp = (description.indexOf(']{') !== -1) ? tagRegExpFull : tagRegExpLight;
tagRegExp = description.indexOf(']{') !== -1 ? tagRegExpFull : tagRegExpLight;

const processTheLink = (string, tagInfo, leadingText) => {
let leading = extractLeadingText(string, tagInfo.completeTag);
let split;
let result;
let resultInCompodoc;
let newLink;
let rootPath;
let stringtoReplace;
Expand All @@ -31,20 +29,19 @@ export class ParseDescriptionHelper implements IHtmlEngineHelper {
split = splitLinkText(tagInfo.text);

if (typeof split.linkText !== 'undefined') {
result = this.dependenciesEngine.findInCompodoc(split.target);
resultInCompodoc = this.dependenciesEngine.findInCompodoc(split.target);
} else {
let info = tagInfo.text;
if (tagInfo.text.indexOf('#') !== -1) {
anchor = tagInfo.text.substr(tagInfo.text.indexOf('#'), tagInfo.text.length);
info = tagInfo.text.substr(0, tagInfo.text.indexOf('#'));
}
result = this.dependenciesEngine.findInCompodoc(info);
resultInCompodoc = this.dependenciesEngine.findInCompodoc(info);
}

if (result) {

label = result.name;
pageName = result.name;
if (resultInCompodoc) {
label = resultInCompodoc.name;
pageName = resultInCompodoc.name;

if (leadingText) {
stringtoReplace = '[' + leadingText + ']' + tagInfo.completeTag;
Expand All @@ -56,19 +53,22 @@ export class ParseDescriptionHelper implements IHtmlEngineHelper {
stringtoReplace = tagInfo.completeTag;
}

if (result.type === 'class') {
result.type = 'classe';
} else if (result.type === 'miscellaneous' || (result.ctype && result.ctype === 'miscellaneous')) {
result.type = 'miscellaneou';
label = result.name;
anchor = '#' + result.name;
if (result.subtype === 'enum') {
if (resultInCompodoc.type === 'class') {
resultInCompodoc.type = 'classe';
} else if (
resultInCompodoc.type === 'miscellaneous' ||
(resultInCompodoc.ctype && resultInCompodoc.ctype === 'miscellaneous')
) {
resultInCompodoc.type = 'miscellaneou';
label = resultInCompodoc.name;
anchor = '#' + resultInCompodoc.name;
if (resultInCompodoc.subtype === 'enum') {
pageName = 'enumerations';
} else if (result.subtype === 'function') {
} else if (resultInCompodoc.subtype === 'function') {
pageName = 'functions';
} else if (result.subtype === 'typealias') {
} else if (resultInCompodoc.subtype === 'typealias') {
pageName = 'typealiases';
} else if (result.subtype === 'variable') {
} else if (resultInCompodoc.subtype === 'variable') {
pageName = 'variables';
}
}
Expand All @@ -95,8 +95,34 @@ export class ParseDescriptionHelper implements IHtmlEngineHelper {
label = split.linkText;
}

newLink = `<a href="${rootPath}${result.type}s/${pageName}.html${anchor}">${label}</a>`;

newLink = `<a href="${rootPath}${
resultInCompodoc.type
}s/${pageName}.html${anchor}">${label}</a>`;

return string.replace(stringtoReplace, newLink);
} else if (!resultInCompodoc && typeof split.linkText !== 'undefined') {
newLink = `<a href="${split.target}">${split.linkText}</a>`;
if (leadingText) {
stringtoReplace = '[' + leadingText + ']' + tagInfo.completeTag;
} else if (leading.leadingText !== undefined) {
stringtoReplace = '[' + leading.leadingText + ']' + tagInfo.completeTag;
} else if (typeof split.linkText !== 'undefined') {
stringtoReplace = tagInfo.completeTag;
} else {
stringtoReplace = tagInfo.completeTag;
}
return string.replace(stringtoReplace, newLink);
} else if (!resultInCompodoc && leading && typeof leading.leadingText !== 'undefined') {
newLink = `<a href="${split.target}">${leading.leadingText}</a>`;
if (leadingText) {
stringtoReplace = '[' + leadingText + ']' + tagInfo.completeTag;
} else if (leading.leadingText !== undefined) {
stringtoReplace = '[' + leading.leadingText + ']' + tagInfo.completeTag;
} else if (typeof split.linkText !== 'undefined') {
stringtoReplace = tagInfo.completeTag;
} else {
stringtoReplace = tagInfo.completeTag;
}
return string.replace(stringtoReplace, newLink);
} else {
return string;
Expand All @@ -118,15 +144,42 @@ export class ParseDescriptionHelper implements IHtmlEngineHelper {
}
}

// Clean description for marked a tag parsed too early

if (description.indexOf('href=') !== -1) {
let insideMarkedATagResults = description.match(/<a [^>]+>([^<]+)<\/a>/g);

if (insideMarkedATagResults && insideMarkedATagResults.length > 0) {
for (let i = 0; i < insideMarkedATagResults.length; i++) {
let markedATagRegExp = new RegExp('<a [^>]+>([^<]+)</a>', 'gm');
let parsedATag = markedATagRegExp.exec(description);
if (parsedATag && parsedATag.length === 2) {
let insideMarkedATag = parsedATag[1];
description = description.replace(
`{@link <a href="${insideMarkedATag}">${insideMarkedATag}</a>`,
`{@link ${insideMarkedATag}`
);
}
}
}
}

do {
matches = tagRegExp.exec(description);

if (matches) {
previousString = description;
if (matches.length === 2) {
description = replaceMatch(processTheLink, 'link', matches[0], matches[1]);
}
if (matches.length === 3) {
description = replaceMatch(processTheLink, 'link', matches[0], matches[2], matches[1]);
description = replaceMatch(
processTheLink,
'link',
matches[0],
matches[2],
matches[1]
);
}
}
} while (matches && previousString !== description);
Expand Down
13 changes: 12 additions & 1 deletion test/src/cli/cli-generation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('CLI simple generation', () => {
componentFile,
moduleFile,
emptyModuleFile,
barModuleFile,
emptyModuleRawFile;
before(function (done) {
tmp.create(distFolder);
Expand All @@ -34,6 +35,7 @@ describe('CLI simple generation', () => {
componentFile = read(`${distFolder}/components/BarComponent.html`);
emptyModuleFile = read(`${distFolder}/modules/EmptyModule.html`);
emptyModuleRawFile = read(`${distFolder}/modules/EmptyRawModule.html`);
barModuleFile = read(`${distFolder}/modules/BarModule.html`);
done();
});
after(() => tmp.clean(distFolder));
Expand Down Expand Up @@ -85,7 +87,6 @@ describe('CLI simple generation', () => {
});

it('it should have a link with this syntax [The BarComponent]{@link BarComponent}', () => {
const barModuleFile = read(`${distFolder}/modules/BarModule.html`);
expect(barModuleFile).to.contain('Watch <a href="../components/BarComponent.html">The BarComponent');
});

Expand All @@ -110,6 +111,16 @@ describe('CLI simple generation', () => {
expect(fooServiceFile).to.contain('typescriptlang.org');
});

it('it should have a link with this syntax {@link http://www.google.fr|Second link}', () => {
expect(barModuleFile).to.contain('<a href="http://www.google.fr">Second link</a>');
});
it('it should have a link with this syntax {@link http://www.google.uk Third link}', () => {
expect(barModuleFile).to.contain('<a href="http://www.google.uk">Third link</a>');
});
it('it should have a link with this syntax [Last link]{@link http://www.google.jp}', () => {
expect(barModuleFile).to.contain('<a href="http://www.google.jp">Last link</a>');
});

/**
* internal/private methods
*/
Expand Down
18 changes: 8 additions & 10 deletions test/src/sample-files/bar.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { BarService } from './bar.service';
/**
* BarModule description
*
* see {@link http://www.google.fr|Second link}
* see {@link http://www.google.uk Third link}
* see [Last link]{@link http://www.google.jp}
*
* Watch [The BarComponent]{@link BarComponent}
*/
@NgModule({
declarations: [
BarDirective, BarComponent
],
exports: [
BarDirective, BarComponent
],
providers: [
BarService
]
declarations: [BarDirective, BarComponent],
exports: [BarDirective, BarComponent],
providers: [BarService]
})
export class BarModule { }
export class BarModule {}

0 comments on commit 6775ef9

Please sign in to comment.