开发 Discourse 插件 - 第一部分 - 创建一个基础插件

在了解了一些特殊之处后,构建 Discourse 插件会非常简单。本文的目标是创建一个骨架插件并向您介绍基础知识。

您的开发环境

请确保您的计算机上运行着 Discourse 的开发环境。我建议您使用相应的设置指南,并在完成后再回来。

plugin.rb

:tada: 使用 GitHub - discourse/discourse-plugin-skeleton: Template for Discourse plugins 在您的 plugins 目录中创建一个完整的 Discourse 插件骨架 :tada:

该骨架现已捆绑在 Discourse 核心中,rake plugin:create[plugin-name] 将使用该骨架创建一个插件

当 Discourse 启动时,它会在 plugins 目录中查找包含 plugin.rb 文件的子目录。plugin.rb 文件有两个用途:它是插件的清单,包含有关插件的必要信息,包括:名称、联系信息和描述。第二个用途是初始化运行插件所需的任何 Ruby 代码。

在我们的例子中,我们不会添加任何 Ruby 代码,但仍然需要 plugin.rb。让我们创建目录 basic-plugin,并在其中创建文件 plugin.rb,内容如下:

basic-plugin/plugin.rb

# name: basic-plugin
# about: A super simple plugin to demonstrate how plugins work
# version: 0.0.1
# authors: Awesome Plugin Developer
# url: https://github.com/yourusername/basic-plugin

创建此文件后,您应该重启本地服务器,插件就会被加载。

一个重要的陷阱!

如果您习惯于常规的 Rails 开发,您可能会注意到插件在重新加载方面并不那么方便。通常,当您对插件进行更改时,您应该按住 \u003ckbd\u003eCtrl\u003c/kbd\u003e+\u003ckbd\u003ec\u003c/kbd\u003e 停止服务器运行,然后使用 bin/ember-cli -u 再次运行它。

我的更改没有生效!:warning:

有时缓存不会完全清除,尤其是在您创建新文件或删除旧文件时。要解决此问题,请删除您的 tmp 文件夹并重新启动 Rails。在 Mac 上,您可以用一个命令完成:rm -rf tmp; bin/ember-cli -u

检查插件是否已加载

重启本地服务器后,访问网址 /admin/plugins(请确保您以管理员帐户登录后,因为只有管理员才能看到插件注册表)。

如果一切正常,您应该会在列表中看到您的插件:

\u003cimg src="//assets-meta-cdck-prod-meta.s3.dualstack.us-west-1.amazonaws.com/original/3X/4/7/47a4b274553bd1fb0bba2d2df699ac136ad6a5cc.png" width="690" height="104"\u003e

恭喜,您刚刚创建了您的第一个插件!

让我们添加一些 Javascript

目前您的插件没有任何作用。让我们添加一个 Javascript 文件,它将在 Discourse 加载时弹出一个警告框。这对任何用户来说都非常烦人,不建议作为实际插件,但这将展示如何将 Javascript 插入到我们正在运行的应用程序中。

创建以下文件:

plugins/basic-plugin/assets/javascripts/discourse/initializers/alert.js

export default {
  name: "alert",
  initialize() {
    alert("alert boxes are annoying!");
  },
};

现在,如果您重启本地服务器,屏幕上应该会出现“alert boxes are annoying!”。(如果未出现,请参阅上面的“我的更改没有生效”标题)。

让我们逐步分析它是如何工作的:

  1. 放置在 assets/javascripts/discourse/initializers 中的 Javascript 文件在 Discourse 应用程序加载时会自动执行。

  2. 此特定文件 export 一个对象,该对象具有 nameinitialize 函数。

  3. name 必须是唯一的,所以我只是称它为 alert

  4. initialize() 函数在应用程序加载时被调用。在我们的例子中,它所做的只是执行我们的 alert() 代码。

您现在是一名官方的 Discourse 插件开发者了!


系列中的更多内容

第 1 部分:本主题
第 2 部分:插件插口
第 3 部分:站点设置
第 4 部分:git 设置
第 5 部分:管理界面
第 6 部分:验收测试
第 7 部分:发布您的插件


\u003csmall\u003e本文档已版本控制 - 在 github 上 建议更改。\u003c/small\u003e

