Skip to content

Commit d2618c5

Browse files
committed
resolves #62 convert remote AsciiDoc to browser guide
1 parent 4b654eb commit d2618c5

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

api/src/index.js

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import neo4j from "neo4j-driver";
55
import { makeAugmentedSchema } from "neo4j-graphql-js";
66
import { GraphQLUpload, graphqlUploadExpress } from "graphql-upload";
77
import Asciidoctor from "asciidoctor";
8+
import fetch from "node-fetch";
89
import dotenv from "dotenv";
910

1011
import * as Sentry from "@sentry/node";
@@ -29,6 +30,8 @@ import * as imagesTypes from "./images/types";
2930
import { getGraphGistBySlug, getGraphGistByUUID } from "./graphgists/utils";
3031

3132
dotenv.config();
33+
const adoc = Asciidoctor();
34+
const GITHUB_FILE_RX = /^https:\/\/github.com\/(?<org>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<branch>[^\/]+)\/(?<path>.*)/
3235

3336
/*
3437
* Create a Neo4j driver instance to connect to the database
@@ -157,26 +160,71 @@ const path = "/graphql";
157160
*/
158161
server.applyMiddleware({ app, path });
159162

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+
160220
app.get("/graph_gists/:slug/graph_guide", function (req, res) {
161221
const session = driver.session();
162222
const txc = session.beginTransaction();
163223
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)
166225
res.render("index", {
167226
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
180228
});
181229
}).catch((error) => {
182230
console.error(error);

api/views/400.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h3><code>400</code> Bad Request</h3>
2+
<pre><%= error %></pre>

api/views/block_listing.html.ejs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
<%
44
var nowrap = node.getAttribute('nowrap') || opts.nowrap;
5-
var graphGist = node.getDocument().getAttribute('graphgist');
6-
var {uuid} = graphGist;
5+
var graphGistId = node.getDocument().getAttribute('graphgistid');
76
var {style, language, sourceHighter, title} = node.getAttributes();
87
%>
98

10-
<div id="<%= uuid && uuid %>" class="listingblock">
9+
<div id="<%= graphGistId && graphGistId %>" class="listingblock">
1110
<% if (title) { %>
1211
<div class="title"><%= title %></div>
1312
<% } %>

0 commit comments

Comments
 (0)