takladev
takladev

Reputation: 413

Github markdown load code block from a repo file

I want to add a JSON code block in my README.md and the code block needs to be loaded from the repo file.

E.g, is it possible to do the following?

```json
<<load data from res/data.json>>
```

where res/data.json is a file in the same repo? I know it is possible to have a hyperlink, but I want the JSON data to appear there directly without clicking on the hyperlink.

Upvotes: 8

Views: 5112

Answers (2)

Tom Hillman
Tom Hillman

Reputation: 327

I was also looking for a way to do this, and while I agree with Chris' analysis, you might consider github actions as a possible work-around:

https://github.com/marketplace/actions/markdown-embed-code-from-file

Upvotes: 1

Chris
Chris

Reputation: 136880

This is not possible to do with GitHub Flavored Markdown.

You could programmatically update your README.md and then commit the resulting file, though. The simplest solution would probably be to have a README-top.md containing everything up to ```json and a README-bottom.md containing ``` to the end of the file.

Then you could simply do something like

cat README-top.md res/data.json README-bottom.md > README.md

There are lots of other options, including fairly complex templating tools.

A pre-commit hook would let you automate this even further, updating your README.md every time you commit locally.

Upvotes: 0

Related Questions