@@ -3,7 +3,7 @@ import { CheerioAPI } from 'cheerio';
33import { Element } from 'domhandler' ;
44import { getType } from '../lib/files/type.js' ;
55import { DIST_FOLDER } from '../lib/files/index.js' ;
6- import { Result } from './lib.js' ;
6+ import { isHiddenProved , Result } from './lib.js' ;
77import { replaceNode } from '../lib/html.js' ;
88
99function sendEvent ( ...args : Parameters < typeof process . send > ) {
@@ -22,7 +22,13 @@ const HOME_HTML_CONTENT = '' +
2222 '</article><div id="disqus_thread"></div ></div></section>' ;
2323
2424
25- async function fixSectionHtml ( $ : CheerioAPI , node : Element ) {
25+ function buildAnchor ( filename : string , hash : string ) {
26+ let result = '#' + filename . replace ( / \. h t m l $ / , '.md' ) ;
27+ if ( hash ) result += '-' + hash . substring ( 1 ) ;
28+ return result ;
29+ }
30+
31+ async function fixSectionHtml ( $ : CheerioAPI , node : Element , relativePath : string ) {
2632 const $node = $ ( node ) ;
2733
2834 $node . find ( '.last-modified, .navigation-links._bottom' ) . remove ( ) ;
@@ -76,18 +82,23 @@ async function fixSectionHtml($: CheerioAPI, node: Element) {
7682 $node . find ( '[id]:not(h1)' ) . each ( function ( _i , node ) {
7783 const article = $ ( node ) . closest ( '.article' ) ;
7884 const h1Id = article . find ( 'h1[id$=".md"]' ) . attr ( 'id' ) ;
79- if ( h1Id && article . length === 1 ) node . attribs . id = h1Id + '-' + node . attribs . id ;
85+ if ( h1Id && article . length === 1 ) node . attribs . id = h1Id + '-' + node . attribs . id ;
8086 } ) ;
8187 $node . find ( 'a[href]' ) . each ( function ( _i , node ) {
88+ let anchor = '' ;
89+
8290 const href = node . attribs . href ;
8391 const url = new URL ( href , 'https://kotlinlang.org/docs/' ) ;
84- if ( url . hostname === 'kotlinlang.org' && dirname ( url . pathname ) === '/docs' ) {
92+
93+ if ( href [ 0 ] === '#' ) anchor = buildAnchor ( basename ( relativePath ) , href ) ;
94+ else if ( url . hostname === 'kotlinlang.org' && dirname ( url . pathname ) === '/docs' ) {
8595 const filename = basename ( url . pathname ) ;
86- if ( filename . endsWith ( '.html' ) ) {
87- let anchor = '#' + filename . replace ( / \. h t m l $ / , '.md' )
88- if ( url . hash ) anchor += '-' + url . hash . slice ( 1 ) ;
89- node . attribs . href = anchor ;
90- }
96+ if ( filename . endsWith ( '.html' ) ) anchor = buildAnchor ( filename , url . hash ) ;
97+ }
98+
99+ if ( anchor ) {
100+ node . attribs . href = anchor ;
101+ node . attribs [ 'data-origin-href' ] = href ;
91102 }
92103 } ) ;
93104
@@ -101,7 +112,10 @@ async function onMessage(relativePath: string) {
101112 let html : string = null ;
102113
103114 const path = join ( DIST_FOLDER , relativePath ) ;
104- const [ type , $ ] = await getType ( relativePath , path ) ;
115+ let [ type , $ ] = await getType ( relativePath , path ) ;
116+
117+ if ( isHiddenProved ( type , relativePath ) )
118+ type = 'Page_Documentation' ;
105119
106120 if ( type === 'Page_Documentation' ) {
107121 const sections = $ ( 'section.panel__content' ) ;
@@ -111,7 +125,7 @@ async function onMessage(relativePath: string) {
111125 } else if ( sections . length > 0 ) {
112126 html = '' ;
113127 for ( const node of sections ) {
114- html += await fixSectionHtml ( $ , node ) ;
128+ html += await fixSectionHtml ( $ , node , relativePath ) ;
115129 }
116130 }
117131 }
0 commit comments