pub struct Server { /* private fields */ }
Expand description
Markdown preview server.
Listens for HTTP connections and serves a page containing a live markdown preview. The page contains JavaScript to open a websocket connection back to the server for rendering updates.
Implementations
sourceimpl Server
impl Server
sourcepub fn bind(addr: impl ToSocketAddrs) -> Result<Self>
pub fn bind(addr: impl ToSocketAddrs) -> Result<Self>
Binds the server to a specified address.
Binding on port 0 will request a port assignment from the OS. Use addr()
to query the
assigned port.
sourcepub fn addr(&self) -> SocketAddr
pub fn addr(&self) -> SocketAddr
Returns the socket address that the server is listening on.
sourcepub fn send(&mut self, markdown: String) -> Result<()>
pub fn send(&mut self, markdown: String) -> Result<()>
Publish new markdown to be rendered by the server.
The new HTML will be sent to all connected websocket clients.
Errors
This method forwards errors from an external renderer, if set. Otherwise, the method is infallible.
sourcepub fn set_static_root(&mut self, root: impl Into<PathBuf>)
pub fn set_static_root(&mut self, root: impl Into<PathBuf>)
Set the directory that static files will be served from.
This can be thought of as the “working directory” of the server. Any HTTP requests with non-root paths will be joined to this folder and used to serve files from the filesystem. Typically this is used to serve image links relative to the markdown file.
By default, the server will not serve static files.
sourcepub fn set_highlight_theme(&mut self, theme: String)
pub fn set_highlight_theme(&mut self, theme: String)
Set the highlight.js theme used for code blocks.
Defaults to “github”.
sourcepub fn set_custom_css(&mut self, stylesheets: Vec<String>) -> Result<()>
pub fn set_custom_css(&mut self, stylesheets: Vec<String>) -> Result<()>
Set custom CSS links and files to be served with the rendered HTML.
Accepts URLs and absolute paths. URLs will be inserted as <link>
tags. The contents of
the paths will be read from disk and served in <style>
tags.
sourcepub fn set_external_renderer(&mut self, command: Command)
pub fn set_external_renderer(&mut self, command: Command)
Set an external program to use for rendering the markdown.
By default, aurelius uses pulldown_cmark
to render markdown in-process.
pulldown-cmark
is an extremely fast, CommonMark-compliant parser that is sufficient
for most use-cases. However, other markdown renderers may provide additional features.
The Command
supplied to this function should expect markdown on stdin and print HTML on
stdout.
Example
To use pandoc
to render markdown:
use std::process::Command;
use aurelius::Server;
let mut server = Server::bind("localhost:0")?;
let mut pandoc = Command::new("pandoc");
pandoc.args(&["-f", "markdown", "-t", "html"]);
server.set_external_renderer(pandoc);
sourcepub fn open_browser(&self) -> Result<()>
pub fn open_browser(&self) -> Result<()>
Opens the user’s default browser with the server’s URL in the background.
This function uses platform-specific utilities to determine the browser. The following platforms are supported:
Platform | Program |
---|---|
Linux | xdg-open |
OS X | open -g |
Windows | explorer |
sourcepub fn open_specific_browser(&self, command: Command) -> Result<()>
pub fn open_specific_browser(&self, command: Command) -> Result<()>
Opens a browser with a specified command. The HTTP address of the server will be appended to the command as an argument.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl !UnwindSafe for Server
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more