9

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.

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

List of similar projects: github.com/realazthat/snipinator/#-related-projects (disclosure: I wrote snipinator).
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.

2 Comments

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 <details> tag) and showing JSON in that section.
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 about including other Markdown files in a GitHub README.md.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.