/markdown-viewer

Markdown Viewer / Browser Extension

Primary LanguageCSSMIT LicenseMIT

Markdown Viewer / Browser Extension

Install: Chrome / Firefox / Donate: PayPal

Features

  • No special permissions required for file URLs
  • Full control over the allowed origins
  • Supports multiple markdown parsers
  • Full control over the compiler options (marked or remark)
  • Themes support (including GitHub theme) (jasonm23, mixu, cobalt)
  • Supports GitHub Flavored Markdown
  • Syntax highlighted code blocks (prism)
  • Generates Table of Contents (TOC)
  • Remembers scroll position
  • Emoji support (Icons provided free by EmojiOne)
  • MathJax support
  • Settings synchronization
  • Raw and rendered markdown views
  • Detects markdown by header and path
  • Toggle Content Security Policy
  • Override page encoding
  • Built as event page
  • Free and Open Source

Table of Contents

After Install

Local Files

  1. Navigate to chrome://extensions
  2. Make sure that the Allow access to file URLs checkbox is checked for the Markdown Viewer extension

file-urls

Navigate to file:/// in your browser and locate the markdown files that you want to read

Remote Files

  1. Click on the Markdown Viewer icon and select Advanced Options
  2. Add the origins that you want enabled for the Markdown Viewer extension

Compiler Options

Marked

Option Default Description
breaks false Enable GFM line breaks. This option requires the gfm option to be true.
gfm true Enable GFM GitHub Flavored Markdown.
pedantic false Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
sanitize false Sanitize the output. Ignore any HTML that has been input.
smartLists false Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
smartypants false Use "smart" typographic punctuation for things like quotes and dashes.
tables true Enable GFM tables. This option requires the gfm option to be true.

Remark

Option Default Description
breaks false Enable GFM line breaks. This option requires the gfm option to be true.
commonmark false Toggle CommonMark mode.
footnotes false Toggle reference footnotes and inline footnotes.
gfm true Enable GFM GitHub Flavored Markdown.
pedantic false Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
sanitize false Sanitize the output. Ignore any HTML that has been input.

Content Options

Option Default Description
emoji false Convert emoji :shortnames: into EmojiOne images
scroll true Remember scroll position
toc false Generate Table of Contents
mathjax false Render TeX and LaTeX math blocks

Scroll

  • When enabled, the scroll option remembers the current scroll position and scrolls back to it after page load.
  • When disabled, the scroll option either scrolls to the top of the document or to a certain header (anchor) if a hash URL fragment is present.

TOC

  • Generates Table of Contents (TOC) based on the headers found in the markdown document.

MathJax

The following mathjax delimiters are supported:

  • inline math: \(math\) and $math$
  • display math: \[math\] and $$math$$

The following rules apply to your content when mathjax is enabled:

  • Regular dollar sign $ in text that is not part of a math formula should be escaped: \$
  • Regular markdown escaping for parentheses: \( and \), and brackets: \[ and \] is not supported. MathJax will convert anything between these delimiters to math formulas, unless they are wrapped in backticks: `\(` or fenced code blocks.

The MathJax support currently works only on local file URLs and remote origins without strict Content Security Policy (CSP) set. For example it won't work for files hosted on the GitHub's raw.githubusercontent.com origin. However you can bypass this by enabling the Disable CSP option.

Emoji

  • Emoji shortnames like: :sparkles: will be converted to ✨ using EmojiOne images.
  • Currently unicode symbols like and ASCII emoji like :D are not supported.

The Emoji support currently works only on local file URLs and remote origins without strict Content Security Policy (CSP) set. For example it won't work for files hosted on the GitHub's raw.githubusercontent.com origin. However you can bypass this by enabling the Disable CSP option.

Advanced Options

Detecting and rendering local file URLs can be enabled by using the Allow access to file URLs option for the extension.

Access to remote URLs however, needs to be enabled manually.

Add Origin

Here is how you can enable the extension for the https://raw.githubusercontent.com origin:

add-origin

The origin consists of protocol part and domain part. The protocol can be either https, http, or a * to match both https and http.

Enable the above origin and play around with the extension options here.

Allow All Origins

In case you really want to you can enable the extension for all origins:

all-origins

Alternatively you can use the Allow All button.

Note: Take a look at the Path Matching Priority section below to see how the Markdown content is being included for or excluded from rendering!

Header Detection

When this option is enabled the extension will check for the presence of the text/markdown and text/x-markdown content-type header before trying to match the path:

header-detection

Path Matching

If the header detection is disabled or a proper content-type header is missing, the extension will check if the URL is ending with a markdown file extension.

The default regular expression is: \.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\?.*)?$

