In this tutorial, we’ll create a plugin that allows you to search for NPM packages and MDN pages.
Create your plugin using the template. You should have something like this now:
Let’s add a response for when no command is selected:
NPM Search
Let’s add a response for when the user types npm without any package name.
After googling for NPM search API, I found
this documentation ,
which looks exactly like what we will need for the plugin.
We’ll need the package name, version, description, and NPM link.
Let’s describe this data in a class so we can deserialize JSON into this class.
Create a new file called NpmResponse.cs next to your Main.cs file.
This is where we will put all the classes that describe the data we get from the API.
As we see from the documentation, the API returns an object with an objects property.
Now let’s describe the NpmObject class.
It has the following properties: package, score, searchScore.
We’re only interested in the package one, so let’s describe it.
The package property has a few more properties interesting to us. Namely, these are:
name — so we can display the name of the package we found
version — so we can display the version of the package
description — so we can display the description of the package
links — so we can let the user of our plugin go directly to the NPM page of the package
And lastly, the links property has two properties we’re interested in:
npm and homepage, so we could open these links for the user of our plugin.
Now that we have our data classes,
let’s add a method to our plugin that will search for NPM packages and display search results.
We have successfully implemented the NPM search functionality.
Assuming you specified web as your keyword when creating the plugin, here’s how it should look:
web npm svelte
svelte | v5.1.9
Cybernetically enhanced web apps
Alt+1
hast-util-to-jsx-runtime | v2.3.2
hast utility to transform to preact, react, solid, svelte, vue, etc
Alt+2
eslint-plugin-svelte | v2.46.0
ESLint plugin for Svelte using AST
Alt+3
ai | v3.4.31
AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript
Alt+4
@sveltejs/kit | v2.7.4
SvelteKit is the fastest way to build Svelte apps
Alt+5
embla-carousel-react | v8.3.1
A lightweight carousel library with fluid motion and great swipe precision
Alt+6
MDN Search
MDN search is a little bit different.
We’ll be doing the search ourselves, locally.
For that, we’ll need to download the full list of MDN pages.
But before that, let’s add a response for when the user types mdn without any search query.
Now, let’s download the MDN data.
It’s located
here .
This is an array of objects, each object representing an MDN page.
Each object only has two properties: title and url.
Simple enough.
Let’s begin!
First, create a new file called MdnResponse.cs next to your Main.cs file.
In this file, we’ll describe the object structure of the MDN data.
Now we need to actually download that data. We’ll do this on plugin startup, using the Init attribute:
Now that we have the data, let’s add a method to our plugin that will search for MDN pages and display search results.