1.14.0 • Published 3 months ago

@jungvonmatt/contentful-ssg v1.14.0

Weekly downloads
103
License
MIT
Repository
github
Last release
3 months ago

NPM version Build Status Coverage

JvM Contentful export for static site generators

Export contentful entries to filesystem (md/yaml) for static site generators (hugo/grow/...)

Automatically removes all files from the configured output directory that are not part of the exported entries. When a .gitignore file is found only ignored files are removed.

Getting started

Install

npm install --save-dev @jungvonmatt/contentful-ssg

Commands

help

npx cssg help [command]

init

npx cssg init

Initializes contentful-ssg and stores the config values in the contentful-ssg.config.js file.

Contentful SSG ships with built in typescript support. Add --typescript to generate a typescript configuration file.

npx cssg init --typescript

Configuration values

NameTypeDefaultDescription
accessTokenstringundefinedContent Delivery API - access token
previewAccessTokenstringundefinedContent Preview API - access token
spaceIdstringundefinedContentful Space id
environmentIdstring'master'Contentful Environment id
formatstring|function|object'yaml'File format ( yaml, toml, md, json) You can add a function returning the format or you can add a mapping object like {yaml: [glob pattern]} (pattern should match the directory)
plugins[string]|[[string, options]]|[{resolve:'string', options:{}}][]Add plugins to contentful-ssg. See Plugins
directorystring'./content'Base directory for content files.
validatefunctionundefinedPass function(transformContext, runtimeContext){...} to validate an entry. Return false to skip the entry completely. Without a validate function entries with a missing required field are skipped.
transformfunctionundefinedPass function(transformContext, runtimeContext){...} to modify the stored object. Return undefined to skip the entry completely. (no file will be written)
mapDirectoryfunctionundefinedPass function(transformContext, runtimeContext, defaultValue){...} to customize the directory per content-type relative to the base directory.
mapFilenamefunctionundefinedPass function(transformContext, runtimeContext, defaultValue){...} to customize the filename per entry
mapAssetLinkfunctionundefinedPass function(transformContext, runtimeContext, defaultValue){...} to customize how asset links are stored
mapEntryLinkfunctionundefinedPass function(transformContext, runtimeContext, defaultValue){...} to customize how entry links are stored
mapMetaFieldsfunctionundefinedPass function(transformContext, runtimeContext, defaultValue){...} to customize the meta fields per entry
richTextRendererboolean|object|function{}We use the contentful rich-text-html-renderer to render the html. You can pass a configuration object or you can pass function(document){...} to use your own richtext renderer or you can turn it off by passing false to get a mirrored version of the JSON output
beforefunctionundefinedRuns function(runtimeContext){...} before processing the content right after pulling data from contentful
afterfunctionundefinedRuns function(runtimeContext){...} after processing the content right before the cleanup

Plugins

You can add plugins to contentful-ssg by adding the package name or a local path to the plugins array of your configuration.

