Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
code

GitHub Action

Resolve File Includes

v1.0.2

Resolve File Includes

code

Resolve File Includes

Resolves include directives written as HTML comments, like <!-- include: path/to/file.md --> (i.e. in your readme)

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Resolve File Includes

uses: devlooped/actions-includes@v1.0.2

Learn more about this action in devlooped/actions-includes

Choose a version

+Mᐁ includes

A GitHub action that resolves includes (by default in markdown files) and embeds the included files.

Including Content

In your content files:

<!-- include [RELATIVE_PATH][#ANCHOR] -->

The optional #anchor allows including fragments of files. Anchors are defined as:

<!-- #ANCHOR -->

with an optional "closing" anchor defined exactly the same as the starting one. If there is no closing anchor, the included content is from anchor declaration to the end of the file. For example:

<!-- #badges -->
[...] //some shields.io badges you use everywhere
<!-- #badges -->

Which can be included with:

<!-- include common.md#badges -->

You can exclude a file from processing by having as the first (or last) line:

<!-- exclude -->

Action Usage

- name: +Mᐁ includes
  uses: devlooped/actions-include@v6
  with:
    # files to include for processing, i.e. '*.md'
    # Default: *.md
    include: ''

    # files to exclude from processing, i.e. 'header.md'
    # Default: $null
    exclude: ''

    # whether to recurse into subdirectories
    # Default: true
    recurse: true|false

    # Whether to validate include links. If false, a warning will be 
    # generated instead of an error.
    # Default: true
    validate: true|false

Example

To run the action and automatically create a PR with the resolved includes:

on: 
  push:
    branches:
      - 'main'
    paths:
      - '**.md'    

jobs:
  includes:
    runs-on: ubuntu-latest
    steps:
      - name: 🤘 checkout
        uses: actions/checkout@v2

      - name: +Mᐁ includes
        uses: devlooped/actions-include@v1

      - name: ✍ pull request
        uses: peter-evans/create-pull-request@v3
        with:
          base: main
          branch: markdown-includes
          delete-branch: true
          labels: docs
          commit-message: +Mᐁ includes
          title: +Mᐁ includes
          body: +Mᐁ includes

Note how you can trivially create PRs that update the changed files so the includes are always resolved automatically whenever you change any of the monitored files.

How it works

You can include content from arbitrary external files with:

<!-- include header.md -->

# This my actual content

<!-- include footer.md -->

When the action runs for the first time, it will turn the above content into:

<!-- include header.md -->
This comes from the included header!
<!-- header.md -->

# This my actual content

<!-- include footer.md -->
This comes from the included footer!
<!-- footer.md -->

The action is idempotent, so it is safe for it to run on pushes of the same files it changed via the includes (since no further changes will be detected).

NOTE: the included path must be relative to the including file.

Limitations

  • Nested includes are not supported for now (the include keyword inside the HTML comment "directive" will be stripped on inclusion).

  • File processing order matches what the following equivalent command on the working directory returns via PowerShell:

    pwsh> gci -include [include] -exclude [exclude] [-recurse]
    

    This knowledge can be used to workaround the nested includes limitation by running the action multiple times with different sets of files.

Sponsors

Kirill Osenkov C. Augusto Proiete SandRock Amazon Web Services Christian Findlay Clarius Org MFB Technologies, Inc.

Sponsor this project  

Learn more about GitHub Sponsors