109 个赞
Plugin API documentations?
How to create a new plugins?
How do you learn to build Discourse plugins?
Connecting to database with a plugin
Separating View from Data, Plugins, Modularity
Categories Topic Replies
Added custom initializer
Overwriting controllers in plugin
Create fully custom header
How do you learn to build Discourse plugins?
How can I add "dislike" button?
Which files can you override from a plugin?
Developing Discourse Plugins - Part 2 - Connect to a plugin outlet
Developing Discourse Plugins - Part 5 - Add an admin interface
(Superseded) Plugin Tutorial #1 - How to manipulate the text in the composer?
How to properly display jalali dates for Persian language
Topic Ratings Plugin
Enable Like-Button for not logged-in visitors
Why do I need a block storage?
Custom Field not working
Rails plugin generator
Why GNU License?
Code reading help. ES6, plugin-outlet `_connectorCache`, custom-html `_customizations`, appEvents
Customizing handlebars templates
Application Files after Digital Ocean Setup
Application Files after Digital Ocean Setup
Application Files after Digital Ocean Setup
Make an external http request from discourse
Specify user by external id
How can I make my own Discourse plugins?
Reputation and level on member profile
How to Change User Profile Picture
Something like shortcodes for Discourse?
Calendar plugin features to make it really useful for us
Plugin Documentation Style Guide
Any options for over-riding the username restrictions?
How would updating effect custom overrides?
I would like to change the template completely
Adding a second tag description
Rails plugin generator
Creating a plugin
Fatal: Not a git repository (or any parent up to mount point /discourse)
Fatal: Not a git repository (or any parent up to mount point /discourse)
Adding command line tools support for user api keys
Discourse Code Modification for personal use
Plugin routing problems
[PAID] Mentorship needed
Can You Restrict Signups Using `swot` Email Verification?
Plugin API documentations?
Insert tag based upon word match
Triggering account creation/login on external service when a user logs in on discourse
Place for total coding beginners to learn how to customise?
Add additional button post menu
Fetch third party data for topic list
The Ignore User feature (now in 2.3.0.beta7)
Help with a plugin
Create Custom APIs
Proper Development and Deployment
How to make only a group of users can send messages to staff
How might we better structure #howto?
Theme-Component v Plugin: What's the difference
Creating Parsing Extensions
How can I customized my own site
All latest images in posts from a category
(Ruby) Selecting only posts that are made by non-original posters
Hosting discourse on our infrastructure AWS
Display FullName instead of UserName in the Profile
Calling Python scripts w/ arguments on the backend via plugin?
Extend Existing Controller?
How to add custom fields to models
Extend Existing Controller?
Bot writer's tip: Processing every post
Add user to group after purchase
Support for proposed plug-in
Autocompletion for a user field at signup
Custom discourse with limited features
Overriding user_guardian.rb in a plugin (no fork necessary!)
Problems with git clone
Friendly user profile url
Website Integration
Discourse developement environment setup
How to show Full Name in Embeds
How to display full name in comments embeds
Rake task "assets:precompile" is failing due to JS Compilation issue
Change code does not work in VS Studio code editor
Edit Code
Discourse Chain Topics Plugin
Contributing to Discourse
How to edit topics/show.html.erb
Install Discourse on macOS for development
Developing Discourse Plugins - Part 3 - Add custom site settings
Developing Discourse Plugins - Part 4 - Setup git
Developing Discourse Plugins - Part 7 - Publish your plugin
Developing Discourse Plugins - Part 6 - Add acceptance tests
Learn how to start building stuff for Discourse if you're newbie (like myself)
How to disable updating username for admins & moderators?
Backend code
Backend code
Install Discourse on Ubuntu or Debian for Development
How to make changes on backend side of discourse application using rails
Introduction to Discourse Development
Discourse blog (article and comments)
How to learn more about Discourse back-end?
I want to learn how to program for Discourse
Discourse Audio Message
Create custom Automations
How can one add a toggle button to the post menu and implement an action for that toggle button in a Discourse plugin?
Content box
Create an advertising page
Discourse 有自定义插件模板吗,如何写自定义插件呢
Deploy custom Discourse repository via docker
Multiple custom fields for topics similar to custom userfields
How do I go about making a very customized theme?
How can I create a Discourse that's just like Stemaway.com?
Adding new columns to user lists in groups
How to pin a post within a topic?
Ruby script to send automatic welcome message to new users
:cn: Installing the Discourse development environment on Ubuntu 在 Ubuntu 上安装 Discourse 开发环境
Store pdf and doc files as raw text in the database - Where to start?
Admin not accessible because of auto-minify for Cloudflare
Update Password Encryption Method
I'm trying to run discourse app locally in production env without docker
How to turn off "Body seems unclear, is it a complete sentence?'
Hosting dynamic pages with discourse
Invisible way to force display order of tags?
Controllers and Routes
Learn about Discourse client-side flow?
Admin not accessible because of auto-minify for Cloudflare
Customizing CSS via plugin
About the Plugin category
Visual diagraming to add visual dimension to Conversations?
Custom Onebox engine
How can I install my own plugin written in Ruby?
Allow reply-to individual instead of topic/forum (mailing list feature)
Adapt github changes to my own site
Push to digital ocean from command line and rebuild
How to convince my university to use/install Discourse?
Hack to enable invitations for Trust level 1 users
Dropcaps in Discourse - cannot override span tag
Getting certain posts to Zapier
Plugin: add a menu icon (next to search)
Error When creating custom plugin
Error When creating custom plugin
IOTA as a currency for Discourse
New columns in directory (/users)
Bootstrap fails when plugin file specifies gem version range
Running my own discourse image
How to add a job to cron/anacron when creating Docker container
How to transfer data from plugin to app/views templates?
Which is Better? Discourse or Flarum?
Hide features for non-admin users through plugin
Customizing handlebars templates