@@ -3,6 +3,8 @@ import FileEngine from './file.engine';
33
44const ngdT = require ( '@compodoc/ngd-transformer' ) ;
55
6+ type GraphContext = 'module' | 'overview' ;
7+
68export 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 i d = " n o d e \d + " c l a s s = " n o d e " > [ \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 ( / < t i t l e > ( [ \s \S ] * ?) < \/ t i t l e > / ) ;
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 i d = " n o d e \d + " c l a s s = " n o d e " > / ) ;
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, '&' )
124+ . replace ( / " / g, '"' )
125+ . replace ( / < / g, '<' )
126+ . replace ( / > / g, '>' ) ;
127+ }
42128}
43129
44130export default NgdEngine . getInstance ( ) ;
0 commit comments