include an SVG (hosted on github) in MarkDown
I know with that an image can be placed in a MD with the MD syntax of either ![Alt text](/path/to/img.jpg)
or ![Alt text](/path/to/img.jpg "Optional title")
, but I am having difficulty placing an SVG in MD where the code is hosted on Github.
Ultimately using rails3, and changing the model frequently right now, so I am using RailRoady to generate an SVG of the schema diagram of the models. I would like for that SVG to then be placed in the ReadMe.md, and be displayed. When I open the SVG file locally, it does work, so how do I get the browser to render the SVG in the MD file? Given that the code will be dynamic until it is finalized (seemingly never), hosting the SVG in a separate place seems overkill and that I am missing an approach to accomplish this.
The SVG I am trying to include is here on Github: https://github.com/specialorange/FDXCM/blob/master/Rails/fdxcm/doc/models_brief.svg
I have tried the following, with an actual image as well to verify the syntax is working, just that the SVG code isn't being rendered:
![Overview][1]
[1]: https://github.com/specialorange/FDXCM/blob/master/doc/controllers_brief.svg "Overview"
<img src="http://www.djcxy.com/uploads/images/7e/b9/7eb91070188ebcdcd2e10bb503362ec8.svg">
![Alt text](http://www.djcxy.com/uploads/images/7e/b9/7eb91070188ebcdcd2e10bb503362ec8.svg)
[Google Doc](https://docs.google.com/drawings/d/1B95ajItJTAImL2WXISX0fkBLYk3nldea4Vm9eo-VyE4/edit) :
<img src="https://docs.google.com/drawings/pub?id=117XsJ1kDyaY-n8AdPS3_8jTgMyITqaoT3-ah_BSc9YQ&w=960&h=720">
<img src="http://www.djcxy.com/uploads/images/7e/b9/7eb91070188ebcdcd2e10bb503362ec8.svg">
<img src="https://docs.google.com/drawings/d/1B95ajItJTAImL2WXISX0fkBLYk3nldea4Vm9eo-VyE4/edit">
to get the results of :
Google Doc :
The purpose of raw.github.com
is to allow users to view the contents of a file, so for text based files (SVG, JS, CSS, etc) this means you get the wrong headers and things break in the browser.
Update: Github has implemented a feature which makes it possible for SVG's to be used with the Markdown image syntax. The SVG image will be sanitized and displayed with the correct HTTP header. Certain tags (like <script>
) are removed.
To view the sanitized SVG or to achieve this effect from other places (ie from markdown files not hosted in repos on http://github.com/) simply append ?sanitize=true
to the SVG's raw URL.
See the examples below for rendering details.
Although you cannot link directly to SVG images from raw.github.com
you could put the SVG files on the gh-pages
branch (or configure master
as source for Github Pages) and link to the files from github.io
As the file you are trying to get to display seems to part of your projects documentation this might be a win-win situation
If using Github Pages is not your thing, rawgithub.com could be an option. RawGit retrieves your files and sets the correct headers for you.
As stated by AdamKatz in the comments, using a source other than github.io can introduce potentially privacy and security risks. See the answer by CiroSantilli and the answer by DavidChambers for more details.
As stated by MonsieurDart in the comments, RawGit does not work for private repos.
The issue to resolve this was opened on Github on October 13th 2015 and was resolved on August 31th 2017
Examples
I copied the SVG image from the question to a repo on github in order to create the examples below:
Linking to relative files (Works, but obviously only https://github.com/)
Code
![Alt text](./controllers_brief.svg)
<img src="./controllers_brief.svg">
Result
See the working example on github.com.
Linking to RAW files (Does not work)
Code
![Alt text](http://www.djcxy.com/uploads/images/78/55/7855aa99fa7d59ae85be824ebd503850.svg)
<img src="http://www.djcxy.com/uploads/images/78/55/7855aa99fa7d59ae85be824ebd503850.svg">
Result
Linking to RAW files using ?sanitize=true
(Works)
Code
![Alt text](http://www.djcxy.com/uploads/images/78/55/7855aa99fa7d59ae85be824ebd503850.svg?sanitize=true)
<img src="http://www.djcxy.com/uploads/images/78/55/7855aa99fa7d59ae85be824ebd503850.svg?sanitize=true">
Result
Linking to files hosted on github.io (Works)
Code
![Alt text](http://www.djcxy.com/uploads/images/04/21/0421cc400fd4bbffe1c507beab83ab91.svg)
<img src="http://www.djcxy.com/uploads/images/04/21/0421cc400fd4bbffe1c507beab83ab91.svg">
Result
Linking to RAW files using rawgithub.com (Also Works)
Note: Sometimes the RawGithub service is down/doesn't work. If you don't see an image below, that is probably the case.
Code
![Alt text](http://www.djcxy.com/uploads/images/7a/6d/7a6dcf7b7b11e32cf48cb894b7d57538.svg)
<img src="http://www.djcxy.com/uploads/images/7a/6d/7a6dcf7b7b11e32cf48cb894b7d57538.svg">
Result
I contacted GitHub to say that github.io-hosted SVGs are no longer displayed in GitHub READMEs. I received this reply:
We have had to disable svg image rendering on GitHub.com due to potential cross site scripting vulnerabilities.
rawgit.com solves this problem nicely. For each request, it retrieves the appropriate document from GitHub and, crucially, serves it with the correct Content-Type header.
链接地址: http://www.djcxy.com/p/20178.html