3

How can I display existing source code files within MkDocs? I want to include the files directly from another GitHub repository in code blocks without reformatting them so updated files will be shown in the MkDocs document.

sample_code.py

    def fn():
        pass
flywire
  • 1,155
  • 1
  • 14
  • 38

1 Answers1

6

Using MkDocs with Snippets extension. Snippets and/or off-line processing require files to be available locally which is explained in the Pro Git book Git Tools Submodules section.

  1. Include full file path even if the file in the same folder:

index.md

.
```python
--8<-- "docs/sample_code.py"
```
.
  1. Create the source code file:

sample_code.py

def fn():
    pass
  1. Add the extension to mkdocs configuration file:

mkdocs.yml

site_name: Demo

markdown_extensions:
    - pymdownx.snippets:

nav:
    - Demo: index.md

Output

.

def fn():
    pass

.

flywire
  • 1,155
  • 1
  • 14
  • 38