@@ -25,7 +25,6 @@ const common = require('./common.js');
2525const fs = require ( 'fs' ) ;
2626const marked = require ( 'marked' ) ;
2727const path = require ( 'path' ) ;
28- const preprocess = require ( './preprocess.js' ) ;
2928const typeParser = require ( './type-parser.js' ) ;
3029
3130module . exports = toHTML ;
@@ -42,76 +41,36 @@ marked.setOptions({
4241 renderer : renderer
4342} ) ;
4443
45- // TODO(chrisdickinson): never stop vomiting / fix this.
46- const gtocPath = path . resolve ( path . join (
47- __dirname ,
48- '..' ,
49- '..' ,
50- 'doc' ,
51- 'api' ,
52- '_toc.md'
53- ) ) ;
54- var gtocLoading = null ;
55- var gtocData = null ;
44+ const docPath = path . resolve ( __dirname , '..' , '..' , 'doc' ) ;
45+
46+ const gtocPath = path . join ( docPath , 'api' , '_toc.md' ) ;
47+ const gtocMD = fs . readFileSync ( gtocPath , 'utf8' ) . replace ( / ^ @ \/ \/ .* $ / gm, '' ) ;
48+ const gtocHTML = marked ( gtocMD ) . replace (
49+ / < a h r e f = " ( .* ?) " / g,
50+ ( all , href ) => `<a class="nav-${ toID ( href ) } " href="${ href } "`
51+ ) ;
52+
53+ const templatePath = path . join ( docPath , 'template.html' ) ;
54+ const template = fs . readFileSync ( templatePath , 'utf8' ) ;
55+
5656var docCreated = null ;
5757var nodeVersion = null ;
5858
5959/**
60- * opts: input, filename, template, nodeVersion.
60+ * opts: input, filename, nodeVersion.
6161 */
6262function toHTML ( opts , cb ) {
63- const template = opts . template ;
64-
6563 nodeVersion = opts . nodeVersion || process . version ;
6664 docCreated = opts . input . match ( DOC_CREATED_REG_EXP ) ;
6765
68- if ( gtocData ) {
69- return onGtocLoaded ( ) ;
70- }
71-
72- if ( gtocLoading === null ) {
73- gtocLoading = [ onGtocLoaded ] ;
74- return loadGtoc ( function ( err , data ) {
75- if ( err ) throw err ;
76- gtocData = data ;
77- gtocLoading . forEach ( function ( xs ) {
78- xs ( ) ;
79- } ) ;
80- } ) ;
81- }
82-
83- if ( gtocLoading ) {
84- return gtocLoading . push ( onGtocLoaded ) ;
85- }
86-
87- function onGtocLoaded ( ) {
88- const lexed = marked . lexer ( opts . input ) ;
89- fs . readFile ( template , 'utf8' , function ( er , template ) {
90- if ( er ) return cb ( er ) ;
91- render ( {
92- lexed : lexed ,
93- filename : opts . filename ,
94- template : template ,
95- nodeVersion : nodeVersion ,
96- analytics : opts . analytics ,
97- } , cb ) ;
98- } ) ;
99- }
100- }
101-
102- function loadGtoc ( cb ) {
103- fs . readFile ( gtocPath , 'utf8' , function ( err , data ) {
104- if ( err ) return cb ( err ) ;
105-
106- preprocess ( gtocPath , data , function ( err , data ) {
107- if ( err ) return cb ( err ) ;
108-
109- data = marked ( data ) . replace ( / < a h r e f = " ( .* ?) " / gm, function ( a , m ) {
110- return `<a class="nav-${ toID ( m ) } " href="${ m } "` ;
111- } ) ;
112- return cb ( null , data ) ;
113- } ) ;
114- } ) ;
66+ const lexed = marked . lexer ( opts . input ) ;
67+ render ( {
68+ lexed : lexed ,
69+ filename : opts . filename ,
70+ template : template ,
71+ nodeVersion : nodeVersion ,
72+ analytics : opts . analytics ,
73+ } , cb ) ;
11574}
11675
11776function toID ( filename ) {
@@ -150,7 +109,7 @@ function render(opts, cb) {
150109 template = template . replace ( / _ _ T O C _ _ / g, toc ) ;
151110 template = template . replace (
152111 / _ _ G T O C _ _ / g,
153- gtocData . replace ( `class="nav-${ id } ` , `class="nav-${ id } active` )
112+ gtocHTML . replace ( `class="nav-${ id } ` , `class="nav-${ id } active` )
154113 ) ;
155114
156115 if ( opts . analytics ) {
0 commit comments