Code Monkey home page Code Monkey logo

gitea-preview's Introduction

Previewing files directly from a Gitea repo

This extension lets you preview files directly from a Gitea repo, using cleaner, future-proof URL's e.g.

  • mysite.com/preview/USER/REPO/doc/
  • mysite.com/preview/USER/REPO/doc/screenshot.png
  • mysite.com/preview/USER/REPO/doc/index.html

HTML files are served using a modified version of Jerzy Głowacki's htmlpreview.github.io, adapted to work from a single pre-configured domain (so CORS is no longer an issue).

Other links are translated to their Gitea-specific URL's e.g.

mysite.com/preview/USER/REPO/doc/screenshot.png

becomes

mysite.com/USER/REPO/raw/branch/master/doc/screenshot.png

The work is done by a PHP script, so your Gitea instance needs to be fronted by nginx or Apache, and the preview URL's intercepted and redirected to the script.

Setting things up

Create a directory preview in your webroot, and save preview.php and preview-html.js there.

At the top of both files, configure the domain name your Gitea is using.

If you're using nginx, create a rewrite rule like this to catch preview requests, and send them to the PHP script:

rewrite ^/preview/(.*?)/(.*?)/(.*)$ /preview/preview.php?user=$1&repo=$2&path=$3 last ;
location /preview {
    try_files $uri $uri$is_args$args =404 ;
}

If you're using Apache, set up a .htaccess inside the preview/ directory like this:

RewriteRule ^(.+?)/(.+?)/(.+?)$ preview.php?user=$1&repo=$2&path=$3 [QSA,L]

How are branches handled?

Not very well :-( The preview.php script gets everything from master, but this can be changed by using a branch query parameter. However, AFAICT there's no way to set this when creating links.

For example, if I want to link to an HTML file from a README.md, I might write something like this:

Documentation is [here](https://mysite.com/preview/USER/REPO/doc/index.html).

However, if I branch the code, the preview will show the HTML file from the master branch, which is not what I want. I really need to write the Markdown something like this:

Documentation is [here](https://mysite.com/preview/USER/REPO/doc/index.html?branch={{CURR_BRANCH}}).

but this would require support from Gitea to provide the CURR_BRANCH value and insert it into the HTML when rendering the Markdown.

gitea-preview's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.