It's a simple regular expression that matches URLs ending with:

  • markdown file extension: \.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)
  • and optionally anchor or querystring after that: (?:#.*|\?.*)?

The ?: used in (?:match) stands for non-capturing group and it's there for performance reasons.

You can modify the path matching regular expression for each enabled origin individually. The settings are being updated as you type.

Path Matching Priority

Let's assume that we have allowed the following origins and have set their respective Path Matching RegExp accordingly:

Origin Match
*://* \.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\?.*)?$
*://asite.com \/some\/custom\/path\/to\/serve\/markdown\/$
*://github.com something impossible to match
*://gitlab.com .*\/raw\/.*\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\?.*)?$
  1. In this example we have allowed all origins *://* (the first entry) with the default Path Matching RegExp, meaning that all origins are going to match only on those URLs ending with markdown file extension.
    For example this is going to match: https://raw.githubusercontent.com/simov/markdown-viewer/master/README.md
    However it's important to note that the *://* (Allow All) origin serves as a fallback. Its Path Matching RegExp will be used only if any other explicitly added (more specific) origins fail to match, meaning that all other explicitly added origins override the *://* (Allow All) origin and therefore its Path Matching RegExp!

  2. For example let's assume that our imaginary origin asite.com (the second entry) serves markdown files on a custom path: https://asite.com/some/custom/path/to/serve/markdown/
    In this case we'll have to explicitly add an entry for asite.com and modify its path matching RegExp even if the extension was enabled for all origins!

  3. Now take a look at the third entry. GitHub is a good example of a website that serves rendered HTML content on URLs ending with markdown file extension.
    For example we would like the extension to render:
    https://raw.githubusercontent.com/simov/markdown-viewer/master/README.md
    but not:
    https://github.com/simov/markdown-viewer/blob/master/README.md
    In this case we want to exclude the github.com origin althogether. To exclude an origin you have to set its path matching RegExp to something that's impossible to match!

  4. Moving on to the forth entry. What if we want to match things like:
    https://gitlab.com/gitlab-org/gitlab-ce/raw/master/README.md
    but not:
    https://gitlab.com/gitlab-org/gitlab-ce/blob/master/README.md
    Notice the subtle difference between the two URLs - the first one contains the raw word in its path. Now take a closer look at the table above and you'll notice that the gitlab.com entry have a slightly modified path matching RegExp too - it have the .*\/raw\/.* string prepended in front of the default RegExp that matches every path ending with markdown file extension.
    The result is as you might expect, the first URL:
    https://gitlab.com/gitlab-org/gitlab-ce/raw/master/README.md is going to be matched and rendered by Markdown Viewer, however the second one:
    https://gitlab.com/gitlab-org/gitlab-ce/blob/master/README.md will be excluded!

Remove Origin

At any point click on the REMOVE button for the origin that you want to remove. This actually removes the permission itself so that the extension is no longer able to inject code into that origin.

Note that the Chrome's consent popup shows up only when you add the origin for the first time. In case you re-add it you'll no longer see that popup. That's a Chrome thing and it's not controllable through the extension.

Refresh Origin

The extension synchronizes your preferences across all of your devices using Google Sync. The list of your allowed origins is being synced too. However, the actual permissions that you give using the Chrome's consent popup cannot be synced.

In case you've recently added a new origin on one of your devices you'll have to explicitly allow it on your other devices. The REFRESH button for each origin is used for that.

Disable Content Security Policy

Some remote origins may serve its content with a content-security-policy header set that prevents the extension from executing certain JavaScript code inside the content of that page. For example on raw.githubusercontent.com certain things such as remembering your scroll position, generating TOC, displaying MathJax or Emojis won't work.

Using the Disable Content Security Policy switch you can optionally tell the extension to strip that header from the incoming request and therefore allow its full functionality to work:

disable-csp

It's important to note that even if you enable this option, the Content Security Policy header will be stripped only for those requests that either have a correct Markdown Content Type or URL that matches the corresponding Path Matching RegExp for that origin.

Even if you have Allowed All Origins and disabled the Content Security Policy at the same time, the header will be stripped only for those requests that either have a correct Markdown Content Type or URL that matches your explicitly set Path Matching regex for the Allow All origin *://*.

Character Encoding

By default Markdown Viewer uses the browser's built-in encoding detection. In case you want to force a certain Character Encoding for a specific origin - use the Encoding select control.

Markdown Syntax and Features

A few files located in the test folder of this repo can be used to test what's possible with Markdown Viewer:

  • Add the raw.githubusercontent.com origin through the Advanced Options
  • Navigate to the test files: syntax, mathjax, yaml, toml and play around with the Compiler and Content options
  • Use the Markdown/HTML button to switch between raw markdown and rendered HTML
  • At any point click on the Defaults button to reset back the compiler options

Note that in order for the extension to fully function on the raw.githubusercontent.com origin you have to enable the Disable CSP option.

More Compilers

Markdown Viewer can be used with any markdown parser/compiler. Currently the following compilers are implemented: marked, remark, showdown, markdown-it, remarkable, commonmark, markdown-js.

  1. Clone the compilers branch
  2. Navigate to chrome://extensions
  3. Make sure that the Developer mode checkbox is checked at the top
  4. Disable the Markdown Viewer extension downloaded from the Chrome Store
  5. Click on the Load unpacked extension... button and select the cloned directory

License

The MIT License (MIT)

Copyright (c) 2013-present, Simeon Velichkov simeonvelichkov@gmail.com (https://github.com/simov/markdown-viewer)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.