Skip to content

Commit

Permalink
resolves #62 convert remote AsciiDoc to browser guide
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jun 13, 2022
1 parent 4b654eb commit d2618c5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
76 changes: 62 additions & 14 deletions api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import neo4j from "neo4j-driver";
import { makeAugmentedSchema } from "neo4j-graphql-js";
import { GraphQLUpload, graphqlUploadExpress } from "graphql-upload";
import Asciidoctor from "asciidoctor";
import fetch from "node-fetch";
import dotenv from "dotenv";

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

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

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

function convertToBrowserGuide (content, id) {
return adoc.convert(content, {
attributes: {
"graphGistId": id,
"env-guide": true,
"experimental": true
},
header_footer: true,
catalog_assets: true,
safe: 0,
template_dir: 'views',
template_cache: false,
})
}

app.get("/browser_guide", async function (req, res) {
try {
if ("source" in req.query) {
let url = req.query.source
const githubFileMatch = url.match(GITHUB_FILE_RX)
if (githubFileMatch) {
const { org, repo, branch, path } = githubFileMatch.groups
url = `https://raw.githubusercontent.com/${org}/${repo}/${branch}/${path}`
}
const response = await fetch(url)
const text = await response.text()
const html = convertToBrowserGuide(text, "1")
res.render("index", {
title: "Browser Guide",
html
});
} else {
res.render("400", { error: new Error("source parameter is mandatory") });
}
} catch (error) {
console.error(error);
res.render("400", { error });
}
});

app.get("/browser_guide/github/:org/:repo/:branch/:path", async function (req, res) {
try {
const { org, repo, branch, path } = req.params
const url = `https://raw.githubusercontent.com/${org}/${repo}/${branch}/${path}`
const response = await fetch(url)
const text = await response.text()
const html = convertToBrowserGuide(text, "1")
res.render("index", {
title: "Browser Guide",
html
});
} catch (error) {
console.error(error);
res.render("400", { error });
}
});

app.get("/graph_gists/:slug/graph_guide", function (req, res) {
const session = driver.session();
const txc = session.beginTransaction();
getGraph(req.params.slug, txc).then((graph) => {
//const adocText =graph.asciidoc.replaceAll(/^=+ /, '== ')
const adoc = Asciidoctor();
const html = convertToBrowserGuide(graph.asciidoc, graph.uuid)
res.render("index", {
title: graph.title,
html: adoc.convert(graph.asciidoc, {
attributes: {
"graphGist": graph,
"env-guide": true,
"experimental": true
},
header_footer: true,
catalog_assets: true,
safe: 0,
template_dir: 'views',
template_cache: false,
})
html
});
}).catch((error) => {
console.error(error);
Expand Down
2 changes: 2 additions & 0 deletions api/views/400.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h3><code>400</code> Bad Request</h3>
<pre><%= error %></pre>
5 changes: 2 additions & 3 deletions api/views/block_listing.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

<%
var nowrap = node.getAttribute('nowrap') || opts.nowrap;
var graphGist = node.getDocument().getAttribute('graphgist');
var {uuid} = graphGist;
var graphGistId = node.getDocument().getAttribute('graphgistid');
var {style, language, sourceHighter, title} = node.getAttributes();
%>

<div id="<%= uuid && uuid %>" class="listingblock">
<div id="<%= graphGistId && graphGistId %>" class="listingblock">
<% if (title) { %>
<div class="title"><%= title %></div>
<% } %>
Expand Down

0 comments on commit d2618c5

Please sign in to comment.