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::nodes::{Ast, AstNode, NodeCodeBlock, NodeHtmlBlock, NodeValue};
|
||||||
use comrak::{format_html, parse_document, Arena, ComrakOptions};
|
use comrak::{format_html, parse_document, Arena, ComrakOptions};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use rouille::try_or_400;
|
use rouille::{router, try_or_400};
|
||||||
use rouille::Response;
|
use rouille::Response;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
@ -283,10 +283,9 @@ fn format_code<R: BufRead, W: Write>(
|
||||||
writeln!(writer, "</pre>").expect("write should not fail");
|
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.
|
// replaces the 'syntect_server' component of Sourcegraph.
|
||||||
fn highlighting_server(listen: &str) {
|
fn code_endpoint(request: &rouille::Request) -> rouille::Response {
|
||||||
println!("Starting syntax highlighting server on '{}'", listen);
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct SourcegraphQuery {
|
struct SourcegraphQuery {
|
||||||
filepath: String,
|
filepath: String,
|
||||||
|
@ -294,32 +293,63 @@ fn highlighting_server(listen: &str) {
|
||||||
code: String,
|
code: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sourcegraph only uses a single endpoint, so we don't attempt to
|
let query: SourcegraphQuery = try_or_400!(rouille::input::json_input(request));
|
||||||
// deal with routing here for now.
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
rouille::start_server(listen, move |request| {
|
|
||||||
let query: SourcegraphQuery = try_or_400!(rouille::input::json_input(request));
|
// We don't use syntect with the sourcegraph themes bundled
|
||||||
println!("Handling highlighting request for '{}'", query.filepath);
|
// currently, so let's fall back to something that is kind of
|
||||||
|
// similar (tm).
|
||||||
|
let theme = &THEMES.themes[match query.theme.as_str() {
|
||||||
|
"Sourcegraph (light)" => "Solarized (light)",
|
||||||
|
_ => "Solarized (dark)",
|
||||||
|
}];
|
||||||
|
|
||||||
|
format_code(
|
||||||
|
theme,
|
||||||
|
&mut query.code.as_bytes(),
|
||||||
|
&mut buf,
|
||||||
|
&query.filepath,
|
||||||
|
);
|
||||||
|
|
||||||
|
Response::json(&json!({
|
||||||
|
"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();
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
|
format_markdown(&mut text.as_bytes(), &mut buf);
|
||||||
|
*text = String::from_utf8_lossy(&buf).to_string();
|
||||||
|
}
|
||||||
|
|
||||||
// We don't use syntect with the sourcegraph themes bundled
|
Response::json(&texts)
|
||||||
// currently, so let's fall back to something that is kind of
|
}
|
||||||
// similar (tm).
|
|
||||||
let theme = &THEMES.themes[match query.theme.as_str() {
|
|
||||||
"Sourcegraph (light)" => "Solarized (light)",
|
|
||||||
_ => "Solarized (dark)",
|
|
||||||
}];
|
|
||||||
|
|
||||||
format_code(
|
fn highlighting_server(listen: &str) {
|
||||||
theme,
|
println!("Starting syntax highlighting server on '{}'", listen);
|
||||||
&mut query.code.as_bytes(),
|
|
||||||
&mut buf,
|
|
||||||
&query.filepath,
|
|
||||||
);
|
|
||||||
|
|
||||||
Response::json(&json!({
|
rouille::start_server(listen, move |request| {
|
||||||
"is_plaintext": false,
|
router!(request,
|
||||||
"data": String::from_utf8_lossy(&buf)
|
// 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