Skip to content

πŸ“š Configure Mkdocs and Auto deploy to Github.ioΒΆ

SetupΒΆ

Mkdocs can be setup from https://squidfunk.github.io/mkdocs-material/getting-started/#with-docker either cloning repo or using docker

Text Only
git clone https://github.com/squidfunk/mkdocs-material.git

Note: For mac docker

Text Only
docker pull ghcr.io/afritzler/mkdocs-material

Template mkdocs is published at https://padamshrestha.github.io/mkdocs-material/

Add Access Token to the repoΒΆ

Create a new token with proper access and set it Secrets which will be used by the deployment or ci script

Repository can have it's own site at github.io if it's public repo.

Enable git actionsΒΆ

Enable git actions to auto trigger ci

For gh-pages deployment for public repositoryΒΆ

Add same-gh-deployment.yml ci configuration file at .github/workflows

Text Only
name: same-gh-deployment
on:
  push:
    branches:
      - master 
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: 3.x
      - name: Install requirements
        run: pip install -r requirements.txt
      - name: Install plugins
        run: pip install -r requirements-plugins.txt
      - name: Deploy
        run: mkdocs gh-deploy --force

For gh-pages deployment from private repository to public gh-pagesΒΆ

  • Repository containing code will be private.
  • Create public repository with gh-pages enabled
  • Add remote-gh-deployment.yml ci configuration file at .github/workflows and change public repo path accordingly
    Text Only
    name: remote-gh-deployment
    on:
      push:
        branches:
          - master 
          - main
    jobs:
      deploy:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - uses: actions/setup-python@v4
            with:
              python-version: 3.x
          - name: Install requirements
            run: pip install -r requirements.txt
          - name: Install plugins
            run: pip install -r requirements-plugins.txt
    
          - name: Build, init new repo in site folder and commit generated files        
            run: |
              mkdocs build
              cd site
              git init
              git add -A
              git config --local user.name "padamshrestha"
              git config --local user.email "padamshres@gmail.com"
              git commit -m 'deploy'
              git push -f https://$USERNAME:$ACTION_TOKEN@github.com/padamshrestha/mkdocs-material.git master:gh-pages
            env:
              ACTION_TOKEN: ${{ secrets.DEPLOY_ACTION_TOKEN }}
              USERNAME: padamshrestha
    
  • With the configuration, when new code gets pushed private repo, compiled mkdocs will be deployed to public gh-pages. The site takes a few mins to reflect new changes.

Gitlab pagesΒΆ

Gitlab has default private pages for the repo at https://user_name.gitlab.com/repo_name

Here is the ci script for gitlab

.gitlab-cli.yml
image: python:3.11.0
pages:
  stage: deploy
  only:
    - master 
    - main
  before_script:
    - pip install -r requirements.txt
    - pip install -r requirements-plugins.txt
  script:
    - pip install mkdocs-material
    - mkdocs build --site-dir public
  artifacts:
    paths:
      - public

Setting for markdown-paste-imageΒΆ

JSON
"MarkdownPaste.path": "../assets/images/"

Customize templatesΒΆ

Add custom directory to hold the template files so update mkdocs.yml file

Markdown
theme:
  name: material
  custom_dir: overrides
Get a copy of html from github repo for modification and add that to
Markdown
β”œβ”€ docs
β”œβ”€ overrides/
β”‚  └─ partials/
β”‚     └─ header.html
└─ mkdocs.yml

New elements can be added to header.html and any custom css or js has to be included in mkdocs.yml Add css and js

Markdown
β”œβ”€ docs
|   β”œβ”€ assets
          β”œβ”€ css
              extra.css
          β”œβ”€ js
              extra.js
└─ mkdocs.yml

and provide the references in mkdocs.yml

Markdown
# css
extra_css:
  - assets/css/custom.css
  - assets/css/extra.css

# js
extra_javascript:
  - assets/js/extra.js

Image Paste shortcutΒΆ

MacΒΆ

  • Copy - Ctrl+Option+s

  • Paste - Ctrl+Option+v

Win VDI with PicPick hot keys remapped and capture default to clipboardΒΆ

  • Copy - Shift+Alt+s
  • Paste - Ctrl+Option+v

Convert jupytext to markdownΒΆ

Text Only
jupytext --to markdown notebook.ipynb           # convert notebook.ipynb to a .md file
jupytext --to markdown notebook.py              # convert .py to a .md file
jupytext --to py notebook.ipynb                 # convert notebook.ipynb to a .py file