facebook / docusaurus

Easy to maintain open source documentation websites.
https://docusaurus.io
MIT License
55.96k stars 8.4k forks source link

Need a quick Docusaurus V2 example of developing a plugin. #1928

Closed goodlux closed 4 years ago

goodlux commented 4 years ago

📚 Documentation

Complete noob here, trying to write a simple plugin for Docusarus v2.

Trying to use these docs to reach that end:

https://v2.docusaurus.io/docs/advanced-plugins

I realize this doc is a WIP at this stage, but it is a bit of a mish-mash, and I'm having trouble trying to instantiate and use a local plugin after installing. I've tried everything and now my eyes are bleeding.

Would be really helpful to have a super easy example of a one-function plugin that does something easy like read and write a file, or even print something to the javascript console. I'm happy to write this doc if someone can get me started with a simple code sample.

The sample here appears to be a complete plugin scaffold:

https://v2.docusaurus.io/docs/lifecycle-apis#example

But again, having trouble understanding where this code is actually called in the build process after running :

npm start

Thanks!

Have you read the Contributing Guidelines on issues?

Yes

wgao19 commented 4 years ago

Hi @goodlux what kind of plugins are you trying to write?

For plugins that perform a single task that does not participate in the static site's build step, you can take a look at the gtag plugin.

If you hope to write a plugin like docs or blog, i.e., the kind that parses content into pages, it can be slightly more complex. But docusaurus-plugin-content-pages is a relatively simple example.

endiliey commented 4 years ago

Another simpler example is https://github.com/facebook/docusaurus/blob/master/packages/docusaurus-plugin-ideal-image/src/index.js

It tries to modify webpack config. and it provides a theme component. Much much simpler than pages plugin (i think)

Help on documentation is appreciated

vjpr commented 4 years ago

Would be nice to have a boilerplate with all the TypeScript tooling setup, such that you can npm link to it or use it in a monorepo:

packages
  docusaurus-app
  docusaurus-plugin
slorber commented 4 years ago

@vjpr you don't need a monorepo setup to handle your own plugins. If they are small and only useful for your site you can use a local plugin.

https://v2.docusaurus.io/docs/using-plugins/#installing-a-plugin

I think this is good enough for most sites, not sure we want to maintain a monorepo setup boilerplate. Maybe the init boilerplate could include a local plugin.

About TS, not all D2 users are frontend developers, so it's probably not a good idea to include anything related to TS by default.

vjpr commented 4 years ago

About TS, not all D2 users are frontend developers, so it's probably not a good idea to include anything related to TS by default.

For new users the auto-complete from TypeScript would be useful.

slorber commented 4 years ago

One thing that we could do is bootstrap new projects with an empty JS plugin with all the lifecycles as noop.

For TS users, we could annotate the plugins JS files with JS docs comment? Need to check that, but maybe the IDE's would understand the syntax and provide autocomplete?

https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html