diff options
| author | Jason A. Dönerfield <no-replay@sl0p.foo> | 2026-07-15 16:13:14 +0200 |
|---|---|---|
| committer | Jason A. Dönerfield <no-replay@sl0p.foo> | 2026-07-15 16:13:14 +0200 |
| commit | d191bab7341da2242c7d0be92fa01465e64757f2 (patch) | |
| tree | c7782618c78800024a71ea240ab7a6c9bc7dd485 | |
| parent | ui-summary: optionally render the repo readme on the summary page (diff) | |
| download | cgit-d191bab7341da2242c7d0be92fa01465e64757f2.tar.gz cgit-d191bab7341da2242c7d0be92fa01465e64757f2.tar.xz cgit-d191bab7341da2242c7d0be92fa01465e64757f2.zip | |
md2html: rewrite relative image links to the plain/ blob URL
A README's relative image paths (screenshots/foo.png) are repo blobs that
cgit only serves via its plain command, but a browser resolves a relative
<img src> against the about-page URL, so the image 404s. Rewrite relative
<img src> to an absolute /<repo>/plain/<path>?h=<branch> URL using the
CGIT_REPO_URL / CGIT_REPO_DEFBRANCH environment cgit gives the about-filter.
Absolute so it also works with ui-summary readme rendering.
| -rwxr-xr-x | filters/html-converters/md2html | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/filters/html-converters/md2html b/filters/html-converters/md2html index 59f43a8..de393c7 100755 --- a/filters/html-converters/md2html +++ b/filters/html-converters/md2html @@ -288,10 +288,10 @@ sys.stdout.write(HtmlFormatter(style='pastie').get_style_defs('.highlight')) sys.stdout.write(''' </style> ''') -sys.stdout.write("<div class='markdown-body'>") -sys.stdout.flush() +# Render to a string (not straight to stdout) so we can post-process it. # Note: you may want to run this through bleach for sanitization -markdown.markdownFromFile( +html = markdown.markdown( + sys.stdin.read(), output_format="html5", extensions=[ "markdown.extensions.fenced_code", @@ -301,4 +301,29 @@ markdown.markdownFromFile( TocExtension(anchorlink=True)], extension_configs={ "markdown.extensions.codehilite":{"css_class":"highlight"}}) + +# A README's relative image paths (e.g. screenshots/x.png) are repo blobs, which +# cgit only serves via its `plain` command; but a browser resolves a relative +# <img src> against the about-page URL (/<repo>/about/), so the image 404s. +# Rewrite relative <img src> to an absolute /<repo>/plain/<path>?h=<branch> URL +# (absolute so it also works where cgit renders the readme on the summary page). +# cgit exports the repo context to about-filters via these environment vars; if +# they're absent we leave the src untouched. +import os, re +repo = os.environ.get("CGIT_REPO_URL", "").strip("/") +if repo: + branch = os.environ.get("CGIT_REPO_DEFBRANCH", "").strip() + plain = "/" + repo + "/plain/" + query = ("?h=" + branch) if branch else "" + # leave absolute, protocol-relative, root-relative, anchor and data: URLs alone + skip = re.compile(r"^(?:[a-z][a-z0-9+.\-]*:|//|/|#)", re.I) + def rewrite(m): + url = m.group(2) + if not url or skip.match(url): + return m.group(0) + return m.group(1) + plain + re.sub(r"^\./", "", url) + query + m.group(3) + html = re.sub(r'(<img\b[^>]*?\bsrc=")([^"]*)(")', rewrite, html, flags=re.I) + +sys.stdout.write("<div class='markdown-body'>") +sys.stdout.write(html) sys.stdout.write("</div>") |
