11分で上がる


イメージバイ11ty.dev
私はどのように使用を開始した私の独断的なパスを共有したいと思います.

目次
  • What is Eleventy?
  • Why Eleventy?
  • Create a new project
  • Adding content
  • Running and building
  • HTML templates
  • Providing data for your site
  • Collections
  • Filters
  • Plugins
  • Adding toolchains for CSS and JS

  • 第11回 Eleventy 人気のある静的サイトジェネレータです.入力ファイルから静的サイトを作成します.入力ファイルは、コンテンツファイル、HTMLテンプレートファイル、およびこのファイルでカバーされるデータファイルです.
    これは、いくつかのデータファイル形式、コンテンツファイル形式とHTMLテンプレートエンジンを使用すると一緒に使用することができます.この記事ではMarkdown ファイルをNunjucks テンプレート.

    なぜ上がる?

    There are many static site generators out there and you may wonder what benefits it offers compared to others. The key points which make me really love Eleventy are:

    • it is built on node.js
    • it does one job and does that well: create markup from content plus templates
    • it is completely unopinionated about client-side JS+CSS toolchains: bring your own
    • no angular/react/vue knowledge necessary.
    • html first approach which makes it great for progressive enhancement.
    • easily extendible and combinable with npm packages
    • similar to jekyll, but with node.js as a base
    • easy to mock data via the data folder.

    新しいプロジェクトの作成
    mkdir my-awesome-site
    cd my-awesome-site
    git config --global init.defaultBranch main
    npm init -y
    npm i @11ty/eleventy -D
    

    リポジトリを初期化する

    Additionally, run git init and provide a .gitignore if you are working with git:

    node_modules
    .DS_Store
    Thumbs.db
    
    # Eleventy output folder
    public
    

    構成

    Configuration is done via a single javascript file named .eleventy.js . Providing a configuration is optional.

    My personal preference is to provide src as input folder and public as output folder. Additionally, I use to specify folders that are copied over to the output folder on build. These are also automatically watched by eleventy when starting the development server.

    module.exports = (config) => {
      // specify folders to be copied to the output folder
      config.addPassthroughCopy('./src/js/');
      config.addPassthroughCopy('./src/css/');
    
      return {
        markdownTemplateEngine: 'njk',
        htmlTemplateEngine: 'njk',
        dir: {
          input: 'src',    // default: '.'
          output: 'public' // default: '_site'
        }
      }
    };
    

    コンテンツの追加

    Create a markdown file and name it like this: src/index.md

    ---
    title: "Hello world"
    ---
    # Hello World
    
    Welcome to my awesome {{title}} site! 
    

    マークダウンと前件 Markdown files can optionally provide a metadata block, which is marked via three hyphens --- in the beginning and the end of the block. In this block, you can specify meta data in YAML 表記.

    コンテンツからパスへ

    For each markdown content file, eleventy creates a folder with an index.html for nice urls:

    index.md            --> /
    about.md            --> /about/
    faq.md              --> /faq/
    blog/hello-world.md --> /blog/hello-world/
    

    ランニングとビル

    Finally, we can start adding the start and build tasks to our package.json file:

    {
      "scripts": {
        "start": "eleventy --serve",
        "build": "eleventy"
      }
    }
    
    • npm start -> start Development server
    • npm run build -> build your website
    • npx serve public -> test your build

    The development server is using browsersync which automatically keeps track of changes and updates your DOM. Additionally, if you open the page in multiple browsers, events are kept in sync, which is useful for cross-browser-testing.


    HTMLテンプレート To define your HTML structure and layouts, you can use HTML templates. There are several template engines available in Eleventy. In this example, I'm using the Nunjucks テンプレートエンジン.
    サポートされる他のテンプレート形式.html , .liquid , .hbs , .ejs , .haml , .pug .
    一般的なアプローチは、ベースのHTMLファイルをビルドし、ビルドすることです
    あなたがそれに基づいて必要ないくつかの他の構造.
    加えるsrc/_includes/base.njk ファイル
    <!DOCTYPE html>
    <html lang="en">
      <head><title>{{ title }}</title></head>
      <body>
        <main>
          {% block main %}
            {{ content | safe }}
          {% endblock %}
        </main>
      </body>
    </html>
    
    経由で{% block %}{% endblock %} 構文を使用すると、テンプレートを拡張するときに使用するテンプレートにいくつかのスロットを追加できます.
    The content 変数は現在の内容ファイルの内容体を含む予約変数です.
    The | safe ディレクティブは、テンプレートエンジンに指示します.このように、HTMLタグはプレーンテキストに変換されませんHTML entities . これは、コンテンツ内のHTMLを使用することができます.

    コンテンツファイルのテンプレートの使用

    In the front matter of your markdown file, specify the layout you want to use:

    layout: base
    

    テンプレートの拡張

    Next to your base.njk file, create an article.njk file:

    {% extends "base.njk" %}
    
    {% block main %}
      <article class="article">
        {{ content | safe }}
      </article>
    {% endblock %}
    
    Nunjucks also has a section about inheritance in the documentation: https://mozilla.github.io/nunjucks/templating.html#template-inheritance

    含む

    You can include partial layouts anywhere in your njk or markdown files:

    {% include "header.njk" %}
    

    あなたのサイトのデータを提供する

    There are several ways to provide data that can be used from inside your templates or content files:

    • file specific: in the markdown's front matter
    • folder specific: add a json file to a content folder
    • globally _data directory: globally available
    • _data supports .js , .yaml , .json files
    _data
    ナビゲーションを構築し、JSONファイルからすべてのURLエントリを提供したいと思います.src/_data/nav.json
    [
      {"title": "Home", "url": "/"},
      {"title": "Blog", "url": "/blog/"}
    ]
    
    次に、メインテンプレートに含めるには、一部のHTMLスニペットを作成できます.src/_includes/nav.njk
    <nav>
      <ul>
        {% for link in nav %}
          <li>
            <a href="{{ link.url }}">{{ link.title }}</a>
          </li>
        {% endfor %}
      </ul>
    </nav>
    
    _data JavaScriptの例_data/site.js
    module.exports = {
      name: 'My awesome site',
      url: 'https://awesome.site/'
    };
    
    このような内容で使用できます.{{ site.name }}を持つ.js プレーンJSONまたはYAMLファイルの代わりにファイルを使用すると、ノードを使用する柔軟性をもたらします.JS環境変数(開発や生産環境にあるかどうかを確認).また、そこからAPIのフェッチを行うことができますヘッドのCMSでは、例えばプル.

    コレクション

    You can tag your content with a keyword and then iterate through these via collections.

    This is useful for auto-generating table of contents or listing articles that are related to each other


    コレクションの例
    あなたのsrcフォルダでblog マルクダウンファイルの束を持つフォルダ.としてタグposts 前もって
    tags: posts
    
    次に、マークダウンまたはインクルードファイルで、forループを介してこれらのコレクションを反復処理できます.index.md :
    # Blog
    
    <ul>{% for post in collections.posts %}<li>
      <a href="{{ post.url }}">
        {{ post.date | date('YYYY-MM-DD') }}: {{ post.data.title }}
      </a>
    </li>{% endfor %}</ul>
    

    フィルタ

    Filters provide a way to further process your content. You can use these filters from inside your content and template files by using the pipe.


    カスタムフィルタの追加
    あなたの.eleventy.js ファイルを使用すると、ファイル内で使用できるいくつかのフィルタを追加できます.サードパーティライブラリをここで使用することもできます.これは、スクリームフィルタと日付書式フィルタの例です.
    const moment = require('moment');
    
    module.exports = (config) => {
      config.addFilter('date', (date, format) => moment(date).format(format || 'YYYY-MM-DD'));
      config.addFilter('scream', (str) => str.toUpperCase());
      // ...additional config 
      return { ... }
    };
    
    次に、このフィルタをこのような内容やテンプレートファイルに使用できます.
    {{ content | scream | safe }}`
    {{ page.date | date('YYYY-MM-DD') }}
    

    処理にはフィルタ付きファイルが含まれます
    あなたがフィルタでインクルードを処理したいならば、あなたはNunjucksを使うことができますset 変数に格納するディレクティブ.私の個人的なサイトでは、このテクニックを使ってWebGL遮光物コードをオンザフライしています.
    {% set vertexShader %}
    {% include 'shaders/vertex-shader.vs' %}
    {% endset %}
    
    {{ vertexShader | glslminify | safe }}
    

    内蔵フィルタ
  • あなたはすべてを使用することができますbuilt-in nunjucks filters

  • safe – コンテンツが挿入するのは安全ですので、HTML固有の文字はHTMLエンティティに変換されません(HTMLとスクリプトを注入するために使用します).

  • url – プリペアドパスを指定します(サブディレクトリに展開する場合、例えばGITUUBページ上で便利です).

  • slug – 文字列をURLフレンドリーなスラグに変換するMy site to my-site )

  • get*CollectionItem – コレクション内の次または前の項目を取得する

  • プラグインとツール

    Eleventy provides a rich plugin ecosystem where you can add further magic✨ to your workflow 🙌.

    Check out the Eleventy plugins documentation

    CSSとJS用のツールチェーンの追加

    In the article, we used a pass-through-copy command and used CSS and JS without any bundling or further processing. My favorite approach is to use a CSS preprocessor plus ES module JavaScript files. These are not supported in legacy browsers such as IE11. When using progressive enhancement, JavaScript is not required to read
    your content.

    In the following, I will demonstrate the approach I used (only using a CSS transpiles) and a complete JS+CSS toolchain using parcel as an alternative approach.


    CSSトランスポーターのみ
    私の個人的なプロジェクトでは、私は同時に2つのプロセスを同時に開始するために同時にSASSを使用しましたnpm start スクリプト.
    npm i sass concurrently -D
    
    CSSをビルドするにはsass src/scss:src/css はすべてをコンパイルする.CSSへのSSS
    {
      "scripts": {
        "start": "concurrently 'npm:watch-css' 'npm:serve-11ty'",  
        "build-11ty": "eleventy",
        "serve-11ty": "eleventy --serve",
        "build-css": "sass src/scss/:src/css/",
        "watch-css": "sass src/scss/:src/css/ --watch",
        "build": "npm run build-css -s && npm run build-11ty -s"
      }
    }
    

    またはJavaScript + CSSのツールチェーンを完了します.
    JavaScriptとCSSをコンパイルするための完全なフロントエンドのツールチェーンを持っているなら、使用する一つの方法はParcel .
    npm i parcel-bundler concurrently -D
    echo src/dist >> .gitignore
    echo .cache >> .gitignore
    
    開発モードでは、私も使用しているconcurrently パレットと小包を並列に開始するには、次の手順に従います
    {
      "scripts": {
        "start": "concurrently 'npm:watch-bundle' 'npm:serve-11ty'",
        "build": "npm run build-bundle -s && npm run build-11ty -s",
        "watch-bundle": "parcel watch src/app/index.js -d src/dist",
        "build-bundle": "parcel build src/app/index.js -d src/dist",
        "serve-11ty": "eleventy --serve",
        "build-11ty": "eleventy"
      }
    }
    
    インsrc/app , を置くindex.js ファイル.さらに、任意のCSSインポートを入れます.
    import './scss/styles.scss';
    
    console.log('Hello world');
    
    最後に、あなたの11番目の設定でパスをコピーして、その出力を上位の出力フォルダにコピーします.
    config.addPassthroughCopy('./src/dist/');
    
    次に、小包はインデックスを作成します.インデックス.このようなHTMLテンプレートで使用できるdistフォルダのcss
    <!-- in your head tag --> 
    <link rel="stylesheet" href="/dist/index.css">
    
    <!-- right before your closing </body> tag -->
    <script src="/dist/index.js"></script>
    

    プロジェクト例

  • https://github.com/terabaud/lea-codes/

  • 資源

  • https://terabaud.github.io/eleventy-talk/ - 私のスライド11話(本編のまとめとして役に立つ)

  • https://11ty.dev/ - 公式サイト

  • https://11ty.rocks/ - 岩

  • https://piccalil.li/course/learn-eleventy-from-scratch - 深さコース

  • ありがとう👩‍💻