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

marked-alm

v0.0.2

Published

Marked.js extension for AniList-flavored Markdown

Downloads

81

Readme

Anilist-Flavored Markdown (unofficial)

npm install size downloads github actions snyk

  • An extension for the Marked library that supports parsing the AniList markdown spec.
  • This library is not officially supported by AniList, so future changes to AniList may break its functionality.

Demo

Check out the example of how to use this library with the Svelte framework. Other front-end frameworks can be used similarly

Installation

npm:

npm install marked-alm

yarn:

yarn add marked-alm

CDN:

<!-- Using jsDelivr CDN -->
<script src="https://cdn.jsdelivr.net/npm/marked-alm/marked-alm.min.js"></script>
<!-- Or using unpkg CDN -->
<script src="https://unpkg.com/marked-alm/marked-alm.min.js"></script>

Usage

import { marked } from "marked";
import { markedAlm } from "marked-alm";
import createDOMPurify from "dompurify";

// Create an HTML sanitizer function
// the code below is similar to the code found in the source code of https://anilist.co

const DOMPurify = createDOMPurify(); // In Node.js, this line of code will cause an error
// because Node.js does not have a global window variable.
// Therefore, you need to use JSDOM to create a mock window.
// Please refer to the dompurify documentation for implementation details.

DOMPurify.addHook("afterSanitizeAttributes", (currentNode) => {
  "target" in currentNode &&
    (currentNode.setAttribute("target", "_blank"),
      currentNode.setAttribute("rel", "noopener noreferrer"));
});
const sanitize = (htmlString: string) => {
  return DOMPurify.sanitize(htmlString, {
    ALLOWED_TAGS: [
      "a",
      "b",
      "blockquote",
      "br",
      "center",
      "del",
      "div",
      "em",
      "font",
      "h1",
      "h2",
      "h3",
      "h4",
      "h5",
      "hr",
      "i",
      "img",
      "li",
      "ol",
      "p",
      "pre",
      "code",
      "span",
      "strike",
      "strong",
      "ul",
    ],
    ALLOWED_ATTR: [
      "align",
      "height",
      "href",
      "src",
      "target",
      "width",
      "rel",
    ],
  });
};

// Create marked alm
const { extensions, hooks, tokenizer } = markedAlm({
  preprocessOptions: {
    // Handle youtube embedded in markdown
    youtube({ type, selector, youtube, renderer }) {
      // type: 'youtube'
      // selector: the selector of the placeholder tag added by the parser to the HTML parsed from markdown
      // youtube: { id: string } - the data for the YouTube video
      // renderer: a string of HTML used to override the default placeholder tag (not recommended as it can lead to various issues, use with caution)
    },

    // Handle user taged in markdown
    user({ type, user, renderer }) {
      // type: 'user'
      // user: { username: string } - the data for the Anilist user
      // renderer: a string of HTML used to override the default placeholder tag (not recommended as it can lead to various issues, use with caution)
    },

    // Handle video embedded in markdown
    video({ type, selector, video, renderer }) {
      // type: 'video'
      // selector: the selector of the placeholder tag added by the parser to the HTML parsed from markdown
      // video: { src: string } - the data for the video
      // renderer: a string of HTML used to override the default placeholder tag (not recommended as it can lead to various issues, use with caution)
    }
  },

  postprocessOptions: {
    // Add html sanitization
    sanitize(htmlString) {
      // Accepts an unsanitized HTML string and returns a sanitized HTML string
      return sanitize(htmlString)
    },
    // Handle Spoiler
    spoiler({ type, selector, spoiler, renderer }) {
      // type: 'spoiler'
      // selector: the selector of the placeholder tag added by the parser to the HTML parsed from markdown
      // spoiler: { contents: string } - the data for the spoiler
      // renderer: a string of HTML used to override the default placeholder tag (not recommended as it can lead to various issues, use with caution)
    }
    // Handle Anime/Manga embedded in markdown
    media({ type, selector, media, renderer }) {
      // type: 'media'
      // selector: the selector of the placeholder tag added by the parser to the HTML parsed from markdown
      // media: { id: string; type: "ANIME" | "MANGA" } - the data for the media
      // renderer: a string of HTML used to override the default placeholder tag (not recommended as it can lead to various issues, use with caution)
    },
  }
});

// Config marked
marked.setOptions({
  gfm: true,
  breaks: true,
  pedantic: false,
});

// Add marked alm extension to marked
marked.use(extensions);
marked.use(hooks);
marked.use(tokenizer);

const markdownParser = (markdown: string) => {
  const htmlString = marked(markdown) as string;
  return htmlString
};

// Please see the Demo section above for a better understanding of how to use it.