From e471281578ec5c9db876a8e8efcfeb12f0ff5c09 Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Thu, 18 Dec 2014 14:18:12 +1300 Subject: [PATCH 1/6] Make this usable with browserify - Add dependencies to package.json - Wrap the lib with AMD / CommonJS checks - Document use in nodejs --- .gitignore | 1 + .jshintrc | 26 ++++++++++++++++++ README.md | 19 ++++++++++++- markdown.js | 77 ++++++++++++++++++++++++++++++++-------------------- package.json | 3 ++ 5 files changed, 96 insertions(+), 30 deletions(-) create mode 100644 .jshintrc diff --git a/.gitignore b/.gitignore index fbe05fc..0cfc89f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bower_components/ +node_modules/ \ No newline at end of file diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..d430ef2 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,26 @@ +{ + "node": true, + "browser": true, + "esnext": true, + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 2, + "latedef": true, + "newcap": true, + "noarg": true, + "quotmark": "single", + "regexp": true, + "undef": true, + "unused": true, + "strict": true, + "trailing": true, + "smarttabs": true, + "maxdepth": 2, + "maxcomplexity": 10, + "globals": { + "angular": false + } +} diff --git a/README.md b/README.md index dbfcff3..f5cee7c 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,29 @@ Bower Component for a simple AngularJS Markdown directive using [Showdown](https ## Usage + +### Bower + 1. `bower install angular-markdown-directive` 2. Include `angular-sanitize.js`. It should be located at `bower_components/angular-sanitize/`. 3. Include `showdown.js`. It should be located at `bower_components/showdown/`. 4. Include `markdown.js` provided by this component into your app. 5. Add `btford.markdown` as a module dependency to your app. -6. Insert the `btf-markdown` directive into your template: + +### NPM (and browserify) + +1. `npm install --save angular-markdown-directive` +2. `npm install --save angular-sanitize` +4. Require `angular-sanitize` and `angular-markdown-directive/markdown` provided by this component into your app. + ``` + require('angular-sanitize/angular-sanitize'); + require('angular-markdown-directive/markdown.js'); + ``` +5. Add `btford.markdown` as a module dependency to your app + +### Using the directive + +Insert the `btf-markdown` directive into your template: ```html diff --git a/markdown.js b/markdown.js index 989a6be..3668509 100644 --- a/markdown.js +++ b/markdown.js @@ -4,33 +4,52 @@ * License: MIT */ -'use strict'; +'format global'; /* global define */ +'deps angular'; +'deps angular-sanitize'; +'deps showdown'; -angular.module('btford.markdown', ['ngSanitize']). - provider('markdownConverter', function () { - var opts = {}; - return { - config: function (newOpts) { - opts = newOpts; - }, - $get: function () { - return new Showdown.converter(opts); - } - }; - }). - directive('btfMarkdown', ['$sanitize', 'markdownConverter', function ($sanitize, markdownConverter) { - return { - restrict: 'AE', - link: function (scope, element, attrs) { - if (attrs.btfMarkdown) { - scope.$watch(attrs.btfMarkdown, function (newVal) { - var html = newVal ? $sanitize(markdownConverter.makeHtml(newVal)) : ''; - element.html(html); - }); - } else { - var html = $sanitize(markdownConverter.makeHtml(element.text())); - element.html(html); - } - } - }; - }]); +(function () { + 'use strict'; + + function angularMarkdown(angular, Showdown) { + + angular.module('btford.markdown', ['ngSanitize']) + .constant('Showdown', Showdown) + .provider('markdownConverter', ['Showdown', function (Showdown) { + var opts = {}; + return { + config: function (newOpts) { + opts = newOpts; + }, + $get: function () { + return new Showdown.converter(opts); + } + }; + }]) + .directive('btfMarkdown', ['$sanitize', 'markdownConverter', function ($sanitize, markdownConverter) { + return { + restrict: 'AE', + link: function (scope, element, attrs) { + if (attrs.btfMarkdown) { + scope.$watch(attrs.btfMarkdown, function (newVal) { + var html = newVal ? $sanitize(markdownConverter.makeHtml(newVal)) : ''; + element.html(html); + }); + } else { + var html = $sanitize(markdownConverter.makeHtml(element.text())); + element.html(html); + } + } + }; + }]); + } + + if (typeof define === 'function' && define.amd) { + define('angular-markdown-directive', ['angular', 'moment'], angularMarkdown); + } else if (typeof module !== 'undefined' && module && module.exports) { + angularMarkdown(angular, require('Showdown')); + } else { + angularMarkdown(angular, window.Showdown); + } +})(); \ No newline at end of file diff --git a/package.json b/package.json index b245c18..d8eb53f 100644 --- a/package.json +++ b/package.json @@ -27,5 +27,8 @@ "karma-jasmine": "^0.1.5", "karma-firefox-launcher": "^0.1.3", "karma-chrome-launcher": "^0.1.3" + }, + "dependencies": { + "showdown": "git+https://github.com/ushahidi/showdown#fix-browserify" } } From a8c3a94d5170120aa92811fe7a6853dcad2bcecf Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Fri, 19 Dec 2014 07:57:45 +1300 Subject: [PATCH 2/6] Fix karma tests --- markdown.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/markdown.js b/markdown.js index 3668509..34d6152 100644 --- a/markdown.js +++ b/markdown.js @@ -7,7 +7,7 @@ 'format global'; /* global define */ 'deps angular'; 'deps angular-sanitize'; -'deps showdown'; +'deps Showdown'; (function () { 'use strict'; @@ -47,7 +47,7 @@ if (typeof define === 'function' && define.amd) { define('angular-markdown-directive', ['angular', 'moment'], angularMarkdown); - } else if (typeof module !== 'undefined' && module && module.exports) { + } else if (typeof module !== 'undefined' && module && module.exports && typeof require !== 'undefined') { angularMarkdown(angular, require('Showdown')); } else { angularMarkdown(angular, window.Showdown); From a639eba86a52b413e77349015c44f23a5b5319ca Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Fri, 19 Dec 2014 08:01:52 +1300 Subject: [PATCH 3/6] Remove stray reference to moment instead of showdown --- markdown.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markdown.js b/markdown.js index 34d6152..b81b126 100644 --- a/markdown.js +++ b/markdown.js @@ -46,7 +46,7 @@ } if (typeof define === 'function' && define.amd) { - define('angular-markdown-directive', ['angular', 'moment'], angularMarkdown); + define('angular-markdown-directive', ['angular', 'showdown'], angularMarkdown); } else if (typeof module !== 'undefined' && module && module.exports && typeof require !== 'undefined') { angularMarkdown(angular, require('Showdown')); } else { From fda15af798697b2749f38edc8f484ed64564dd47 Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Fri, 19 Dec 2014 08:02:09 +1300 Subject: [PATCH 4/6] Module name should be 'showdown' not 'Showdown' --- markdown.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/markdown.js b/markdown.js index b81b126..802ecba 100644 --- a/markdown.js +++ b/markdown.js @@ -7,7 +7,7 @@ 'format global'; /* global define */ 'deps angular'; 'deps angular-sanitize'; -'deps Showdown'; +'deps showdown'; (function () { 'use strict'; @@ -48,7 +48,7 @@ if (typeof define === 'function' && define.amd) { define('angular-markdown-directive', ['angular', 'showdown'], angularMarkdown); } else if (typeof module !== 'undefined' && module && module.exports && typeof require !== 'undefined') { - angularMarkdown(angular, require('Showdown')); + angularMarkdown(angular, require('showdown')); } else { angularMarkdown(angular, window.Showdown); } From 4bfeedf8578022aa492ce2ef6bc6f21fdf97970a Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Fri, 19 Dec 2014 08:06:35 +1300 Subject: [PATCH 5/6] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d8eb53f..e02719c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-markdown-directive", - "version": "0.3.1", + "version": "0.3.1-browserify.1", "description": "simple AngularJS markdown directive with showdown", "main": "markdown.js", "scripts": { From 4cea9cd8923eada8200afea055e8880e6f255675 Mon Sep 17 00:00:00 2001 From: Robbie Mackay Date: Wed, 25 Feb 2015 13:36:07 +1300 Subject: [PATCH 6/6] Update to use showdown2 --- markdown.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/markdown.js b/markdown.js index 802ecba..8360386 100644 --- a/markdown.js +++ b/markdown.js @@ -23,7 +23,7 @@ opts = newOpts; }, $get: function () { - return new Showdown.converter(opts); + return new Showdown.Converter(opts); } }; }]) diff --git a/package.json b/package.json index e02719c..94a93d4 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,6 @@ "karma-chrome-launcher": "^0.1.3" }, "dependencies": { - "showdown": "git+https://github.com/ushahidi/showdown#fix-browserify" + "showdown": "git+https://github.com/showdownjs/showdown#showdown2" } }