Skip to content

Remark to import code block and optionally replace the code block to another syntax tree.

License

Notifications You must be signed in to change notification settings

fuxingloh/remark-code-import-replace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Remark Code Import Replace remark-code-import-replace

CI License MIT

Remark code file import and optionally replace the code block.

Basic usage

```js import=code.js
```

Code import and replace node

replace will only run if import= is set.

```js import=code.js render param
```

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
      })
    }
  }
}

About

Remark to import code block and optionally replace the code block to another syntax tree.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published