0.2.0 • Published 3 years ago

remark-code-import-replace v0.2.0

Weekly downloads
13
License
MIT
Repository
github
Last release
3 years ago

Remark Code Import Replace remark-code-import-replace

CI License MIT

Remark code file import and optionally replace the code block.

Basic usage

Code import and replace node

replace will only run if import= is set.

Remark options

export default {
  remark: ['remark-code-import-replace', {
    // optionally override baseDir
    baseDir: '/snippets',
    replace: (node, meta, {u}) => {
      // Access any meta declared in codeblock
      if (meta.param) {
        return [node]
      }

      // e.g. to wraped into another component
      if (meta.render) {
        return [
          u('html', {value: `<div>`}),
          node,
          u('html', {value: `</div>`}),
        ]
      }
    },
  }]
}

Using with Nuxt Content and Components

  • Code file import
  • Register a new directory for global import
  • Replace and add a new node with the name of the component
// nuxt.config.js
export default {
  content: {
    markdown: {
      remarkPlugins: [
        ['remark-code-import-replace', {
          replace: (node, meta, {u}) => {
            return [
              u('html', {value: `<${meta.file.name}>`}),
              u('html', {value: `</${meta.file.name}>`}),
              node,
            ]
          }
        }]
      ],
    },
  },
  hooks: {
    'components:dirs': async (dirs) => {
      dirs.push({
        path: "~/content",
        global: true
      })
    }
  }
}