Code Monkey home page Code Monkey logo

discord-markdown's Introduction

discord-markdown

A markdown parser for Discord messages.

Using

yarn add discord-markdown
npm i discord-markdown

For browser use, import dist/discord-markdown.min.js

const { parser, htmlOutput, toHTML } = require('discord-markdown');

console.log(toHTML('This **is** a __test__'));
// => This <strong>is</strong> a <u>test</u>

Fenced codeblocks will include highlight.js tags and classes.

Options

const { toHTML } = require('discord-markdown');
toHTML('This **is** a __test__', options);

options is an object with the following properties (all are optional):

  • embed: Boolean (default: false), if it should parse embed contents (rules are slightly different)
  • escapeHTML: Boolean (default: true), if it should escape HTML
  • discordOnly: Boolean (default: false), if it should only parse the discord-specific stuff
  • discordCallback: Object, callbacks used for discord parsing. Each receive an object with different properties, and are expected to return an HTML escaped string
    • user: (id: Number) User mentions "@someperson"
    • channel: (id: Number) Channel mentions "#somechannel"
    • role: (id: Number) Role mentions "@somerole"
    • everyone: () Everyone mention "@everyone"
    • here: () Here mention "@here"
  • cssModuleNames: Object, maps CSS class names to CSS module class names

Mention and Emoji Handling

Using the discordCallback option you can define custom functions to handle parsing mention and emoji content. You can use these to turn IDs into names.

Example:

const { toHTML } = require('discord-markdown');
toHTML('This is a mention for <@95286900801146880>', {
	discordCallback: {
		user: node => '@' + users[node.id];
	}
}); // -> This is a mention for @Brussell

Customizing

It is possible to change the rules used by discord-markdown. Take a look at the code to see how to create your own modified rule set.

Contributing

Find an inconsistency? File an issue or submit a pull request with the fix and updated test(s).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.