DefinitelyTyped/types/showdown/showdown-tests.ts
yisraelx 498c398948 [showdown] fix and reorder definitions, add and fix docs (#35104)
* docs(ShowdownOptions): add and fix

* fix(ShowdownOptions): add missing props

* `encodeEmails`
* `rawPrefixHeaderId `
* `rawHeaderId`

* fix(ShowdownOptions): Mark `excludeTrailingPunctuationFromURLs` as deprecated

It now default behavior:
d3ebff7ef0

* chore(Converter): order the method by the code order

https://github.com/showdownjs/showdown/blob/master/src/converter.js

* fix(Converter): add missing method `getFlavor`

* feat: add `Flavor` type for flavor names

* docs(Converter): fix and add

* chore(Showdown): order the mthods by the code order

https://github.com/showdownjs/showdown/blob/master/src/showdown.js

* fix(Showdown): Remove duplicate method definitions

* `getOption`
* `getOptions`
* `getDefaultOptions`
* `extension`
* `resetExtensions`

* fix(Showdown): add missing methods

* `getFlavor`
* `getFlavorOptions`
* `subParser`
* `validateExtension`

* fix(Showdown): fix incorrect definitions and order

* `setOption` - fix incorrect returns signature
* `getDefaultOptions` - fix incorrect args and returns signature
* `extension` - fix incorrect args and returns signature
* `ShowdownOptions` - support for custom options.
* `ShowdownOptionsSchema` - new interface to describe showdown options schema(`getDefaultOptions` method)
* `ShowdownExtensions` - new interface to describe showdown extensions store.
* `SubParser` - new type to describe showdown SubParser (`subParser` method)

* docs(Showdown): fix and add

* fix(ConverterOptions): incorrect property type of `extensions`

* fix: incorrect arg (`extension`) type of several method signatures

* test: fix and add

* fix(ShowdownExtension): add missing support of event listener

* add missing property 'listeners' type `Extension` (for listener extension and mixing)
* add new interface to describe showdown event listener function.

* fix(Converter): add missing `listen` method
2019-05-13 09:45:48 -07:00

136 lines
4.2 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 markdownToShowdownExt = {
type: 'lang',
regex: /markdown/g,
replace: 'showdown'
};
var commaExt: showdown.FilterExtension = {type: 'output', filter: text => text.replace(',', '')};
var myExt: showdown.ShowdownExtension = { type: 'lang', filter: (text, converter) => { return text.replace('#', '*') } };
var someExtArray: showdown.ShowdownExtension[] = [markdownToShowdownExt, commaExt];
var listenToCodeBlocksExt = {
type: 'listener',
listeners: {
"codeBlocks.before": (evtName: string) => {
console.log(evtName);
},
"codeBlocks.after": (evtName: string, text: string) => {
console.log(evtName);
return text;
}
}
};
var combinedExt = {
type: 'lang',
regex: /\s+/g,
replace: ' ',
listeners: {'paragraphs.after': console.log}
};
showdown.extension('my-ext', myExt);
showdown.extension('listen-ext', listenToCodeBlocksExt);
showdown.extension('combinedExt', () => [combinedExt]);
var preloadedExtensions = [ 'my-ext' ],
extensionsConverter = new showdown.Converter({ extensions: preloadedExtensions });
var preloadedMultipleExtensions = [
'my-ext',
{type: 'lang', filter: (text: string) => text.replace('h', 'H')},
() => [markdownToShowdownExt],
() => commaExt
],
multipleExtensionsConverter = new showdown.Converter({ extensions: preloadedMultipleExtensions });
var configuredConverter = new showdown.Converter();
configuredConverter.addExtension({type: 'output', filter(text: string){ return text.replace(' ', '_')} }, 'myext');
configuredConverter.addExtension([
{type: 'lang', filter: (text, converter)=>{return text.replace('#', '*')}},
{type: 'output', filter: (text, converter)=>{return text.replace('p', 'span')}}
], '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(multipleExtensionsConverter.makeHtml(exampleMarkdown));
// should log '<p>*Hello showdown</p>'
console.log(configuredConverter.makeHtml(exampleMarkdown));
// should log '<span>*hello,_markdown</p>'
console.log(converter.makeMarkdown(exampleHTML));
// should log '#hello, markdown'
configuredConverter.useExtension('listen-ext');
configuredConverter.addExtension(commaExt);
configuredConverter.addExtension([combinedExt]);
configuredConverter.addExtension(() => markdownToShowdownExt);
configuredConverter.removeExtension(combinedExt);
configuredConverter.addExtension(() => [listenToCodeBlocksExt, combinedExt]);
configuredConverter.listen('unescapeSpecialChars.after', (evtName, text) => `"${ text }"`);
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;
}
}
]
});
showdown.extension('myExt');
showdown.removeExtension('myExt');
showdown.extension('myExt', someExtArray);
showdown.resetExtensions()
showdown.extension('commaExt', () => commaExt)
showdown.validateExtension(markdownToShowdownExt);
showdown.validateExtension([markdownToShowdownExt, markdownToShowdownExt])
showdown.setOption('noHeaderId', true);
showdown.setOption('foo', 'bar'); // custom option
converter.setOption('tables', true);
converter.setOption('color', 'red'); // custom option
showdown.setFlavor('github');
console.log(showdown.getFlavor());
// should log 'github'
converter.setFlavor('ghost');
console.log(converter.getFlavor());
// should log 'ghost'