feat(tools/cheddar): Add endpoint for Markdown rendering
Similar to the source code highlighting endpoint, but for Markdown. This is to be used by the bug tracker, as well as Sourcegraph in the future. Change-Id: I4bea5c46ba969ba9965b61409e1c19c2edf1246c Reviewed-on: https://cl.tvl.fyi/c/depot/+/1424 Tested-by: BuildkiteCI Reviewed-by: glittershark <grfn@gws.fyi>
This commit is contained in:
parent
a12b0efc9e
commit
6936ee40af
1 changed files with 56 additions and 26 deletions
|
@ -3,7 +3,7 @@ use comrak::arena_tree::Node;
|
|||
use comrak::nodes::{Ast, AstNode, NodeCodeBlock, NodeHtmlBlock, NodeValue};
|
||||
use comrak::{format_html, parse_document, Arena, ComrakOptions};
|
||||
use lazy_static::lazy_static;
|
||||
use rouille::try_or_400;
|
||||
use rouille::{router, try_or_400};
|
||||
use rouille::Response;
|
||||
use serde::Deserialize;
|
||||
use std::cell::RefCell;
|
||||
|
@ -283,10 +283,9 @@ fn format_code<R: BufRead, W: Write>(
|
|||
writeln!(writer, "</pre>").expect("write should not fail");
|
||||
}
|
||||
|
||||
// Starts a Sourcegraph-compatible syntax highlighting server. This
|
||||
// Server endpoint for rendering the syntax of source code. This
|
||||
// replaces the 'syntect_server' component of Sourcegraph.
|
||||
fn highlighting_server(listen: &str) {
|
||||
println!("Starting syntax highlighting server on '{}'", listen);
|
||||
fn code_endpoint(request: &rouille::Request) -> rouille::Response {
|
||||
#[derive(Deserialize)]
|
||||
struct SourcegraphQuery {
|
||||
filepath: String,
|
||||
|
@ -294,11 +293,7 @@ fn highlighting_server(listen: &str) {
|
|||
code: String,
|
||||
}
|
||||
|
||||
// Sourcegraph only uses a single endpoint, so we don't attempt to
|
||||
// deal with routing here for now.
|
||||
rouille::start_server(listen, move |request| {
|
||||
let query: SourcegraphQuery = try_or_400!(rouille::input::json_input(request));
|
||||
println!("Handling highlighting request for '{}'", query.filepath);
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
|
||||
// We don't use syntect with the sourcegraph themes bundled
|
||||
|
@ -320,6 +315,41 @@ fn highlighting_server(listen: &str) {
|
|||
"is_plaintext": false,
|
||||
"data": String::from_utf8_lossy(&buf)
|
||||
}))
|
||||
}
|
||||
|
||||
// Server endpoint for rendering a Markdown file.
|
||||
fn markdown_endpoint(request: &rouille::Request) -> rouille::Response {
|
||||
let mut texts: HashMap<String, String> =
|
||||
try_or_400!(rouille::input::json_input(request));
|
||||
|
||||
for text in texts.values_mut() {
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
format_markdown(&mut text.as_bytes(), &mut buf);
|
||||
*text = String::from_utf8_lossy(&buf).to_string();
|
||||
}
|
||||
|
||||
Response::json(&texts)
|
||||
}
|
||||
|
||||
fn highlighting_server(listen: &str) {
|
||||
println!("Starting syntax highlighting server on '{}'", listen);
|
||||
|
||||
rouille::start_server(listen, move |request| {
|
||||
router!(request,
|
||||
// Markdown rendering route
|
||||
(POST) (/markdown) => {
|
||||
markdown_endpoint(request)
|
||||
},
|
||||
|
||||
// Code rendering route
|
||||
(POST) (/) => {
|
||||
code_endpoint(request)
|
||||
},
|
||||
|
||||
_ => {
|
||||
rouille::Response::empty_404()
|
||||
},
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue