Skip to content

Commit 573b56e

Browse files
committed
Add clickable navigation for dependency graph nodes
closes #513 closes #1296 closes #1330 closes #1359
1 parent 3ced357 commit 573b56e

2 files changed

Lines changed: 91 additions & 3 deletions

File tree

‎src/app/application.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,6 +3576,7 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
35763576
"dependencies.svg",
35773577
),
35783578
modules[i].name,
3579+
"module",
35793580
).then(
35803581
(data) => {
35813582
modules[i].graph = data;
@@ -3620,6 +3621,7 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
36203621
finalMainGraphPath + path.sep + "dependencies.svg",
36213622
),
36223623
"Main graph",
3624+
"overview",
36233625
).then(
36243626
(data) => {
36253627
Configuration.mainData.mainGraph = data;

‎src/app/engines/ngd.engine.ts‎

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import FileEngine from './file.engine';
33

44
const ngdT = require('@compodoc/ngd-transformer');
55

6+
type GraphContext = 'module' | 'overview';
7+
68
export class NgdEngine {
79
public engine;
810

@@ -34,11 +36,95 @@ export class NgdEngine {
3436
}
3537
}
3638

37-
public readGraph(filepath: string, name: string): Promise<string> {
38-
return FileEngine.get(filepath).catch(err =>
39-
Promise.reject('Error during graph read ' + name)
39+
public readGraph(filepath: string, name: string, context: GraphContext = 'overview'): Promise<string> {
40+
return FileEngine.get(filepath)
41+
.then(svg => this.addNavigationLinks(svg, context))
42+
.catch(_err => Promise.reject('Error during graph read ' + name));
43+
}
44+
45+
private addNavigationLinks(svg: string, context: GraphContext): string {
46+
// Graphviz emits one <g class="node"> per logical node.
47+
// Wrapping each node body inside an <a> makes the whole shape clickable.
48+
return svg.replace(
49+
/<g id="node\d+" class="node">[\s\S]*?<\/g>/g,
50+
nodeBlock => {
51+
if (nodeBlock.indexOf('xlink:href=') !== -1 || nodeBlock.indexOf(' href=') !== -1) {
52+
return nodeBlock;
53+
}
54+
55+
const titleMatch = nodeBlock.match(/<title>([\s\S]*?)<\/title>/);
56+
if (!titleMatch) {
57+
return nodeBlock;
58+
}
59+
60+
const nodeName = titleMatch[1].trim();
61+
const href = this.resolveDocumentationHref(nodeName, context);
62+
if (!href) {
63+
return nodeBlock;
64+
}
65+
66+
const openTagMatch = nodeBlock.match(/^<g id="node\d+" class="node">/);
67+
if (!openTagMatch) {
68+
return nodeBlock;
69+
}
70+
71+
const openTag = openTagMatch[0];
72+
const inner = nodeBlock
73+
.slice(openTag.length)
74+
.replace(/<\/g>$/, '');
75+
const safeHref = this.escapeAttribute(href);
76+
77+
return `${openTag}<a xlink:href="${safeHref}" href="${safeHref}" target="_top">${inner}</a></g>`;
78+
}
4079
);
4180
}
81+
82+
private resolveDocumentationHref(nodeName: string, context: GraphContext): string | undefined {
83+
if (!nodeName) {
84+
return undefined;
85+
}
86+
87+
const resolved = DependenciesEngine.find(nodeName);
88+
if (!resolved || resolved.source !== 'internal' || !resolved.data) {
89+
return undefined;
90+
}
91+
92+
const dependencyType = String(resolved.data.type || '');
93+
const targetFolderByType: Record<string, string> = {
94+
class: 'classes',
95+
component: 'components',
96+
controller: 'controllers',
97+
directive: 'directives',
98+
entity: 'entities',
99+
guard: 'guards',
100+
injectable: 'injectables',
101+
interceptor: 'interceptors',
102+
interface: 'interfaces',
103+
module: 'modules',
104+
pipe: 'pipes'
105+
};
106+
107+
const targetFolder = targetFolderByType[dependencyType];
108+
if (!targetFolder) {
109+
return undefined;
110+
}
111+
112+
const pageName = resolved.data.isDuplicate ? resolved.data.duplicateName : resolved.data.name;
113+
if (!pageName) {
114+
return undefined;
115+
}
116+
117+
const relativePrefix = context === 'module' ? '..' : '.';
118+
return `${relativePrefix}/${targetFolder}/${pageName}.html`;
119+
}
120+
121+
private escapeAttribute(value: string): string {
122+
return value
123+
.replace(/&/g, '&amp;')
124+
.replace(/"/g, '&quot;')
125+
.replace(/</g, '&lt;')
126+
.replace(/>/g, '&gt;');
127+
}
42128
}
43129

44130
export default NgdEngine.getInstance();

0 commit comments

Comments
 (0)