Skip to content

Add local remark plugin #1367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Benbinbin opened this issue Jul 21, 2022 · 12 comments
Closed

Add local remark plugin #1367

Benbinbin opened this issue Jul 21, 2022 · 12 comments
Labels

Comments

@Benbinbin
Copy link
Contributor

Benbinbin commented Jul 21, 2022

Environment

Reproduction

https://github.com/Benbinbin/nuxt-content-local-remark-plugin

Describe the bug

The former documentation show how to use the local remark plugin, like ~/markdownPlugins/remark-plugin.js

but the new documentation delete this part

when I use the same syntax which is mentioned at the old documentation

export default defineNuxtConfig({
  // ...
  content: {
    // ...
    markdown: {
      remarkPlugins: ['~/markdownPlugins/remarkPlugin.js']
    }
  }
})

but then I get an error (see th Logs part 👇 ) it recognizes the local remark plugin file as a package?

maybe the old documentation just suitable for content v1, any new way to add local remark plugin for content v2?

Additional context

some relative issues about plugin:

Logs

[nuxt] [request error] Cannot find package '~' imported from D:\Work\Code\Web\Frontend\Nuxt\nuxt-content-local-remark-plugin\.nuxt\dev\index.mjs
  at new NodeError (node:internal/errors:371:5)  
  at packageResolve (node:internal/modules/esm/resolve:884:9)  
  at moduleResolve (node:internal/modules/esm/resolve:929:18)  
  at defaultResolve (node:internal/modules/esm/resolve:1044:11)  
  at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
  at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
  at ESMLoader.import (node:internal/modules/esm/loader:276:22)
  at importModuleDynamically (node:internal/modules/esm/translators:111:35)
  at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
  at importPlugins (/D:/Work/Code/Web/Frontend/Nuxt/nuxt-content-local-remark-plugin/.nuxt/dev/index.mjs:1096:32)
@nozomuikuta
Copy link
Collaborator

Here is a debug memo.

Nuxt Content v2 dynamically imports plugins without resolving Nuxt aliases, so they are assumed to be inside node_modules.

async function importPlugins (plugins: Record<string, false | MarkdownPlugin> = {}) {
const resolvedPlugins = {}
for (const [name, plugin] of Object.entries(plugins)) {
if (plugin) {
resolvedPlugins[name] = {
instance: await import(name).then(m => m.default || m),
...plugin
}
} else {
resolvedPlugins[name] = false
}
}
return resolvedPlugins
}

Nuxt Content v1 resolves aliases by resolver:

const processMarkdownPlugins = (type, markdown, resolvePath) => {
const plugins = []
for (const plugin of markdown[`${type}Plugins`]) {
let name
let options
let instance
if (typeof plugin === 'string') {
name = plugin
options = markdown[camelCase(name)]
} else if (Array.isArray(plugin)) {
[name, options] = plugin
}
try {
instance = require(resolvePath(name))
plugins.push({ instance, name, options })
} catch (e) {
logger.error(e.toString())
}
}
return plugins
}

@ManasMadrecha
Copy link

@nozomuikuta How to solve it then?

@Benbinbin
Copy link
Contributor Author

@ManasMadrecha My solution is adding the custom Remark plugins as a npm module, you can check this repo for more detail.

Refer to this stackoverflow answer.

@ManasMadrecha
Copy link

ManasMadrecha commented Jul 31, 2022

@Benbinbin

In the BlogiNote link you shared, we don't need to actually publish the plugin to npm, right?
Just use the plugin offline and just change the link in the Package.json to file:... and then Content Module will be able to pick it up, right?

@Benbinbin
Copy link
Contributor Author

@ManasMadrecha yep, you're right

@ManasMadrecha
Copy link

ManasMadrecha commented Jul 31, 2022

@Benbinbin Amazing! BTW, your website's Table of Contents is just next level. It's magical... 😮👍🏻

Image

image

@innocenzi
Copy link

Seems quite inconvenient for one-time, project-specific plugins. Is there not a syntax for importing local plugins?

@Benbinbin
Copy link
Contributor Author

Seems quite inconvenient for one-time, project-specific plugins. Is there not a syntax for importing local plugins?

@innocenzi maybe the hook content:file:afterParse provided by Nuxt Content is the alternative solution for remark or rehype plugin

@ManasMadrecha
Copy link

@Benbinbin But the hook content:file:afterParse works as only rehype plugin and not remark plugin. Still, need to install a local remark plugin as npm module 😮‍💨

@DraftProducts
Copy link

Are there news about this regression ?

Copy link

github-actions bot commented Feb 7, 2025

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 7, 2025
Copy link

github-actions bot commented Mar 9, 2025

This issue was closed because it has been stalled for 30 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants