Skip to content

Instantly share code, notes, and snippets.

@syrte
Created May 1, 2017 14:59
Show Gist options
  • Save syrte/8bd641a43f6b7e1c99f23e8c3ec82c4d to your computer and use it in GitHub Desktop.
Save syrte/8bd641a43f6b7e1c99f23e8c3ec82c4d to your computer and use it in GitHub Desktop.
Use Marked for TiddlyWiki. Originally fork from http://bjtools.tiddlyspot.com/

Use marked instead of markdown-js for Markdown parser of TiddlyWiki 5. Originally fork from http://bjtools.tiddlyspot.com/

Main New Features

  • support fenced code block
  • support code highlighting

Install

  1. Install the official Markdown and Hightlight.js plugin.

  2. Replace markdown.js with content of https://raw.githubusercontent.com/chjj/marked/master/lib/marked.js

  3. Replace wrapper.js with

/*\
title: $:/plugins/tiddlywiki/markdown/wrapper.js
type: application/javascript
module-type: parser

Wraps up the marked parser for use in TiddlyWiki5
Originally fork from http://bjtools.tiddlyspot.com

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";


var hljs = require("$:/plugins/tiddlywiki/highlight/highlight.js");
var marked = require("$:/plugins/tiddlywiki/markdown/markdown.js");

var HighLighter = function(str, lang) {
  try {
    if (lang && hljs.getLanguage(lang))
      return hljs.highlight(lang, str).value;
    else
      return hljs.highlightAuto(str).value;
  } catch (err) {
    return '';
  }
};

marked.setOptions({
  highlight: HighLighter,
  renderer: new marked.Renderer(),
  gfm: true,
  tables: true,
  breaks: true,
  pedantic: false,
  sanitize: false,
  smartLists: true,
  smartypants: false
});

var MarkdownParser = function(type, text, options) {
  this.tree = [{type: "raw", html: marked(text)}];
};

exports["text/x-markdown"] = MarkdownParser;

})();
  • Done
@syrte
Copy link
Author

syrte commented Dec 21, 2017

An update with markdown-it, supporting LaTeX, see https://gist.github.com/syrte/8062e11d8fec6221820c5f998239af61

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment