@@ -34,6 +34,33 @@ function runGuidReplacer() {
3434 var matchCount = 0 ;
3535 var nodeCount = 0 ;
3636
37+ // Collect all text nodes with possible GUID values (default)
38+ function traverseTextNodes ( element ) {
39+ var treeWalker = document . createTreeWalker ( element , NodeFilter . SHOW_TEXT , null , false ) ;
40+
41+ // Cache all nodes to be able to add nodes while traversing
42+ var textNodes = [ ] ;
43+ while ( treeWalker . nextNode ( ) ) {
44+ var node = treeWalker . currentNode ;
45+
46+ // Skip nodes that are too short to contain a GUID
47+ if ( ! node . nodeValue || node . nodeValue . length < 32 ) {
48+ continue ;
49+ }
50+
51+ // skip script nodes
52+ if ( node . parentNode . nodeName === "SCRIPT" ) {
53+ continue ;
54+ }
55+
56+ textNodes . push ( treeWalker . currentNode ) ;
57+ }
58+
59+ textNodes . forEach ( function ( node ) {
60+ processTextNode ( node ) ;
61+ } ) ;
62+ }
63+
3764 // Function to process a text node and add labels to GUIDs
3865 function processTextNode ( node ) {
3966 var originalContent = node . nodeValue ;
@@ -44,10 +71,10 @@ function runGuidReplacer() {
4471
4572 var replacement = guidLookup [ match ] ;
4673 var tagText = replacement ? replacement . fileName : "No matching GUID found" ;
74+ matchCount ++ ;
4775 return match + '<span class="guidResolverTag">[' + tagText + ']</span>' ;
4876 } ) ;
4977 if ( originalContent !== modifiedContent ) {
50- matchCount ++ ;
5178 node . nodeValue = '' ;
5279 var span = document . createElement ( 'span' ) ;
5380 span . innerHTML = modifiedContent ;
@@ -56,26 +83,65 @@ function runGuidReplacer() {
5683 nodeCount ++ ;
5784 }
5885
59- // Recursive function to traverse all text nodes in an element
60- function traverseTextNodes ( element ) {
61- var treeWalker = document . createTreeWalker ( element , NodeFilter . SHOW_TEXT , null , false ) ;
6286
63- // Cache all nodes to be able to add nodes while traversing
64- var textNodes = [ ] ;
65- while ( treeWalker . nextNode ( ) ) {
66- var node = treeWalker . currentNode ;
67- // Skip nodes that are too short to contain a GUID
68- if ( ! node . nodeValue || node . nodeValue . length < 32 ) {
69- continue ;
87+ // collect all special github elements with data-code-text attribute
88+ function traverseDataCodeTextElements ( element ) {
89+ var treeWalker = document . createTreeWalker ( element , NodeFilter . SHOW_ELEMENT , {
90+ acceptNode : function ( node ) {
91+ if ( node . hasAttribute ( 'data-code-text' ) && node . getAttribute ( 'data-code-text' ) . length >= 32 ) {
92+ return NodeFilter . FILTER_ACCEPT ;
93+ }
94+ return NodeFilter . FILTER_SKIP ;
7095 }
71- textNodes . push ( treeWalker . currentNode ) ;
96+ } , false ) ;
97+
98+ // Cache all elements to be able to add elements while traversing
99+ var dataCodeTextElements = [ ] ;
100+ while ( treeWalker . nextNode ( ) ) {
101+ var element = treeWalker . currentNode ;
102+ dataCodeTextElements . push ( element ) ;
72103 }
73104
74- textNodes . forEach ( function ( node ) {
75- processTextNode ( node ) ;
105+ dataCodeTextElements . forEach ( function ( element ) {
106+ processElementWithCodeTextAttribute ( element ) ;
76107 } ) ;
77108 }
78109
110+ // Function to process an element with data-code-text attribute
111+ // Tag elements need to be added in between the attributes to give them a special style
112+ function processElementWithCodeTextAttribute ( element ) {
113+ var originalContent = element . getAttribute ( 'data-code-text' ) ;
114+ var matches = originalContent . match ( guidRegex ) ;
115+ var currentIndex = 0 ;
116+ if ( matches ) {
117+ for ( var i = 0 ; i < matches . length ; i ++ ) {
118+ var match = matches [ i ] ;
119+ if ( match === invalidGuid ) {
120+ continue ;
121+ }
122+
123+ var replacement = guidLookup [ match ] ;
124+ var tagText = replacement ? replacement . fileName : "No matching GUID found" ;
125+
126+ matchCount ++ ;
127+ var index = originalContent . indexOf ( match , currentIndex ) ;
128+ var upToMatch = originalContent . substring ( currentIndex , index + match . length ) ;
129+ var afterMatch = originalContent . substring ( index + match . length ) ;
130+ currentIndex = index + match . length ;
131+
132+ var upToMatchElement = element . cloneNode ( true ) ;
133+ upToMatchElement . setAttribute ( 'data-code-text' , upToMatch ) ;
134+ element . parentNode . insertBefore ( upToMatchElement , element ) ;
135+ var tagElement = document . createElement ( 'span' ) ;
136+ tagElement . innerHTML = '[' + tagText + ']' ;
137+ tagElement . classList . add ( 'guidResolverTag' ) ;
138+ upToMatchElement . parentNode . insertBefore ( tagElement , upToMatchElement . nextSibling ) ;
139+ element . setAttribute ( 'data-code-text' , afterMatch ) ;
140+ }
141+ }
142+ }
143+
144+
79145 // measure time
80146 var startTime = performance . now ( ) ;
81147
@@ -87,6 +153,8 @@ function runGuidReplacer() {
87153
88154 // Traverse all text nodes in the document body
89155 traverseTextNodes ( document . body ) ;
156+ traverseDataCodeTextElements ( document . body ) ;
157+
90158 console . log ( "Found " + matchCount + " matches" + " in " + nodeCount + " nodes (duration: " + ( performance . now ( ) - startTime ) + " ms)" ) ;
91159}
92160
0 commit comments