{"id":165780,"date":"2020-10-10T17:16:23","date_gmt":"2020-10-10T09:16:23","guid":{"rendered":"http:\/\/4563.org\/?p=165780"},"modified":"2020-10-10T17:16:23","modified_gmt":"2020-10-10T09:16:23","slug":"%e4%bd%bf%e7%94%a8-jsdelivr-%e5%85%8d%e8%b4%b9%e5%8a%a0%e9%80%9f-github-pages-%e5%8d%9a%e5%ae%a2%e7%9a%84%e9%9d%99%e6%80%81%e8%b5%84%e6%ba%90%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=165780","title":{"rendered":"\u4f7f\u7528 jsDelivr \u514d\u8d39\u52a0\u901f GitHub Pages \u535a\u5ba2\u7684\u9759\u6001\u8d44\u6e90\uff08\u4e8c\uff09"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4f7f\u7528 jsDelivr \u514d\u8d39\u52a0\u901f GitHub Pages \u535a\u5ba2\u7684\u9759\u6001\u8d44\u6e90\uff08\u4e8c\uff09               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mzlogin <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e4b\u524d\u5199\u8fc7\u4e00\u7bc7 \u4f7f\u7528 jsDelivr \u514d\u8d39\u52a0\u901f GitHub Pages \u535a\u5ba2\u7684\u9759\u6001\u8d44\u6e90\uff0c\u5728\u90a3\u4e4b\u540e\uff0c\u53c8\u9646\u7eed\u60f3\u5230\u5e76\u5b9e\u65bd\u4e86\u51e0\u70b9\u5229\u7528 jsDelivr \u8fdb\u4e00\u6b65\u52a0\u901f\u9759\u6001\u8d44\u6e90\u52a0\u8f7d\u7684\u63aa\u65bd\uff0c\u65b0\u8d77\u4e00\u7bc7\u4f5c\u4e3a\u8bb0\u5f55\u548c\u5206\u4eab\u3002<\/p>\n<p>\u7ee7\u4e0a\u4e00\u8f6e\u6539\u9020\u8fc7\u540e\uff0c\u6bd4\u8f83\u62d6\u9875\u9762\u52a0\u8f7d\u901f\u5ea6\u7684\u4e3b\u8981\u6709\u4e09\u70b9\uff1a<\/p>\n<ol>\n<li>\u9875\u9762\u9996\u4e2a\u8bf7\u6c42\u54cd\u5e94\u65f6\u95f4\uff1b<\/li>\n<li>\u56fe\u7247\u8d44\u6e90\u52a0\u8f7d\u65f6\u95f4\uff1b<\/li>\n<li>\u7ad9\u5185\u641c\u7d22\u5f15\u7528\u7684 JSON \u8d44\u6e90\u52a0\u8f7d\u65f6\u95f4\u3002<\/li>\n<\/ol>\n<p>\u7b2c 1 \u70b9\u5728\u9875\u9762\u4ecd\u7136\u6258\u7ba1\u5728 GitHub Pages \u7684\u524d\u63d0\u4e0b\uff0c\u4f3c\u4e4e\u6ca1\u6709\u4ec0\u4e48\u597d\u529e\u6cd5\u80fd\u4ea7\u751f\u8d28\u7684\u98de\u8dc3\uff1b\u672c\u7bc7\u4e3b\u8981\u6539\u5584\u4e86\u7b2c 2 \u70b9\u548c\u7b2c 3 \u70b9\u3002<\/p>\n<h2>0x01 \u56fe\u7247\u8d44\u6e90\u52a0\u901f<\/h2>\n<p>\u8fd9\u91cc\u6240\u8bf4\u7684\u56fe\u7247\u4e3b\u8981\u662f\u6307\u6587\u7ae0\u91cc\u5f15\u7528\u7684\u56fe\u7247\u3002<\/p>\n<p>\u6211\u4e00\u76f4\u5c06\u56fe\u7247\u653e\u5728\u535a\u5ba2\u6e90\u7801\u6839\u76ee\u5f55\u7684 images \u6587\u4ef6\u5939\u4e0b\uff0c\u5f15\u7528\u56fe\u7247\u7684\u4e60\u60ef\u5199\u6cd5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>![after use cdn](\/images\/posts\/github\/cdn-after.png) <\/code><\/pre>\n<p>\u5982\u679c\u60f3\u5c06\u8fd9\u4e2a\u56fe\u7247\u5730\u5740\u66ff\u6362\u4e3a jsDelivr \u7684\u5730\u5740\uff0c\u9700\u8981\u505a\u7684\u5c31\u662f\u5c06 <code>\/images<\/code> \u66ff\u6362\u4e3a <code>https:\/\/cdn.jsdelivr.net\/gh\/mzlogin\/<span class=\"__cf_email__\" data-cfemail=\"e28f988e8d858b8ccc858b968a9780cc8b8da28f8391968790\">[email&#160;protected]<\/span>\/images<\/code>\u3002<\/p>\n<p>\u4e00\u5904\u4e00\u5904\u66ff\u6362\u884c\u4e0d\u884c\uff1f\u5f53\u7136\u4e5f\u884c\uff0c\u4f46\u540e\u9762\u5199\u65b0\u6587\u7ae0\u65f6\u8981\u5f15\u7528\u56fe\u7247\uff0c\u8fd8\u5f97\u624b\u52a8\u5199\u8fd9\u4e00\u957f\u4e32\uff0c\u4e0d\u65b9\u4fbf\uff1b\u4e07\u4e00 jsDeliver \u51fa\u72b6\u51b5\uff0c\u4e5f\u4e0d\u597d\u4e00\u952e\u5207\u6362\u56de\u6765\u3002\u6709\u6ca1\u6709\u4e00\u52b3\u6c38\u9038\u7684\u65b9\u6cd5\uff1f\u5f53\u7136\u4e5f\u6709\uff0c\u6211\u4eec\u4ece Jekyll \u7684 layout \u673a\u5236\u6765\u60f3\u529e\u6cd5\u3002<\/p>\n<p>Jekyll \u7684 layout \u53ef\u4ee5\u7406\u89e3\u4e3a\u9875\u9762\u6a21\u677f\uff0c\u5b83\u662f\u53ef\u4ee5\u7ee7\u627f\u7684\uff0c\u6bd4\u5982\u6211\u7684\u535a\u5ba2\u7684\u6240\u6709\u9875\u9762\u6a21\u677f\u6709\u4e00\u4e2a\u5171\u540c\u7684\u7956\u5148\u6a21\u677f _layouts\/default.html\uff0c\u6a21\u677f\u91cc\u53ef\u4ee5\u4f7f\u7528 Liquid \u8bed\u6cd5\u5bf9\u5185\u5bb9\u8fdb\u884c\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u70b9\uff0c\u6765\u81ea\u52a8\u5b8c\u6210\u6279\u91cf\u66ff\u6362\u7684\u5de5\u4f5c\u3002<\/p>\n<p>\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>{% assign assets_base_url = site.url %} {% if site.cdn.jsdelivr.enabled %} {% assign assets_base_url = \"https:\/\/cdn.jsdelivr.net\/gh\/\" | append: site.repository | append: '@master' %} {% endif %} {% assign assets_images_url = 'src=\"' | append: assets_base_url | append: \"\/images\" %} {% include header.html %}     {{ content | replace: 'src=\"\/images', assets_images_url }} {% include footer.html %} <\/code><\/pre>\n<p>\u5927\u610f\u5c31\u662f\uff0c\u5982\u679c\u6253\u5f00\u4e86\u542f\u7528 jsDelivr \u52a0\u901f\u7684\u5f00\u5173\uff0c\u5c31\u5c06 <code>content<\/code> \u91cc\u7684 <code>src=\"\/images\"<\/code> \u66ff\u6362\u4e3a <code>src=\"https:\/\/cdn.jsdelivr.net\/gh\/mzlogin\/<span class=\"__cf_email__\" data-cfemail=\"3b564157545c5255155c524f534e591552547b565a484f5e49\">[email&#160;protected]<\/span>\/images\"<\/code>\uff0c\u5426\u5219\u66ff\u6362\u4e3a <code>src=\"https:\/\/mazhuang.org\/images\"<\/code>\u3002<\/p>\n<p>\u4ee5\u4e0a\u4fbf\u8fbe\u6210\u4e86\u6211\u4eec\u7684\u76ee\u7684\u3002<\/p>\n<h2>0x02 \u7ad9\u5185\u641c\u7d22\u5f15\u7528\u7684 JSON \u8d44\u6e90\u52a0\u901f<\/h2>\n<p>\u6211\u662f\u4f7f\u7528 Simple-Jekyll-Search \u8fd9\u4e2a JavaScript \u5e93\u6765\u5b9e\u73b0\u7ad9\u5185\u641c\u7d22\u7684\uff0c\u5b83\u7684\u641c\u7d22\u6570\u636e\u662f\u6765\u81ea\u4e00\u4e2a\u52a8\u6001\u751f\u6210\u7684 JSON \u6587\u4ef6\u3002<\/p>\n<p>\u8fd9\u4e2a JSON \u6587\u4ef6\u7f16\u8bd1\u524d\u957f\u8fd9\u6837\uff1a<\/p>\n<p>https:\/\/github.com\/mzlogin\/mzlogin.github.io\/blob\/master\/assets\/search_data.json<\/p>\n<p>Jekyll \u7f16\u8bd1\u540e\u957f\u8fd9\u6837\uff1a<\/p>\n<p>https:\/\/mazhuang.org\/assets\/search_data.json<\/p>\n<p>\u8fd9\u6837\u7684\u8d44\u6e90\u662f\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u901a\u8fc7\u66ff\u6362\u7f51\u5740\u6765\u7528 jsDelivr \u52a0\u901f\u7684\uff0c\u56e0\u4e3a jsDelivr \u4e0a\u7f13\u5b58\u7684\u662f\u7f16\u8bd1\u524d\u7684\u6587\u4ef6\uff0c\u800c\u6211\u4eec\u9700\u8981\u7684\u662f\u7f16\u8bd1\u540e\u7684\u3002<\/p>\n<p>\u90a3\u6211\u4eec\u5c31\u60f3\u529e\u6cd5\uff1a<\/p>\n<ol>\n<li>\u5c06\u535a\u5ba2\u6e90\u7801\u7f16\u8bd1\uff1b<\/li>\n<li>\u5c06\u7f16\u8bd1\u7ed3\u679c\u4fdd\u5b58\u5230\u53e6\u4e00\u4e2a\u5206\u652f\uff1b<\/li>\n<li>\u901a\u8fc7 jsDelivr \u5f15\u7528\u65b0\u5206\u652f\u4e0a\u7684\u8fd9\u4e2a\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u901a\u8fc7 GitHub \u53bb\u5e74\u63a8\u51fa\u7684\u65b0\u7279\u6027 Actions \u6765\u5b8c\u6210\uff0c\u5728\u6211\u4eec\u6bcf\u4e00\u6b21\u5411\u535a\u5ba2\u6e90\u7801\u4ed3\u5e93 push \u4ee3\u7801\u65f6\u81ea\u52a8\u89e6\u53d1\u3002<\/p>\n<p>\u5173\u952e\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\n<p>\u5728 GitHub \u65b0\u5efa\u4e00\u4e2a Personal access Token\uff1a<\/p>\n<p>Settings &#8211;&gt; Developer settings &#8211;&gt; Personal access tokens &#8211;&gt; Generate new token &#8211;&gt; \u586b\u5199 note\uff0c\u52fe\u9009 public_repo\uff0c\u751f\u6210\u4e4b\u540e\u590d\u5236 token \u503c\u5907\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u535a\u5ba2\u6e90\u7801\u4ed3\u5e93\u7684 Settings &#8211;&gt; Secrets &#8211;&gt; New secret\uff0cName \u586b <code>ACCESS_TOKEN<\/code>\uff0cValue \u586b\u7b2c 1 \u6b65\u91cc\u590d\u5236\u7684 token \u503c\uff1b<\/p>\n<\/li>\n<li>\n<p>\u5728\u535a\u5ba2\u6e90\u7801\u6839\u76ee\u5f55\u4e0b\u65b0\u5efa\u6587\u4ef6 .github\/workflows\/ci.yml \uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>name: Build and Deploy  on:   push:     branches: [ master ]  jobs:   build-and-deploy:     runs-on: ubuntu-latest     steps:       - name: Checkout         uses: actions\/[email&#160;protected]         with:            persist-credentials: false        - name: Set Ruby 2.7         uses: actions\/[email&#160;protected]         with:           ruby-version: 2.7        - name: Install and Build         run: |           gem install bundler           bundle install           bundle exec jekyll build        - name: Deploy         uses: JamesIves\/[email&#160;protected]         with:           ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}           BRANCH: built           FOLDER: _site           CLEAN: true <\/code><\/pre>\n<p>\u5927\u610f\u5c31\u662f\u5728\u5411 master \u5206\u652f push \u4ee3\u7801\u65f6\uff0c\u81ea\u52a8\u6267\u884c checkout \u3001\u521d\u59cb\u5316 ruby \u73af\u5883\u3001\u5b89\u88c5 Jekyll \u5e76\u7f16\u8bd1\u535a\u5ba2\u6e90\u7801\u7684\u5de5\u4f5c\uff0c\u6700\u540e\u5c06\u7f16\u8bd1\u751f\u6210\u7684 _site \u76ee\u5f55\u91cc\u7684\u5185\u5bb9\u63a8\u9001\u5230 built \u5206\u652f\u3002\u5bf9 GitHub Actions \u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u53c2\u8003\u5b98\u65b9\u8bf4\u660e\u5b66\u4e60\u3002<\/p>\n<\/li>\n<li>\n<p>\u4fee\u6539\u5f15\u7528 JSON \u6587\u4ef6\u7684\u5730\u65b9\uff0c\u6bd4\u5982\u6211\u7684 _includes\/sidebar-search.html \u91cc\u7684\u5199\u6cd5\u7531\uff1a<\/p>\n<pre><code>json: '{{ site.url }}\/assets\/search_data.json', <\/code><\/pre>\n<p>\u6539\u4e3a\u4e86<\/p>\n<pre><code>{% if site.cdn.jsdelivr.enabled and site.url contains 'mazhuang.org' %}   json: 'https:\/\/cdn.jsdelivr.net\/gh\/mzlogin\/[email&#160;protected]\/assets\/search_data.json', {% else %}   json: '{{ site.url }}\/assets\/search_data.json', {% endif %} <\/code><\/pre>\n<\/li>\n<li>\n<p>\u5c06\u4ee5\u4e0a\u66f4\u6539\u63a8\u9001\u5230\u6e90\u7801\u4ed3\u5e93\uff0c\u7b49\u5f85\u5904\u7406\u5b8c\u6210\u5373\u53ef\u3002<\/p>\n<\/li>\n<\/ol>\n<h2>0x03 \u7ed3\u8bed<\/h2>\n<p>\u7ecf\u8fc7\u4ee5\u4e0a\u6539\u9020\uff0c\u535a\u5ba2\u9875\u9762\u7684\u52a0\u8f7d\u901f\u5ea6\u53c8\u5f97\u5230\u4e86\u5c0f\u5c0f\u7684\u63d0\u5347\uff0c\u6240\u6709\u76f8\u5173\u6e90\u7801\u53ef\u4ee5\u5728 https:\/\/github.com\/mzlogin\/mzlogin.github.io \u627e\u5230\uff0c\u6709\u76f8\u5173\u5fc3\u5f97\u6216\u5efa\u8bae\u7684\u670b\u53cb\u6b22\u8fce\u4ea4\u6d41\u6307\u6b63\u3002<\/p>\n<p>\u76f8\u5173\u6587\u7ae0\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 jsDelivr \u514d\u8d39\u52a0\u901f GitHub Pages \u535a\u5ba2\u7684\u9759\u6001\u8d44\u6e90<\/li>\n<\/ul><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3663983\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wolfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u4f55\u89e3\u51b3 github pages \u4e0a\u5899\u7684\u95ee\u9898\u5450\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3663984\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : miao1007 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7136\u540e\u89e3\u6790\u7684\u7ed3\u679c\u662f 127.0.0.1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3663985\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : mzlogin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wolfan \u8fd9\u4e2a\u2026\u2026\u6211\u6ca1\u6709\u4ec0\u4e48\u597d\u529e\u6cd5\uff0c\u6362\u6258\u7ba1\u5230\u56fd\u5185\u7684\u670d\u52a1\u5427\uff0cgitee \u4e4b\u7c7b\u7684\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3663986\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : mzlogin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @miao1007 \u55ef\uff1f\u662f\u6307\u4ec0\u4e48\u5462                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 jsDelivr \u514d\u8d39\u52a0\u901f &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/165780"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165780"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/165780\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165780"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}