2019-01-04 18:14:53 +00:00
|
|
|
import MarkdownIt from "markdown-it";
|
2019-09-17 01:48:32 +00:00
|
|
|
import MarkdownItFootnote from "markdown-it-footnote";
|
2019-06-29 22:39:04 +00:00
|
|
|
import slugify from "@sindresorhus/slugify";
|
2019-01-04 18:14:53 +00:00
|
|
|
import * as util from "./util";
|
2019-11-11 03:19:56 +00:00
|
|
|
import { URL } from "url";
|
2019-01-04 18:14:53 +00:00
|
|
|
|
|
|
|
const md = new MarkdownIt({
|
|
|
|
highlight: util.highlight,
|
2019-06-29 19:02:14 +00:00
|
|
|
html: true,
|
|
|
|
typographer: true
|
2019-01-04 18:14:53 +00:00
|
|
|
});
|
|
|
|
|
2019-09-17 01:48:32 +00:00
|
|
|
md.use(MarkdownItFootnote);
|
|
|
|
|
2019-06-29 22:39:04 +00:00
|
|
|
// Inserts heading anchors
|
|
|
|
// Based on https://github.com/valeriangalliat/markdown-it-anchor
|
2019-11-11 03:19:56 +00:00
|
|
|
md.core.ruler.push("header anchor", (state) => {
|
2019-06-29 22:39:04 +00:00
|
|
|
const tokens = state.tokens;
|
|
|
|
|
|
|
|
tokens
|
|
|
|
.filter((token) => token.type === "heading_open")
|
|
|
|
.forEach((token) => {
|
|
|
|
const index = tokens.indexOf(token);
|
|
|
|
const title = tokens[index + 1]
|
|
|
|
.children
|
|
|
|
.filter((token) => token.type === "text" || token.type == "code_inline")
|
|
|
|
.reduce((acc, t) => acc + t.content, "");
|
|
|
|
|
|
|
|
let slug = token.attrGet("id") || slugify(title);
|
|
|
|
if (token.attrGet("id") == null) {
|
|
|
|
token.attrPush(["id", slug]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const linkTokens = [
|
|
|
|
Object.assign(new state.Token("link_open", "a", 1), {
|
|
|
|
attrs: [
|
|
|
|
["class", "header-anchor"],
|
|
|
|
["href", "#" + slug],
|
2019-09-16 22:11:57 +00:00
|
|
|
["aria-hidden", "true"],
|
|
|
|
["role", "presentation"]
|
2019-06-29 22:39:04 +00:00
|
|
|
]
|
|
|
|
}),
|
|
|
|
Object.assign(new state.Token("html_block", "", 0), { content: token.markup }),
|
|
|
|
new state.Token("link_close", "a", -1),
|
|
|
|
Object.assign(new state.Token("text", "", 0), { content: " " })
|
|
|
|
];
|
|
|
|
|
|
|
|
state.tokens[index + 1].children.unshift(...linkTokens);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-11 03:19:56 +00:00
|
|
|
// Adds data-link attributes to anchor elements
|
|
|
|
const defaultRenderer = md.renderer.rules.link_open || function(tokens, index, options, env, self) {
|
|
|
|
return self.renderToken(tokens, index, options);
|
|
|
|
};
|
|
|
|
md.renderer.rules.link_open = function(tokens, index, options, env, self) {
|
|
|
|
const href = tokens[index].attrGet("href");
|
|
|
|
if (href != null) {
|
|
|
|
try {
|
|
|
|
const parsed = new URL(href);
|
2021-09-01 22:28:18 +00:00
|
|
|
let hostname = parsed.hostname;
|
|
|
|
|
|
|
|
// hide wwww from beginning of domain
|
|
|
|
if (hostname.startsWith("www.")) {
|
|
|
|
hostname = hostname.substr(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the path is /, omit it
|
2019-11-11 03:36:02 +00:00
|
|
|
const formattedPathname = parsed.pathname === "/" ? "" : parsed.pathname;
|
2021-09-01 22:28:18 +00:00
|
|
|
|
|
|
|
let formatted = `${hostname}${formattedPathname}`;
|
|
|
|
|
|
|
|
if (hostname.endsWith("youtube.com") && formattedPathname == "/watch") {
|
|
|
|
// otherwise yt video links end up as `youtube.com/watch` which looks weird
|
|
|
|
formatted += parsed.search;
|
|
|
|
}
|
|
|
|
|
2019-11-11 03:19:56 +00:00
|
|
|
if (formatted.length > 40) {
|
|
|
|
formatted = formatted.substr(0, 40) + "...";
|
|
|
|
}
|
|
|
|
tokens[index].attrPush(["data-link", formatted]);
|
|
|
|
} catch (e) {
|
2019-11-11 03:36:02 +00:00
|
|
|
tokens[index].attrPush(["data-link", href]);
|
2019-11-11 03:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultRenderer(tokens, index, options, env, self);
|
|
|
|
};
|
|
|
|
|
2019-01-04 18:14:53 +00:00
|
|
|
export function render(text: string): string {
|
|
|
|
return md.render(text);
|
2019-09-16 22:11:57 +00:00
|
|
|
}
|