Skip to content

Commit dff9165

Browse files
authored
Use named exports for public api (#354)
Default exports are always a headache for public api. They are hard to use when same module is used for both node and webpack via commonjs. We have this problem webpack/webpack#6584 Mixing named and default exports works even buggier. webpack/webpack#7973 The best solution is using only named exports. They fits perfectly with interops and may work without interop for commonjs. (module.exports = was a mistake, export default IMO too)
1 parent 1898b25 commit dff9165

File tree

11 files changed

+52
-36
lines changed

11 files changed

+52
-36
lines changed

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,24 @@ npm install remarkable --save
3333
## Usage
3434

3535
```js
36-
var Remarkable = require('remarkable');
36+
import { Remarkable } from 'remarkable';
3737
var md = new Remarkable();
3838

3939
console.log(md.render('# Remarkable rulezz!'));
4040
// => <h1>Remarkable rulezz!</h1>
4141
```
4242

43+
or with commonjs
44+
45+
```js
46+
const { Remarkable } = require('remarkable');
47+
var md = new Remarkable();
48+
49+
console.log(md.render('# Remarkable rulezz!'));
50+
// => <h1>Remarkable rulezz!</h1>
51+
```
52+
53+
4354
If installed globally with `npm`:
4455

4556
```sh
@@ -101,7 +112,8 @@ console.log(md.render('# Remarkable rulezz!'));
101112
Or define options via the `.set()` method:
102113

103114
```js
104-
var Remarkable = require('remarkable');
115+
import { Remarkable } from 'remarkable';
116+
105117
var md = new Remarkable();
106118

107119
md.set({
@@ -126,7 +138,7 @@ active syntax rules and options for common use cases.
126138
Enable strict [CommonMark](http://commonmark.org/) mode with the `commonmark` preset:
127139

128140
```js
129-
var Remarkable = require('remarkable');
141+
import { Remarkable } from 'remarkable';
130142
var md = new Remarkable('commonmark');
131143
```
132144

@@ -135,7 +147,7 @@ var md = new Remarkable('commonmark');
135147
Enable all available rules (but still with default options, if not set):
136148

137149
```js
138-
var Remarkable = require('remarkable');
150+
import { Remarkable } from 'remarkable';
139151
var md = new Remarkable('full');
140152

141153
// Or with options:
@@ -151,8 +163,8 @@ var md = new Remarkable('full', {
151163
Apply syntax highlighting to fenced code blocks with the `highlight` option:
152164

153165
```js
154-
var Remarkable = require('remarkable');
155-
var hljs = require('highlight.js') // https://highlightjs.org/
166+
import { Remarkable } from 'remarkable';
167+
import hljs from 'highlight.js' // https://highlightjs.org/
156168

157169
// Actual default values
158170
var md = new Remarkable({
@@ -233,7 +245,7 @@ Although full-weight typographical replacements are language specific, `remarkab
233245
provides coverage for the most common and universal use cases:
234246

235247
```js
236-
var Remarkable = require('remarkable');
248+
import { Remarkable } from 'remarkable';
237249
var md = new Remarkable({
238250
typographer: true,
239251
quotes: '“”‘’'
@@ -281,12 +293,20 @@ plugins.
281293
Autoconvert URL-like text to links
282294

283295
```js
284-
import Remarkable from 'remarkable';
285-
import linkify from 'remarkable/linkify';
296+
import { Remarkable } from 'remarkable';
297+
import { linkify } from 'remarkable/linkify';
286298

287299
var md = new Remarkable().use(linkify);
288300
```
289301

302+
### UMD
303+
304+
UMD bundle provides linkify out of the box
305+
306+
```js
307+
const { Remarkable, linkify, utils } = window.remarkable;
308+
```
309+
290310

291311
## References / Thanks
292312

lib/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import argparse from 'argparse';
3-
import Remarkable from './index';
4-
import linkify from './linkify';
3+
import { Remarkable } from './index';
4+
import { linkify } from './linkify';
55
import { version } from '../package.json';
66

77
////////////////////////////////////////////////////////////////////////////////

lib/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import defaultConfig from './configs/default';
88
import fullConfig from './configs/full';
99
import commonmarkConfig from './configs/commonmark';
1010

11+
/**
12+
* Expose `utils`, Useful helper functions for custom
13+
* rendering.
14+
*/
15+
16+
export {
17+
utils
18+
}
19+
1120
/**
1221
* Preset configs
1322
*/
@@ -47,7 +56,7 @@ function StateCore(instance, str, env) {
4756
* @param {Object} `options`
4857
*/
4958

50-
export default function Remarkable(preset, options) {
59+
export function Remarkable(preset, options) {
5160
if (typeof preset !== 'string') {
5261
options = preset;
5362
preset = 'default';
@@ -186,10 +195,3 @@ Remarkable.prototype.renderInline = function (str, env) {
186195
env = env || {};
187196
return this.renderer.render(this.parseInline(str, env), this.options, env);
188197
};
189-
190-
/**
191-
* Expose `utils`, Useful helper functions for custom
192-
* rendering.
193-
*/
194-
195-
Remarkable.utils = utils;

lib/linkify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ function parseTokens(state) {
155155
}
156156
};
157157

158-
export default function linkify(md) {
158+
export function linkify(md) {
159159
md.core.ruler.push('linkify', parseTokens);
160160
};

lib/umd.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
import Remarkable from './index';
2-
import linkify from './linkify';
3-
4-
Remarkable.linkify = linkify;
5-
6-
export default Remarkable;
1+
export { Remarkable, utils } from './index';
2+
export { linkify } from './linkify';

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import nodeResolve from 'rollup-plugin-node-resolve';
33
import json from 'rollup-plugin-json';
44
import { terser } from 'rollup-plugin-terser';
55

6-
const name = 'Remarkable';
6+
const name = 'remarkable';
77
const external = id => !id.startsWith('.') && !path.isAbsolute(id);
88

99
export default [

support/specsplit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import fs from 'fs';
88
import util from 'util';
99
import argparse from 'argparse';
10-
import Remarkable from '../lib/index.js';
10+
import { Remarkable } from '../lib/index.js';
1111
import pkg from '../package.json';
1212

1313
var cli = new argparse.ArgumentParser({

test/commonmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22
import { addTests } from './utils';
3-
import Remarkable from '../lib/index';
3+
import { Remarkable } from '../lib/index';
44

55
describe('CommonMark', function () {
66
var md = new Remarkable('commonmark');

test/linkify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path from 'path';
22
import assert from 'assert';
33
import { addTests } from './utils';
4-
import Remarkable from '../lib/index';
5-
import linkify from '../lib/linkify';
4+
import { Remarkable } from '../lib/index';
5+
import { linkify } from '../lib/linkify';
66

77
describe('linkify plugin', function () {
88
var md = new Remarkable({ html: true }).use(linkify);

test/misc.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import assert from 'assert';
2-
import Remarkable from '../lib/index';
3-
import linkify from '../lib/linkify';
4-
5-
const { utils } = Remarkable
2+
import { Remarkable, utils } from '../lib/index';
3+
import { linkify } from '../lib/linkify';
64

75

86
describe('Utils', function () {

test/remarkable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22
import { addTests } from './utils';
3-
import Remarkable from '../lib/index';
3+
import { Remarkable } from '../lib/index';
44

55
describe('remarkable', function () {
66
var md = new Remarkable('full', {

0 commit comments

Comments
 (0)