npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gatsby-remark-copy-linked-files-with-poster

v1.0.2

Published

Find files which are linked to from markdown and copy them to the public directory

Downloads

1

Readme

gatsby-remark-copy-linked-files-with-poster

Copies local files linked to/from Markdown (.md|.markdown) files to the root directory (i.e., public folder).

A sample markdown file:

---
title: My awesome blog post
---

Hey everyone, I just made a sweet PDF with lots of interesting stuff in it.

[Download it now](my-awesome-pdf.pdf)

When you build your site:

The my-awesome-pdf.pdf file will be copied to the root directory (i.e., public/some-really-long-contenthash/my-awesome-pdf.pdf) and the generated HTML page will be modified to point to it.

Note: The my-awesome-pdf.pdf file should be in the same directory as the markdown file.


Install plugin

npm install --save gatsby-remark-copy-linked-files-with-poster

Add plugin to Gatsby Config

Default settings:

Add gatsby-remark-copy-linked-files-with-poster plugin as a plugin to gatsby-transformer-remark:

// In your gatsby-config.js

// add plugin by name only
plugins: [
  {
    resolve: `gatsby-transformer-remark`,
    options: {
      plugins: [`gatsby-remark-copy-linked-files-with-poster`],
    },
  },
]

Custom settings:

// In your gatsby-config.js

// add plugin by name and options
plugins: [
  {
    resolve: `gatsby-transformer-remark`,
    options: {
      plugins: [
        {
          resolve: `gatsby-remark-copy-linked-files-with-poster`,
          options: {
            destinationDir: `path/to/dir`,
            ignoreFileExtensions: [`png`, `jpg`, `jpeg`, `bmp`, `tiff`],
          },
        },
      ],
    },
  },
]

Custom set where to copy the files using destinationDir

By default, all files will be copied to the root directory (i.e., public folder) in the following format: contentHash/fileName.ext.

For example, [Download it now](my-awesome-pdf.pdf) will copy the file my-awesome-pdf.pdf to something like public/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf

Simple usage

To change this, set destinationDir to a path of your own choosing (i.e., path/to/dir).

// In your gatsby-config.js
plugins: [
  {
    resolve: `gatsby-transformer-remark`,
    options: {
      plugins: [
        {
          resolve: "gatsby-remark-copy-linked-files-with-poster",
          options: {
            destinationDir: "path/to/dir",
          },
        },
      ],
    },
  },
]

So now, [Download it now](my-awesome-pdf.pdf) will copy the file my-awesome-pdf.pdf to public/path/to/dir/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf

Advanced usage

For more advanced control, set destinationDir to a function expression using properties name and/or hash to specify the path.

Examples:

# save `my-awesome-pdf.pdf` to `public/my-awesome-pdf.pdf`
destinationDir: f => `${f.name}`

# save `my-awesome-pdf.pdf` to `public/2a0039f3a61f4510f41678438e4c863a.pdf`
destinationDir: f => `${f.hash}`

# save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf`
destinationDir: f => `downloads/${f.hash}/${f.name}`

# save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a-my-awesome-pdf.pdf`
destinationDir: f => `downloads/${f.hash}-${f.name}`

# save `my-awesome-pdf.pdf` to `public/my-awesome-pdf/2a0039f3a61f4510f41678438e4c863a.pdf`
destinationDir: f => `${f.name}/${f.hash}`

# save `my-awesome-pdf.pdf` to `public/path/to/dir/hello-my-awesome-pdf+2a0039f3a61f4510f41678438e4c863a_world.pdf`
destinationDir: f => `path/to/dir/hello-${f.name}+${f.hash}_world`

Note: Make sure you use either name or hash property in your function expression! If you don't include both name and hash properties in your function expression, gatsby-remark-copy-linked-files-with-poster plugin will resolve the function expression to a string value and use default settings as a fallback mechanism to prevent your local files from getting copied with the same name (causing files to get overwritten).

# Note: `my-awesome-pdf.pdf` is saved to `public/hello/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf`
# because `name` and `hash` properties are not referenced in the function expression.
# So these function expressions are treated the same way
destinationDir: _ => `hello`
destinationDir: `hello`

Caveat: Error thrown if destinationDir points outside the root directory (i.e. public folder)

Note: An error will be thrown if the destination points outside the root directory (i.e. public folder).

Correct:

# saves to `public/path/to/dir/`
destinationDir: `path/to/dir`

# saves to `public/path/to/dir/`
destinationDir: _ => `path/to/dir`

# saves to `public/path/to/dir/fileName.ext`
destinationDir: f => `path/to/dir/${f.name}`

# saves to `public/contentHash.ext`
destinationDir: f => `${f.hash}`

Error thrown:

# cannot save outside root directory (i.e., outside `public` folder)
destinationDir: `../path/to/dir`
destinationDir: _ => `../path/to/dir`
destinationDir: f => `../path/to/dir/${f.name}`
destinationDir: f => `../${f.hash}`

Custom set which file types to ignore using ignoreFileExtensions

By default, the file types that this plugin ignores are: png, jpg, jpeg, bmp, tiff.

For example, [Download it now](image.png) will be ignored and not copied to the root dir (i.e. public folder)

To change this, set ignoreFileExtensions to an array of extensions to ignore (i.e., an empty array [] to ignore nothing).

// In your gatsby-config.js
plugins: [
  {
    resolve: `gatsby-transformer-remark`,
    options: {
      plugins: [
        {
          resolve: "gatsby-remark-copy-linked-files-with-poster",
          options: {
            // `ignoreFileExtensions` defaults to [`png`, `jpg`, `jpeg`, `bmp`, `tiff`]
            // as we assume you'll use gatsby-remark-images to handle
            // images in markdown as it automatically creates responsive
            // versions of images.
            //
            // If you'd like to not use gatsby-remark-images and just copy your
            // original images to the public directory, set
            // `ignoreFileExtensions` to an empty array.
            ignoreFileExtensions: [],
          },
        },
      ],
    },
  },
]

So now, [Download it now](image.png) will be copied to the root dir (i.e. public folder)


Supported Markdown tags

  • img - ![Image](my-img.png)
  • link - [Link](myFile.txt)

Supported HTML tags

  • <img />
  • <video />
  • <audio />
  • <a />