Skip to content

Commit

Permalink
feat(flavours): add markdown presets/flavors
Browse files Browse the repository at this point in the history
This feature enables users to select a preset/flavor.
A flavor is just a preset of options, a shortcut so users don't have to set each option one by one.

Closes #164
  • Loading branch information
tivie committed Jul 12, 2015
1 parent 20ca099 commit 7e55bce
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 7 deletions.
49 changes: 47 additions & 2 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ showdown.Converter = function (converterOptions) {
_parseExtension(extensionName);
};

/**
* Set the flavor THIS converter should use
* @param {string} name
*/
this.setFlavor = function (name) {
if (flavor.hasOwnProperty(name)) {
var preset = flavor[name];
for (var option in preset) {
if (preset.hasOwnProperty(option)) {
options[option] = preset[option];
}
}
}
};

/**
* Remove an extension from THIS converter.
* Note: This is a costly operation. It's better to initialize a new converter
Expand Down
32 changes: 31 additions & 1 deletion src/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ var showdown = {},
ghCodeBlocks: true, // true due to historical reasons
tasklists: false
},
globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
globalOptions = JSON.parse(JSON.stringify(defaultOptions)),
flavor = {
github: {
omitExtraWLInCodeBlocks: true,
prefixHeaderId: 'user-content-',
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tablesHeaderId: true,
ghCodeBlocks: true,
tasklists: true
},
vanilla: JSON.parse(JSON.stringify(defaultOptions))
};

/**
* helper namespace
Expand Down Expand Up @@ -77,6 +91,22 @@ showdown.resetOptions = function () {
globalOptions = JSON.parse(JSON.stringify(defaultOptions));
};

/**
* Set the flavor showdown should use as default
* @param {string} name
*/
showdown.setFlavor = function (name) {
'use strict';
if (flavor.hasOwnProperty(name)) {
var preset = flavor[name];
for (var option in preset) {
if (preset.hasOwnProperty(option)) {
globalOptions[option] = preset[option];
}
}
}
};

/**
* Get the default options
* @static
Expand Down
28 changes: 28 additions & 0 deletions test/node/showdown.Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ describe('showdown.Converter', function () {
});
});

describe('setFlavor() github', function () {
var converter = new showdown.Converter(),
ghOpts = {
omitExtraWLInCodeBlocks: true,
prefixHeaderId: 'user-content-',
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tablesHeaderId: true,
ghCodeBlocks: true,
tasklists: true
};

converter.setFlavor('github');

for (var opt in ghOpts) {
if (ghOpts.hasOwnProperty(opt)) {
check(opt, ghOpts[opt]);
}
}
function check(key, val) {
it('should set ' + opt + ' to ' + val, function () {
converter.getOption(key).should.equal(val);
});
}
});

describe('extension methods', function () {
var extObjMock = {
type: 'lang',
Expand Down

0 comments on commit 7e55bce

Please sign in to comment.