@@ -5,6 +5,7 @@ import neo4j from "neo4j-driver";
5
5
import { makeAugmentedSchema } from "neo4j-graphql-js" ;
6
6
import { GraphQLUpload , graphqlUploadExpress } from "graphql-upload" ;
7
7
import Asciidoctor from "asciidoctor" ;
8
+ import fetch from "node-fetch" ;
8
9
import dotenv from "dotenv" ;
9
10
10
11
import * as Sentry from "@sentry/node" ;
@@ -29,6 +30,8 @@ import * as imagesTypes from "./images/types";
29
30
import { getGraphGistBySlug , getGraphGistByUUID } from "./graphgists/utils" ;
30
31
31
32
dotenv . config ( ) ;
33
+ const adoc = Asciidoctor ( ) ;
34
+ const GITHUB_FILE_RX = / ^ h t t p s : \/ \/ g i t h u b .c o m \/ (?< org > [ ^ \/ ] + ) \/ (?< repo > [ ^ \/ ] + ) \/ b l o b \/ (?< branch > [ ^ \/ ] + ) \/ (?< path > .* ) /
32
35
33
36
/*
34
37
* Create a Neo4j driver instance to connect to the database
@@ -157,26 +160,71 @@ const path = "/graphql";
157
160
*/
158
161
server . applyMiddleware ( { app, path } ) ;
159
162
163
+ function convertToBrowserGuide ( content , id ) {
164
+ return adoc . convert ( content , {
165
+ attributes : {
166
+ "graphGistId" : id ,
167
+ "env-guide" : true ,
168
+ "experimental" : true
169
+ } ,
170
+ header_footer : true ,
171
+ catalog_assets : true ,
172
+ safe : 0 ,
173
+ template_dir : 'views' ,
174
+ template_cache : false ,
175
+ } )
176
+ }
177
+
178
+ app . get ( "/browser_guide" , async function ( req , res ) {
179
+ try {
180
+ if ( "source" in req . query ) {
181
+ let url = req . query . source
182
+ const githubFileMatch = url . match ( GITHUB_FILE_RX )
183
+ if ( githubFileMatch ) {
184
+ const { org, repo, branch, path } = githubFileMatch . groups
185
+ url = `https://raw.githubusercontent.com/${ org } /${ repo } /${ branch } /${ path } `
186
+ }
187
+ const response = await fetch ( url )
188
+ const text = await response . text ( )
189
+ const html = convertToBrowserGuide ( text , "1" )
190
+ res . render ( "index" , {
191
+ title : "Browser Guide" ,
192
+ html
193
+ } ) ;
194
+ } else {
195
+ res . render ( "400" , { error : new Error ( "source parameter is mandatory" ) } ) ;
196
+ }
197
+ } catch ( error ) {
198
+ console . error ( error ) ;
199
+ res . render ( "400" , { error } ) ;
200
+ }
201
+ } ) ;
202
+
203
+ app . get ( "/browser_guide/github/:org/:repo/:branch/:path" , async function ( req , res ) {
204
+ try {
205
+ const { org, repo, branch, path } = req . params
206
+ const url = `https://raw.githubusercontent.com/${ org } /${ repo } /${ branch } /${ path } `
207
+ const response = await fetch ( url )
208
+ const text = await response . text ( )
209
+ const html = convertToBrowserGuide ( text , "1" )
210
+ res . render ( "index" , {
211
+ title : "Browser Guide" ,
212
+ html
213
+ } ) ;
214
+ } catch ( error ) {
215
+ console . error ( error ) ;
216
+ res . render ( "400" , { error } ) ;
217
+ }
218
+ } ) ;
219
+
160
220
app . get ( "/graph_gists/:slug/graph_guide" , function ( req , res ) {
161
221
const session = driver . session ( ) ;
162
222
const txc = session . beginTransaction ( ) ;
163
223
getGraph ( req . params . slug , txc ) . then ( ( graph ) => {
164
- //const adocText =graph.asciidoc.replaceAll(/^=+ /, '== ')
165
- const adoc = Asciidoctor ( ) ;
224
+ const html = convertToBrowserGuide ( graph . asciidoc , graph . uuid )
166
225
res . render ( "index" , {
167
226
title : graph . title ,
168
- html : adoc . convert ( graph . asciidoc , {
169
- attributes : {
170
- "graphGist" : graph ,
171
- "env-guide" : true ,
172
- "experimental" : true
173
- } ,
174
- header_footer : true ,
175
- catalog_assets : true ,
176
- safe : 0 ,
177
- template_dir : 'views' ,
178
- template_cache : false ,
179
- } )
227
+ html
180
228
} ) ;
181
229
} ) . catch ( ( error ) => {
182
230
console . error ( error ) ;
0 commit comments