mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-06 15:07:52 +08:00
Expand relative URLs in HTML elements in Markdown (#1491)
This commit is contained in:
parent
2063cf9485
commit
ba193be13b
1 changed files with 24 additions and 18 deletions
|
@ -58,10 +58,10 @@ class Markdown {
|
||||||
.use(remarkMath)
|
.use(remarkMath)
|
||||||
.use(remarkPrepareMermaid)
|
.use(remarkPrepareMermaid)
|
||||||
.use(remarkSyntaxHiglight, { highlight })
|
.use(remarkSyntaxHiglight, { highlight })
|
||||||
.use(remarkExpandUrls, { baseUrl: this.baseUrl })
|
|
||||||
// We keep the HTML nodes, parse with rehype-raw and then sanitize
|
// We keep the HTML nodes, parse with rehype-raw and then sanitize
|
||||||
.use(remarkRehype, { allowDangerousHtml: true })
|
.use(remarkRehype, { allowDangerousHtml: true })
|
||||||
.use(rehypeRaw)
|
.use(rehypeRaw)
|
||||||
|
.use(rehypeExpandUrls, { baseUrl: this.baseUrl })
|
||||||
.use(rehypeSanitize, sanitizeSchema())
|
.use(rehypeSanitize, sanitizeSchema())
|
||||||
.use(rehypeKatex)
|
.use(rehypeKatex)
|
||||||
.use(rehypeMermaid)
|
.use(rehypeMermaid)
|
||||||
|
@ -139,32 +139,38 @@ function remarkSyntaxHiglight(options) {
|
||||||
|
|
||||||
// Expands relative URLs against the given base url
|
// Expands relative URLs against the given base url
|
||||||
// and deals with ".." in URLs
|
// and deals with ".." in URLs
|
||||||
function remarkExpandUrls(options) {
|
function rehypeExpandUrls(options) {
|
||||||
return (ast) => {
|
return (ast) => {
|
||||||
if (options.baseUrl) {
|
if (options.baseUrl) {
|
||||||
visit(ast, "link", (node) => {
|
visit(ast, "element", (node) => {
|
||||||
if (
|
if (node.tagName === "a" && node.properties) {
|
||||||
node.url &&
|
const url = node.properties.href;
|
||||||
!isAbsoluteUrl(node.url) &&
|
|
||||||
!isInternalUrl(node.url) &&
|
|
||||||
!isPageAnchor(node.url)
|
|
||||||
) {
|
|
||||||
node.url = urlAppend(options.baseUrl, node.url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
visit(ast, "image", (node) => {
|
if (
|
||||||
if (node.url && !isAbsoluteUrl(node.url) && !isInternalUrl(node.url)) {
|
url &&
|
||||||
node.url = urlAppend(options.baseUrl, node.url);
|
!isAbsoluteUrl(url) &&
|
||||||
|
!isInternalUrl(url) &&
|
||||||
|
!isPageAnchor(url)
|
||||||
|
) {
|
||||||
|
node.properties.href = urlAppend(options.baseUrl, url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.tagName === "img" && node.properties) {
|
||||||
|
const url = node.properties.src;
|
||||||
|
|
||||||
|
if (url && !isAbsoluteUrl(url) && !isInternalUrl(url)) {
|
||||||
|
node.properties.src = urlAppend(options.baseUrl, url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Browser normalizes URLs with ".." so we use a "__parent__"
|
// Browser normalizes URLs with ".." so we use a "__parent__"
|
||||||
// modifier instead and handle it on the server
|
// modifier instead and handle it on the server
|
||||||
visit(ast, "link", (node) => {
|
visit(ast, "element", (node) => {
|
||||||
if (node.url) {
|
if (node.tagName === "a" && node.properties && node.properties.href) {
|
||||||
node.url = node.url
|
node.properties.href = node.properties.href
|
||||||
.split("/")
|
.split("/")
|
||||||
.map((part) => (part === ".." ? "__parent__" : part))
|
.map((part) => (part === ".." ? "__parent__" : part))
|
||||||
.join("/");
|
.join("/");
|
||||||
|
|
Loading…
Reference in a new issue