npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@j127/embed-discourse

v0.3.4

Published

Embed Discourse topics on other websites

Downloads

29

Readme

embed-discourse

This project contains Stencil web components that load a list of Discourse topics in other websites. You can use it with React, Vue, or any JavaScript project.

This is a work in progress. Send me a message if you want to make a pull request.

Installation

$ npm install @j127/embed-discourse

CORS Settings

To load Discourse topics from a domain or subdomain that is different from your Discourse forum, you will need to enable CORS for that domain.

Go to your Discourse settings and search for "cors". Look for the setting called "cors origins". Add the domain of the site that will embed the content there. For example, if you are running this repo in development mode (see below), it runs on localhost:3333, so you would add http://localhost:3333 to "cors origins". That would let your development server load topics from the forum.

Note the help text about the "cors origins" settings where it says that the DISCOURSE_ENABLE_CORS env variable must be set to true to enable CORS. You can enable that in your app.yml file.

React

You can use this component in React based projects.

Create React App

In index.js:

import {
    applyPolyfills,
    defineCustomElements,
} from "@j127/embed-discourse/loader";

applyPolyfills().then(() => {
    defineCustomElements();
});

Then in a React component:

function App() {
    return (
        <div className="App">
            <discourse-embed-topics forum-base-url="https://forum.example.com"></discourse-embed-topics>
        </div>
    );
}

You can pass in other props like this if you want to filter the topics:

<discourse-embed-topics
    forum-base-url="https://forum.example.com"
    tag="some-tagname"
    num-topics="5"
    offset="5"
    category-id="12"
></discourse-embed-topics>

See the full instructions on how to integrate Stencil components into React.

Gatsby

If using Gatsby, you can load the component like this:

useEffect(() => {
    async function loadTopics() {
        try {
            const topicLoader = require("@j127/embed-discourse/loader");
            await topicLoader.defineCustomElements(window);
        } catch (err) {
            console.error(err);
        }
    }
    loadTopics();
}, []);

Then put the component in your JSX in the same way as for create-react-app (above).

Vue

You can import this component in Vue like this in main.js:

import {
    applyPolyfills,
    defineCustomElements,
} from "@j127/embed-discourse/loader";

Vue.config.ignoredElements = [/test-\w*/];

applyPolyfills().then(() => {
    defineCustomElements();
});

Then in a component:

<template>
    <div id="app">
        <discourse-embed-topics
            forum-base-url="https://forum.example.com"
        ></discourse-embed-topics>
    </div>
</template>

<script>
import { DiscourseEmbedTopics } from "@j127/embed-discourse";

export default {
    name: "App",
};
</script>

For more information, see the full documentation for Vue integration.

Plain HTML Pages

You can load the component in any HTML page (including in WordPress, Drupal, and other systems that might not be using a JavaScript framework) something like this:

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="module">
            // Be sure to look at the brower support before using this technique:
            // https://caniuse.com/es6-module
            import { defineCustomElements } from "https://cdn.jsdelivr.net/npm/@j127/embed-discourse/dist/esm/loader.js";
            defineCustomElements();
        </script>
    </head>

    <body>
        <discourse-embed-topics
            forum-base-url="https://forum.example.com"
        ></discourse-embed-topics>
    </body>
</html>

ES6 modules only work in newer browsers, so you might want to use a module bundler/transpiler first.

See the examples/vanillajs.html file for additional examples.

For more information on using this component with plain HTML or other framworks, see the docs.

JavaScript Bundlers

You can use this module with JS bundlers like Webpack and Rollup. See this page for example configurations.

The distribution docs might also be useful.

Development

Run in development:

npm install
npm start

To build the component for production, run:

npm run build

To run the unit tests for the components, run:

npm test