plugins: ['my-plugin-package', './plugins/my-local-plugin]

All plugins can have options specified by wrapping the name and an options object in an array inside your config or by using a more verbose object notation.

For specifying no options, these are all equivalent:

{
  "plugins": ["my-plugin", ["my-plugin"], ["my-plugin", {}], {resolve: "my-plugin", options: {}}]
}

To specify an option, pass an object with the keys as the option names.

{
  "plugins": [
    [
      "my-plugin-a",
      {
        "option": "value"
      }
    ],
    {
      resolve: "my-plugin-b",
      options: {
        "option": "value"
      }
    }
  ]
}

Runtime Hooks

before

import
(runtimeContext) => {
  // Do things before processing the localized contentful entries
  // The return value should be an object which is merged with the runtime context.
  return { key: 'test' };
}

after

(runtimeContext) => {
  // Do things after processing the localized contentful entries before cleanup
  // We have access to values added to the context in the before hook
  console.log(runtimeContext.key); // -> 'test'
};

Transform Hooks

transform

(transformContext, runtimeContext) => {
  const { content } = transformContext;
  // modify content and
  // return object
  return content;
};

mapFilename

(transformContext, runtimeContext, defaultValue) => {
  // customize the filename on entry level
  // return string
  return defaultValue;
};

mapDirectory

(transformContext, runtimeContext, defaultValue) => {
  // customize the directory on entry level
  // return string
  return defaultValue;
};

mapAssetLink

(transformContext, runtimeContext, defaultValue) => {
  const {asset} = transformContext;
  // customize the asset representation in front matter
  // return object
  return { ...defaultValue, ... };
}

mapEntryLink

(transformContext, runtimeContext, defaultValue) => {
  const {entry} = transformContext;
  // customize how the entry is added to your front matter
  // return object
  return { ...defaultValue, ... };
}

mapMetaFields

(transformContext, runtimeContext, defaultValue) => {
  const {entry} = transformContext;
  // customize how the sys meta data is added to your front matter
  // return object
  return { ...defaultValue, ... };
}

Helper functions

collectValues

Get values from linked pages to e.g. build an url out of parent slugs.

{
  transform: (context) => {
    const { utils } = context;
    const slugs = utils.collectValues('fields.slug', {
      linkField: 'fields.parentPage',
    });

    return { ...content, url: '/' + slugs.join('/') };
  };
}
collectParentValues

The same as collectValues just without the value from the current entry

waitFor

Wait for specific entry to be transformed. Be aware that this can lead to deadlocks when you're awaiting something which itself is waiting for the current entry to be transformed.

{
  transform: (context) => {
    const { utils } = context;
    try {
      // You can overwrite the default wait timeout of 20000ms using the second parameter
      const linkedContext = await utils.waitFor('<contentful-id>', 5000);

      // Do something usefull with the transformed data
      // which you can't do with context.entryMap.get('<contentful-id>')
    } catch (error) {
      // Entry isn't available, the transform method for the entry throws an error
      // or we encountered a cyclic dependency
    }

    return { ...content };
  };
}

fetch

Fetch all content entries and store them as yaml in the configured directory

npx cssg fetch

To see all available command line options call

npx cssg help fetch

watch

Same as fetch but also starts a webserver listening for changes and registers a contentful webhook

npx cssg watch

To see all available command line options call

npx cssg help watch

Example configuration

Grow

See cssg-plugin-grow

Hugo

See cssg-plugin-hugo

1.14.0

3 months ago

1.12.3

1 year ago

1.12.0

1 year ago

1.11.3

1 year ago

1.11.2

1 year ago

1.11.1

1 year ago

1.11.0

2 years ago

1.10.0

2 years ago

1.8.0

2 years ago

1.9.0

2 years ago

1.7.4

2 years ago

1.7.5-alpha.5

2 years ago

1.7.5-alpha.0

2 years ago

1.7.5-alpha.6.5

2 years ago

1.8.0-alpha.2

2 years ago

1.8.0-alpha.0

2 years ago

1.8.0-alpha.1

2 years ago

1.8.0-alpha.6

2 years ago

1.8.0-alpha.4

2 years ago

1.8.0-alpha.5

2 years ago

1.7.3

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.4.0

2 years ago

1.4.9

2 years ago

1.4.8

2 years ago

1.0.2

2 years ago

1.0.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.0-alpha.0

2 years ago

1.1.0

2 years ago

0.17.3

3 years ago

0.17.2

3 years ago

0.17.1

3 years ago

0.17.0

3 years ago

0.16.2

3 years ago

0.16.0

3 years ago

0.16.1

3 years ago

0.15.0

3 years ago

0.14.1

3 years ago

0.15.1

3 years ago

0.14.2

3 years ago

0.13.0

3 years ago

0.14.0

3 years ago

0.12.3

3 years ago

0.12.2

3 years ago

0.12.1

3 years ago

0.12.0

3 years ago

0.11.3

3 years ago

0.11.2

3 years ago

0.11.1

3 years ago

0.11.0

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago