nuxt-content-assets
TypeScript icon, indicating that this package has built-in type declarations

1.4.2 • Public • Published

Nuxt Content Assets

npm version npm downloads License Nuxt

Enable locally-located assets in Nuxt Content

Nuxt Content Assets logo

Overview

Nuxt Content Assets enables locally-located assets in Nuxt Content:

+- content
    +- posts
        +- 2023-01-01
            +- index.md
            +- media
                +- featured.png
                +- mountains.jpg
                +- seaside.mp4

In your documents, reference assets with relative paths:

---
title: Summer Holiday
featured: media/featured.png
---

I loved being in the mountains.

![mountains](media/mountains.png)

Almost as much as being in the sea!

:video{src="media/seaside.mp4"}

At build time the module collates and serves assets and content together.

Features

Built on top of Nuxt Content and compatible with any Nuxt Content project or theme, including Docus.

User experience:

  • co-locate assets with content files
  • reference assets using relative paths
  • supports any format (image, video, doc)

Developer experience:

  • works with tags and custom components
  • works in markdown and frontmatter
  • file watching and asset live-reload
  • image size injection
  • zero config

Playground

To test the module before installing, you can try out the Nuxt Content Assets playground.

To clone and run locally:

git clone https://github.com/davestewart/nuxt-content-assets.git
cd nuxt-content-assets
npm install && npm install --prefix ./playground
npm run dev

Then open the playground in your browser at localhost:3000.

To run the playground online, visit:

To browse the playground folder:

Setup

Install the dependency:

npm install nuxt-content-assets

Configure nuxt.config.ts:

export default defineNuxtConfig({
  modules: [
    'nuxt-content-assets', // make sure to add before content!
    '@nuxt/content',
  ]
})

Run the dev server or build and local assets should now be served alongside markdown content.

Usage

Overview

Use relative paths anywhere within your documents:

Images
![image](image.jpg)

Links
[link](docs/article.txt)

Elements / components
:video{src="media/video.mp4"}

HTML
<iframe src="media/example.html" />

Relative paths can be defined in frontmatter – as long as they are the only value:

---
title: Portfolio
images:
  - assets/image-1.jpg
  - assets/image-2.jpg
  - assets/image-3.jpg
---

These values can then be passed to components:

:image-gallery{:data="images"}

See the playground for markup and component examples.

Live reload

In development, the module watches for asset additions, moves and deletes, and will update the browser live.

If you delete an asset, it will be greyed out in the browser until you replace the file or modify the path to it.

If you edit an image, video, embed or iframe source, the content will update immediately, which is useful if you're looking to get that design just right!

Image sizing

HTML

The module is preconfigured to pass image size hints (by default style) to generated <img> tags:

<!-- imageSize: 'style' -->
<img src="/image.jpg" style="aspect-ratio:640/480">

<!-- imageSize: 'attrs' -->
<img src="/image.jpg" width="640" height="480">

Keeping this on prevents content jumps as your page loads.

Prose components

If you use ProseImg components, you can hook into image size hints via the $attrs property:

<template>
  <span class="image">
    <img :src="$attrs.src" :width="$attrs.width" :height="$attrs.height" />
  </span>
</template>

<script>
export default {
  inheritAttrs: false
}
</script>

Frontmatter

If you pass frontmatter to custom components set imageSize to 'src' to encode values in src:

:image-content{:src="image"}

The component will receive the size information as a query string which you can extract and apply:

<img class="image-content" src="/image.jpg?width=640&height=480">

See playground component here.

Nuxt Image

Nuxt Content Assets works with Nuxt Image with just a little configuration:

// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    // Nuxt Image should be placed before Nuxt Content Assets
    '@nuxt/image',
    'nuxt-content-assets',
    '@nuxt/content',
  ],

  extends: [
    // add Nuxt Content Assets build folder as a Nuxt Layer (since v1.4.0)
    '.nuxt/content-assets',
  ],
}

Note that the new Layers setup enables Nuxt Image to load images from both the project's public folder and from content.

To serve all images as Nuxt Image images, create a ProseImg component like so:

<!-- components/content/ProseImg.vue -->
<template>
  <nuxt-img />
</template>

See the playground folder for both the global and a per image solution.

Configuration

The module has the following options:

// nuxt.config.ts
export default defineNuxtConfig({
  contentAssets: {    
    // inject image sizes into the rendered html
    imageSize: 'style',
    
    // treat these extensions as content
    contentExtensions: 'md csv ya?ml json',
    
    // output debug messages
    debug: false,
  }
})

Image size

You can add one or more image size hints to the generated images:

{
  imageSize: 'style attrs src'
}

Pick from the following switches:

Switch What it does
'style' Adds style="aspect-ratio:..." to any <img> tag
'attrs' Adds width and height attributes to any <img> tag
'src' Adds ?width=...&height=... to src attribute (frontmatter only)
false Disable image size hints

Note: if you add only attrs, include the following CSS in your app:

img {
  max-width: 100%;
  height: auto;
}

Content extensions

Generally, you shouldn't need to touch this setting

This setting tells Nuxt Content to ignore anything that is not one of these file extensions:

md csv ya?ml json

This way, you can use any other file type as an asset, without needing to explicitly configure extensions.

Debug

If you want to see what the module does as it runs, set debug to true:

{
  debug: true
}

How it works

When Nuxt builds, the module scans all content sources for assets, copies them to an accessible public assets folder, and indexes path and image metadata.

After Nuxt Content has run, the parsed content is traversed, and both element attributes and frontmatter properties are checked to see if they resolve to the indexed asset paths.

If they do, then the attribute or property is rewritten with the absolute path. If the asset is an image, then the element or metadata is optionally updated with size attributes or a query string.

Finally, Nitro serves the site, and any requests made to the transformed asset paths should be picked up and the copied asset served by the browser.

In development, file watching propagates asset changes to the public folder, updates related cached content, and notifies the browser via web sockets to refresh any loaded images.

Development

Should you wish to develop the project, the scripts are:

Develop the module (running the playground which uses the live module code):

# install dependencies
npm install

# generate playground type stubs (for the first time)
npm run dev:prepare

# develop (runs the playground app)
npm run dev

# run eslint
npm run lint

# run vitest
npm run test
npm run test:watch

Build and check the playground (simulating users' final build choices):

# generate the playground
npm run dev:generate

# build the playground
npm run dev:build

# serve the generated / built playground
npm run dev:preview

Make a new release (so users can install the module):

# dry run the release
npm run release:dry

# release new version
npm run release

Make sure to edit changelog and update package.json version before releasing!

Maintenance

This module was created using the Nuxt Module Builder command:

npx nuxi init -t module nuxt-content-assets

This created the module code from the starter template found here:

Both Nuxi and the module's dependencies and scripts are updated fairly regularly, so from time to time this module may need to be updated to keep in sync. So far, this has meant just updating the dependencies and scripts, which are found in the starter template code mentioned above.

Readme

Keywords

none

Package Sidebar

Install

npm i nuxt-content-assets

Weekly Downloads

462

Version

1.4.2

License

MIT

Unpacked Size

60.5 kB

Total Files

41

Last publish

Collaborators

  • davestewart