5

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
takladev
  • 353
  • 5
  • 16

2 Answers2

1

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

Tom Hillman
  • 327
  • 1
  • 10
0

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • pre-commit hook is an interesting idea, but it is a client-side tool and I can't expect everyone on the project to be using those hooks (given that there is no way of enforcing client-side hooks for all members i.e, members need to download the client-side hooks separately and get it installed). The problem is I have multiple users contributing to the README each having their JSONs in a separate file and need to include that JSON in the README. I was hoping to have a foldable section (which is possible in Github using
    tag) and showing JSON in that section.
    – takladev Dec 24 '20 at 04:22
  • 1
    Then I think we're back to the first line of my answer: this isn't possible with GFM. It doesn't support any kind of logic, including "includes". Here is [a similar question](https://stackoverflow.com/q/35080160/354577) about including other Markdown files in a GitHub `README.md`. – ChrisGPT was on strike Dec 24 '20 at 12:47