Giter Site home page Giter Site logo

rhysd / marked-sanitizer-github Goto Github PK

View Code? Open in Web Editor NEW
14.0 4.0 8.0 4.54 MB

A sanitizer for marked.js which sanitizes HTML elements in markdown with the same manner as GitHub

License: MIT License

TypeScript 100.00%
markdown sanitize github marked

marked-sanitizer-github's Introduction

Port of GitHub's Markdown Sanitizer for marked

npm version badge Build Status Coverage Status

marked-sanitizer-github provides a sanitizer to sanitize HTML elements in Markdown documents. The implementation was ported from html-pipeline.

marked provides sanitization by default. But it does not allow any HTML elements and escapes all of them in a parsing Markdown document. By using marked-sanitizer-github, some safe HTML elements are available.

When a sanitizer detects broken HTML elements (e.g. not closing element), it escapes all elements after that.

This package was created to be used for Shiba.

⚠️ WARNING ⚠️

From marked v0.7.0, sanitize option was deprecated. Please read the usage document of marked for more details.

Installation

$ npm install --save marked-sanitizer-github

Usage

It exports one class SanitizeState because the sanitization is stateful. You can get a sanitizer for marked parser by calling getSanitizer() method. It returns a function object to sanitize.

const marked = require('marked');
const SanitizeState = require('marked-sanitizer-github').default;

const md = `some document`;

const state = new SanitizeState();

// Convert a markdown document to HTML with sanitization
const html = marked(md, {
    sanitize: true,
    sanitizer: state.getSanitizer(),
});

console.log(html);

SanitizeState class also provides reset() method, isBroken() method and isInUse() method.

reset() method resets the sanitization state. If you use the SanitizeState object multiple times, you must call the method before parsing a markdown document.

isBroken() method returns whether the state is broken. A broken state means that Some HTML elements in a sanitized document were broken (e.g. tag mismatch, closing tag does not appear, ...).

You can have a callback to know the reason why the document is broken as follows:

state.onDetectedBroken = (reason, tag) => {
    console.error(`Broken HTML around '${tag}' tag: ${reason}`);
};

isInUse() method returns whether the state object has ongoing state or is ready for parsing a new document. true means the internal state is in use (not ready for parsing a new document). Returning true means it requires to call reset() method before parsing a new document.

Sanitized elements

  • Allowed elements: h1, h2, h3, h4, h5, h6, h7, h8, br, b, i, strong, em, a, pre, code, img, tt, div, ins, del, sup, sub, p, ol, ul, table, thead, tbody, tfoot, blockquote, dl, dt, dd, kbd, q, samp, var, hr, ruby, rt, rp, li, tr, td, th, s, strike, summary and details
  • Allowed attributes: Only following attributes are allowed for allowed elements.
    • a: href
    • img: src and longdesc
    • div: itemscope and itemtype
    • blockquote: cite
    • del: cite
    • ins: cite
    • q: cite
    • ALL: abbr, accept, accept-charset, accesskey, action, align, alt, axis, border, cellpadding, cellspacing, char, charoff, charset, checked, clear, cols, colspan, color, compact, coords, datetime, dir, disabled, enctype, for, frame, headers, height, hreflang, hspace, ismap, label, lang, maxlength, media, method, multiple, name, nohref, noshade, nowrap, open, prompt, readonly, rel, rev, rows, rowspan, rules, scope, selected, shape, size, span, start, summary, tabindex, target, title, type, usemap, valign, value, vspace, width and itemprop
  • Allowed protocols in attributes: Only following protocols are allowed as values of allowed attributes
    • a:
      • href: http, https, mailto, github-windows and github-mac
    • blockquote:
      • cite: http and https
    • del:
      • cite: http and https
    • ins:
      • cite: http and https
    • q:
      • cite: http and https
    • img:
      • src: http and https
      • longdesc: http and https
  • li must be nested in ul and ol
  • Table items (tr, td and th) and table headers (thead, tbody and tfoot) must be nested in table

License

MIT License

marked-sanitizer-github's People

Contributors

dependabot[bot] avatar rhysd avatar victornicollet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

marked-sanitizer-github's Issues

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.