Skip to content

Commit 770d1f3

Browse files
committed
Refactor library
* Throw if attaching without valid parser * Use descriptive name for plugin
1 parent e60eb87 commit 770d1f3

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

index.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
'use strict';
22

3-
var gemoji = require('gemoji').name;
3+
var byName = require('gemoji').name;
44

5-
module.exports = plugin;
5+
module.exports = gemoji;
66

77
var colon = ':';
88
var own = {}.hasOwnProperty;
99

10-
function plugin() {
11-
var proto = this.Parser.prototype;
12-
var methods = proto.inlineMethods;
10+
tokenize.locator = locate;
1311

14-
proto.inlineTokenizers.gemojiShortCode = tokenize;
12+
function gemoji() {
13+
var parser = this.Parser;
14+
var proto;
15+
16+
if (!isRemarkParser(parser)) {
17+
throw new Error('Missing parser to attach `remark-gemoji` to');
18+
}
19+
20+
proto = this.Parser.prototype;
1521

16-
methods.splice(methods.indexOf('strong'), 0, 'gemojiShortCode');
22+
proto.inlineTokenizers.gemojiShortCode = tokenize;
23+
proto.inlineMethods.splice(proto.inlineMethods.indexOf('strong'), 0, 'gemojiShortCode');
1724
}
1825

1926
function tokenize(eat, value, silent) {
@@ -33,7 +40,7 @@ function tokenize(eat, value, silent) {
3340

3441
subvalue = value.slice(1, pos);
3542

36-
if (!own.call(gemoji, subvalue)) {
43+
if (!own.call(byName, subvalue)) {
3744
return;
3845
}
3946

@@ -48,14 +55,13 @@ function tokenize(eat, value, silent) {
4855
/* Eat a text-node. */
4956
subvalue = colon + subvalue + colon;
5057

51-
return eat(subvalue)({
52-
type: 'text',
53-
value: subvalue
54-
});
58+
return eat(subvalue)({type: 'text', value: subvalue});
5559
}
5660

57-
tokenize.locator = locate;
58-
5961
function locate(value, fromIndex) {
6062
return value.indexOf(colon, fromIndex);
6163
}
64+
65+
function isRemarkParser(parser) {
66+
return Boolean(parser && parser.prototype && parser.prototype.inlineTokenizers);
67+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
"browserify": "^14.0.0",
2828
"esmangle": "^1.0.0",
2929
"nyc": "^11.0.0",
30-
"remark": "^8.0.0",
3130
"remark-cli": "^4.0.0",
31+
"remark-parse": "^4.0.0",
3232
"remark-preset-wooorm": "^3.0.0",
3333
"tape": "^4.0.0",
34+
"unified": "^6.1.6",
3435
"xo": "^0.18.0"
3536
},
3637
"scripts": {

test.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
'use strict';
22

33
var test = require('tape');
4-
var remark = require('remark');
5-
var gemoji = require('./index.js');
4+
var unified = require('unified');
5+
var parse = require('remark-parse');
6+
var gemoji = require('./');
67

78
test('remark-gemoji', function (t) {
9+
t.throws(
10+
function () {
11+
unified().use(gemoji).freeze();
12+
},
13+
/^Error: Missing parser to attach `remark-gemoji` to/,
14+
'should throw if without parser'
15+
);
16+
817
t.deepEqual(
9-
remark()
10-
.data('settings', {pedantic: true, position: false})
18+
unified()
19+
.use(parse, {pedantic: true, position: false})
1120
.use(gemoji)
1221
.parse(':a_b_c:'),
1322
{
@@ -26,12 +35,13 @@ test('remark-gemoji', function (t) {
2635
{type: 'text', value: 'c:'}
2736
]
2837
}]
29-
}
38+
},
39+
'should ignore unknown gemoji'
3040
);
3141

3242
t.deepEqual(
33-
remark()
34-
.data('settings', {pedantic: true, position: false})
43+
unified()
44+
.use(parse, {pedantic: true, position: false})
3545
.use(gemoji)
3646
.parse(':heavy_check_mark:'),
3747
{
@@ -45,7 +55,8 @@ test('remark-gemoji', function (t) {
4555
}
4656
]
4757
}]
48-
}
58+
},
59+
'should support gemoji'
4960
);
5061

5162
t.end();

0 commit comments

Comments
 (0)