markio v1.0.3
Markio
Markio is a custom Markdown rendering package for Express that can handle Markdown input from a file or a block of text in the main file.
Installation
You can install Markio using NPM:
npm install markioUsage To use Markio in an Express application, first require the package and add the markioMiddleware middleware to your application:
const express = require("express");
const markio = require("markio");
const app = express();
app.use(markio.markioMiddleware);
app.get("/", (req, res) => {
res.locals.data = "# Hello, world!";
res.send(markio.renderMarkdown(res.locals.data));
});
app.get("/file", (req, res) => {
res.locals.data = { filename: "example.md" };
const markdown = "## " + fs.readFileSync(res.locals.data.filename, "utf8");
res.send(markio.renderMarkdown(markdown));
});
app.listen(3000, () => {
console.log("Server listening on port 3000");
});In this example, we're setting the data variable in res.locals to a Markdown string in the "/" route handler, while in the "/file" route handler, we're setting it to an object with a filename property that points to a Markdown file.
When the markioMiddleware middleware is called for a GET request with the format query parameter set to md, it will check the type of res.locals.data and render the corresponding Markdown input.
API
Markio exports two functions:
renderMarkdown(input: string): string
Renders the given Markdown input string and returns the resulting HTML string.
markioMiddleware(req: Request, res: Response, next: NextFunction): void
Express middleware that checks if the format query parameter is set to md and renders the corresponding Markdown input in the response. If res.locals.data is a string, it is assumed to contain Markdown input. If it is an object with a filename property, the contents of the file with the specified filename are read and assumed to contain Markdown input. If res.locals.data is not a string or an object with a filename property, the next middleware in the chain is called.
License
Markio is released under the ISC License. See LICENSE for details.