External markup renderers can't use in-repo images #2381

Closed
opened 2025-11-02 04:34:20 -06:00 by GiteaMirror · 2 comments
Owner

Originally created by @pjanx on GitHub (Oct 7, 2018).

  • Gitea version (or commit ref): 1.5.1
  • Git version: master
  • Operating system: Linux
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant

Description

The logic in modules/markup/markdown's (*Renderer).Image is inaccessible to external renderers. Relative image URLs pass through unchanged and wrong.

I can think of two solutions:

  • Expose urlPrefix to the process as an environment variable, such as GITEA_PREFIX_SRC=/user/repo/raw/branch/master/subdir/ and GITEA_PREFIX_RAW=/user/repo/raw/branch/master/subdir/. Then you can at least wrap external tools in scripts; asciidoctor just needs to have the imagesdir attribute set appropriately.
  • Change all relative image srcs in a PostProcess step to point to a /raw/ location because they are pointless anyway (a brute force solution would be to use <base href=> in the header template).
Originally created by @pjanx on GitHub (Oct 7, 2018). <!-- 1. Please speak English, this is the language all of us can speak and write. 2. Please ask questions or configuration/deploy problems on our Discord server (https://discord.gg/NsatcWJ) or forum (https://discourse.gitea.io). 3. Please take a moment to check that your issue doesn't already exist. 4. Please give all relevant information below for bug reports, because incomplete details will be handled as an invalid report. --> - Gitea version (or commit ref): 1.5.1 - Git version: master - Operating system: Linux - Database (use `[x]`): - [ ] PostgreSQL - [ ] MySQL - [ ] MSSQL - [x] SQLite - Can you reproduce the bug at https://try.gitea.io: - [ ] Yes (provide example URL) - [ ] No - [x] Not relevant ## Description The logic in modules/markup/markdown's `(*Renderer).Image` is inaccessible to external renderers. Relative image URLs pass through unchanged and wrong. I can think of two solutions: - Expose `urlPrefix` to the process as an environment variable, such as `GITEA_PREFIX_SRC=/user/repo/raw/branch/master/subdir/` and `GITEA_PREFIX_RAW=/user/repo/raw/branch/master/subdir/`. Then you can at least wrap external tools in scripts; asciidoctor just needs to have the `imagesdir` attribute set appropriately. - Change all relative image `src`s in a `PostProcess` step to point to a `/raw/` location because they are pointless anyway (a brute force solution would be to use `<base href=>` in the header template).
GiteaMirror added the type/enhancement label 2025-11-02 04:34:20 -06:00
Author
Owner

@pjanx commented on GitHub (Oct 11, 2018):

Slight generalization of my workaround for nginx, works only for a project's root README:

# Circumventing Gitea issue #5031
# Must not catch POST /user/login, that is an awful surprise
set $redirectable 1;
if ($request_method !~ ^(HEAD|GET)$) {
        set $redirectable 0;
}
if ($request_uri !~ ^(/[^/]+/[^/]+)$) {
        set $redirectable 0;
}
if ($redirectable) {
        return  301 https://$host$request_uri/;
}
#!/bin/sh
external-markup-renderer-invocation | perl -pe '
    # The URL must end with a slash; since <script> does not
    # come through, I had to resolve that in nginx.conf
    s,(<img src=")([^/]*?"),$1raw/branch/master/$2,g'
RENDER_COMMAND = /path/to/that/script
@pjanx commented on GitHub (Oct 11, 2018): Slight generalization of my workaround for nginx, works only for a project's root README: ``` # Circumventing Gitea issue #5031 # Must not catch POST /user/login, that is an awful surprise set $redirectable 1; if ($request_method !~ ^(HEAD|GET)$) { set $redirectable 0; } if ($request_uri !~ ^(/[^/]+/[^/]+)$) { set $redirectable 0; } if ($redirectable) { return 301 https://$host$request_uri/; } ``` ``` #!/bin/sh external-markup-renderer-invocation | perl -pe ' # The URL must end with a slash; since <script> does not # come through, I had to resolve that in nginx.conf s,(<img src=")([^/]*?"),$1raw/branch/master/$2,g' ``` ``` RENDER_COMMAND = /path/to/that/script ```
Author
Owner

@Eisfunke commented on GitHub (Oct 22, 2018):

I noticed this too (when using Pandoc as markdown parser) and was just about to open an issue about this.

I would prefer the approach using an environment variable, as this would be more flexible. If there are no objections and I get around to it, I'll try to make a PR doing that.

Also, thanks for the workaround script!

@Eisfunke commented on GitHub (Oct 22, 2018): I noticed this too (when using Pandoc as markdown parser) and was just about to open an issue about this. I would prefer the approach using an environment variable, as this would be more flexible. If there are no objections and I get around to it, I'll try to make a PR doing that. Also, thanks for the workaround script!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#2381