How can I sync documentation with Github Pages?

后端 未结 10 1333
鱼传尺愫
鱼传尺愫 2020-12-12 10:14

I have a project together with several people and we have a README.md file with a bunch of GitHub Flavored Markdown that is rendered on our GitHub page. We also

10条回答
  •  轮回少年
    2020-12-12 10:25

    It's not hard, two copy and pastes into the terminal and you are all set.

    Jekyll allows you to import your markdown file, and then it will take care of converting them into HTML. The trick is to import your README.md into your index.md file with {% include_relative README.md %}. Here is how we can do that:

    It's worth checking out how to setup a super barebones Jekyll site on github (it's just two files!)

    The setup

    You can copy the two files and have your page going with your current readme by just running this one time setup (copy the whole code block and pase into the terminal) :

    # Copy our two files to the gh-pages branch
    git checkout -b gh-pages &&
    wget https://raw.githubusercontent.com/lazamar/barebones-jekyll-project-readme/master/_config.yml &&
    wget https://raw.githubusercontent.com/lazamar/barebones-jekyll-project-readme/master/index.md &&
    #
    # Commit and publish our page on github
    git add -A && git commit -m "Create project github page" &&
    git push --set-upstream origin gh-pages |
    #
    git checkout master # go back to master branch
    

    Automating

    Then we just need to automate the task of copying all changes from master to the gh-pages branch before every push. We can do that by running this script (you can copy and paste it into the terminal)

    $(cat > .git/hooks/pre-push << EOF
    #!/bin/sh
    we_are_in_gh_pages="\$(git branch | grep -G "* gh-pages")"
    
    if [ ! "\$we_are_in_gh_pages" ];
      then
        git checkout gh-pages &&
        git rebase master &&
        git push -f &&
        git checkout master # go back to master branch
    fi
    EOF
    ) && chmod 775 .git/hooks/pre-push
    

    It will create a push hook that will copy all changes from the master branch to gh-pages every time you run git push.

    That's it. Done.

提交回复
热议问题