diff --git a/showdown/showdown-tests.ts b/showdown/showdown-tests.ts index bb5adc8507..a7a33b783c 100644 --- a/showdown/showdown-tests.ts +++ b/showdown/showdown-tests.ts @@ -3,18 +3,53 @@ // compile: tsc showdown/showdown-tests.ts --noImplicitAny --module commonjs // run: node showdown/showdown-tests.js -import Showdown = require('showdown'); +import showdown = require('showdown'); var exampleMarkdown = '#hello, markdown', - converter = new Showdown.converter(), - preloadedExtensions = [ 'github', 'twitter', 'prettify', 'table' ], - extensionsConverter = new Showdown.converter({ extensions: preloadedExtensions }); + 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'); console.log(converter.makeHtml(exampleMarkdown)); // should log '
#hello, markdown
' +// should log '*hello, markdown
' -Showdown.forEach(preloadedExtensions, console.log); -// should log each item in the above array +console.log(configuredConverter.makeHtml(exampleMarkdown)); +// 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 = '%PLACEHOLDER' + i + '% *<\/p>';
+ text = text.replace(new RegExp(pat, 'gi'), matches[i]);
+ }
+ // reset array
+ matches = [];
+ return text;
+ }
+ }
+ ]
+});
diff --git a/showdown/showdown.d.ts b/showdown/showdown.d.ts
index 2a34defcb5..d2d085a9a4 100644
--- a/showdown/showdown.d.ts
+++ b/showdown/showdown.d.ts
@@ -1,37 +1,49 @@
-// Type definitions for Showdown 0.3.1
+// Type definitions for Showdown 1.4.1
// Project: https://github.com/coreyti/showdown
-// Definitions by: cbowdon some text with__underscores__in middle
+ *
+ * Becomes this:
+ * var foo = 'bar';
+ *
+ *
+ * @default false
+ */
+ omitExtraWLInCodeBlocks?: boolean;
+
+ /**
+ * Disable the automatic generation of header ids. Setting to true overrides prefixHeaderId.
+ *
+ * @default false
+ */
+ noHeaderId?: boolean;
+
+ /**
+ * Add a prefix to the generated header ids.
+ * Passing a string will prefix that string to the header id.
+ * Setting to true will add a generic 'section' prefix.
+ *
+ * @default false
+ */
+ prefixHeaderId?: string | boolean;
+
+ /**
+ * Enable support for setting image dimensions from within markdown syntax.
+ * Examples:
+ *
+ *  simple, assumes units are in px
+ *  sets the height to "auto"
+ *  Image with width of 80% and height of 5em
+ *
+ * @default false
+ */
+ parseImgDimensions?: boolean;
+
+
+ /**
+ * Set the header starting level. For instance, setting this to 3 means that
+ *
+ * # foo
+ *
+ * will be parsed as
+ *
+ * var foo = 'bar';
foo
+ *
+ * @default 1
+ */
+ headerLevelStart?: number;
+
+ /**
+ * Turning this on will stop showdown from interpreting underscores in the middle of
+ * words as and and instead treat them as literal underscores.
+ *
+ * Example:
+ *
+ * some text with__underscores__in middle
+ *
+ * will be parsed as
+ *
+ * strikethrough`.
+ *
+ * @default false
+ */
+ strikethrough?: boolean;
+
+ /**
+ * Enable support for tables syntax. Example:
+ *
+ * | h1 | h2 | h3 |
+ * |:------|:-------:|--------:|
+ * | 100 | [a][1] | ![b][2] |
+ * | *foo* | **bar** | ~~baz~~ |
+ *
+ * See the wiki for more info
+ *
+ * @default false
+ */
+ tables?: boolean;
+
+ /**
+ * If enabled adds an id property to table headers tags.
+ *
+ * @default false
+ */
+ tablesHeaderId?: boolean;
+
+ /**
+ * Enable support for GFM code block style.
+ *
+ * @default true
+ */
+ ghCodeBlocks?: boolean;
+
+ /**
+ * Enable support for GFM takslists. Example:
+ *
+ * - [x] This task is done
+ * - [ ] This is still pending
+ *
+ * @default false
+ */
+ tasklists?: boolean;
+
+ /**
+ * Prevents weird effects in live previews due to incomplete input.
+ *
+ * @default false
+ */
+ smoothLivePreview?: boolean;
+ }
+
+
+ interface ConverterOptions extends ShowdownOptions {
+
+ extensions?: string | string[];
}
interface Converter {
@@ -59,28 +200,131 @@ declare namespace Showdown {
* @return The output HTML
*/
makeHtml(text: string): string;
+
+ /**
+ * Setting a "local" option only affects the specified Converter object.
+ *
+ * @param optionKey
+ * @param string
+ */
+ setOption(optionKey: string, value: string): void;
+
+ /**
+ * Get the option of this Converter instance.
+ *
+ * @param optionKey
+ */
+ getOption(optionKey: string): any;
+
+ /**
+ * Get the options of this Converter instance.
+ */
+ getOptions(): ShowdownOptions;
+
+ /**
+ * Add extension to THIS converter.
+ *
+ * @param extension
+ * @param name
+ */
+ addExtension(extension: ShowdownExtension, name: string): void;
+
+ /**
+ * Use a global registered extension with THIS converter
+ *
+ * @param extensionName Name of the previously registered extension.
+ */
+ useExtension(extensionName: string): void;
+
+ /**
+ * Get all extensions.
+ *
+ * @return all extensions.
+ */
+ getAllExtensions(): ConverterExtensions;
+
+ /**
+ * Remove an extension from THIS converter.
+ *
+ * Note: This is a costly operation. It's better to initialize a new converter
+ * and specify the extensions you wish to use.
+ *
+ * @param extensions
+ */
+ removeExtension(extensions: ShowdownExtension[] | ShowdownExtension): void;
}
interface ConverterStatic {
/**
* @constructor
- * @param converter_options Configuration object, describes which extensions to apply
+ * @param converterOptions Configuration object, describes which extensions to apply
*/
- new(converter_options?: ConverterOptions): Converter;
+ new(converterOptions?: ConverterOptions): Converter;
}
/** Constructor function for a Converter */
- var converter: ConverterStatic;
-
- /** Registered extensions */
- var extensions: { [name: string]: ShowdownExtension };
+ var Converter: ConverterStatic;
/**
- * Showdown defers to the ES5 native or other predefined forEach where available, otherwise simple loop
- * @param obj An array of items
- * @param callback Applied once to each item (signature here is from forEach in lib.d.ts)
+ * Setting a "global" option affects all instances of showdown
*/
- function forEach