DefinitelyTyped/types/showdown/showdown-tests.ts
2018-12-11 10:56:49 -06:00

67 lines
2.0 KiB
TypeScript

// compile: tsc showdown/showdown-tests.ts --noImplicitAny --module commonjs
// run: node showdown/showdown-tests.js
import showdown = require('showdown');
var exampleMarkdown = '#hello, markdown',
exampleHTML = '<h1>hello, markdown</h1>',
converter = new showdown.Converter();
var myExt: showdown.ShowdownExtension = { type: 'output', filter: (text, converter) => { return text.replace('#', '*') } };
showdown.extension('my-ext', myExt);
var preloadedExtensions = [ 'my-ext' ],
extensionsConverter = new showdown.Converter({ extensions: preloadedExtensions });
var configuredConverter = new showdown.Converter();
configuredConverter.addExtension({type: 'output', filter: (text, converter)=>{return text.replace('#', '*')}}, 'myext');
configuredConverter.addExtension([
{type: 'output', filter: (text, converter)=>{return text.replace('#', '*')}},
{type: 'output', filter: (text, converter)=>{return text.replace('#', '*')}}
], 'myext');
console.log(showdown.helper);
console.log(converter.makeHtml(exampleMarkdown));
// should log '<h1 id="hellomarkdown">hello, markdown</h1>'
console.log(extensionsConverter.makeHtml(exampleMarkdown));
// should log '<p>*hello, markdown</p>'
console.log(configuredConverter.makeHtml(exampleMarkdown));
// should log '<p>*hello, markdown</p>'
console.log(converter.makeMarkdown(exampleHTML));
// should log '#hello, markdown'
showdown.extension('myExt', function () {
var matches: string[] = [];
return [
{
type: 'lang',
regex: /%start%([^]+?)%end%/gi,
replace: function (s: string, match: string) {
matches.push(match);
var n = matches.length - 1;
return '%PLACEHOLDER' + n + '%';
}
},
{
type: 'output',
filter: function (text) {
for (var i = 0; i < matches.length; ++i) {
var pat = '<p>%PLACEHOLDER' + i + '% *<\/p>';
text = text.replace(new RegExp(pat, 'gi'), matches[i]);
}
// reset array
matches = [];
return text;
}
}
]
});