Skip to content

Commit

Permalink
Convert source to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Oct 7, 2021
1 parent 223e61d commit 82ad1c6
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 557 deletions.
463 changes: 185 additions & 278 deletions lib/marked.esm.js

Large diffs are not rendered by default.

279 changes: 100 additions & 179 deletions lib/marked.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion marked.min.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/Lexer.js
@@ -1,7 +1,7 @@
const Tokenizer = require('./Tokenizer.js');
const { defaults } = require('./defaults.js');
const { block, inline } = require('./rules.js');
const { repeatString } = require('./helpers.js');
import Tokenizer from './Tokenizer.js';
import { defaults } from './defaults.js';
import { block, inline } from './rules.js';
import { repeatString } from './helpers.js';

/**
* smartypants text replacement
Expand Down Expand Up @@ -47,7 +47,7 @@ function mangle(text) {
/**
* Block Lexer
*/
module.exports = class Lexer {
export default class Lexer {
constructor(options) {
this.tokens = [];
this.tokens.links = Object.create(null);
Expand Down Expand Up @@ -488,4 +488,4 @@ module.exports = class Lexer {

return tokens;
}
};
}
16 changes: 8 additions & 8 deletions src/Parser.js
@@ -1,15 +1,15 @@
const Renderer = require('./Renderer.js');
const TextRenderer = require('./TextRenderer.js');
const Slugger = require('./Slugger.js');
const { defaults } = require('./defaults.js');
const {
import Renderer from './Renderer.js';
import TextRenderer from './TextRenderer.js';
import Slugger from './Slugger.js';
import { defaults } from './defaults.js';
import {
unescape
} = require('./helpers.js');
} from './helpers.js';

/**
* Parsing & Compiling
*/
module.exports = class Parser {
export default class Parser {
constructor(options) {
this.options = options || defaults;
this.options.renderer = this.options.renderer || new Renderer();
Expand Down Expand Up @@ -283,4 +283,4 @@ module.exports = class Parser {
}
return out;
}
};
}
10 changes: 5 additions & 5 deletions src/Renderer.js
@@ -1,13 +1,13 @@
const { defaults } = require('./defaults.js');
const {
import { defaults } from './defaults.js';
import {
cleanUrl,
escape
} = require('./helpers.js');
} from './helpers.js';

/**
* Renderer
*/
module.exports = class Renderer {
export default class Renderer {
constructor(options) {
this.options = options || defaults;
}
Expand Down Expand Up @@ -163,4 +163,4 @@ module.exports = class Renderer {
text(text) {
return text;
}
};
}
4 changes: 2 additions & 2 deletions src/Slugger.js
@@ -1,7 +1,7 @@
/**
* Slugger generates header id
*/
module.exports = class Slugger {
export default class Slugger {
constructor() {
this.seen = {};
}
Expand Down Expand Up @@ -46,4 +46,4 @@ module.exports = class Slugger {
const slug = this.serialize(value);
return this.getNextSafeSlug(slug, options.dryrun);
}
};
}
4 changes: 2 additions & 2 deletions src/TextRenderer.js
Expand Up @@ -2,7 +2,7 @@
* TextRenderer
* returns only the textual part of the token
*/
module.exports = class TextRenderer {
export default class TextRenderer {
// no need for block level renderers
strong(text) {
return text;
Expand Down Expand Up @@ -39,4 +39,4 @@ module.exports = class TextRenderer {
br() {
return '';
}
};
}
10 changes: 5 additions & 5 deletions src/Tokenizer.js
@@ -1,10 +1,10 @@
const { defaults } = require('./defaults.js');
const {
import { defaults } from './defaults.js';
import {
rtrim,
splitCells,
escape,
findClosingBracket
} = require('./helpers.js');
} from './helpers.js';

function outputLink(cap, link, raw, lexer) {
const href = link.href;
Expand Down Expand Up @@ -65,7 +65,7 @@ function indentCodeCompensation(raw, text) {
/**
* Tokenizer
*/
module.exports = class Tokenizer {
export default class Tokenizer {
constructor(options) {
this.options = options || defaults;
}
Expand Down Expand Up @@ -752,4 +752,4 @@ module.exports = class Tokenizer {
};
}
}
};
}
10 changes: 3 additions & 7 deletions src/defaults.js
@@ -1,4 +1,4 @@
function getDefaults() {
export function getDefaults() {
return {
baseUrl: null,
breaks: false,
Expand All @@ -22,12 +22,8 @@ function getDefaults() {
};
}

function changeDefaults(newDefaults) {
export function changeDefaults(newDefaults) {
module.exports.defaults = newDefaults;
}

module.exports = {
defaults: getDefaults(),
getDefaults,
changeDefaults
};
export const defaults = getDefaults();
37 changes: 20 additions & 17 deletions src/esm-entry.js
@@ -1,18 +1,21 @@
const marked = require('./marked.js');
const Lexer = require('./Lexer.js');
const Parser = require('./Parser.js');
const Tokenizer = require('./Tokenizer.js');
const Renderer = require('./Renderer.js');
const TextRenderer = require('./TextRenderer.js');
const Slugger = require('./Slugger.js');
import marked from './marked.js';
import Lexer from './Lexer.js';
import Parser from './Parser.js';

module.exports = marked;
module.exports.parse = marked;
module.exports.Parser = Parser;
module.exports.parser = Parser.parse;
module.exports.Renderer = Renderer;
module.exports.TextRenderer = TextRenderer;
module.exports.Lexer = Lexer;
module.exports.lexer = Lexer.lex;
module.exports.Tokenizer = Tokenizer;
module.exports.Slugger = Slugger;
export default marked;
export const options = marked.options;
export const setOptions = marked.setOptions;
export const getDefaults = marked.getDefaults;
export const defaults = marked.defaults;
export const use = marked.use;
export const walkTokens = marked.walkTokens;
export const parseInline = marked.parseInline;
export const parse = marked;
export const parser = Parser.parse;
export const lexer = Lexer.lex;
export { default as Lexer } from './Lexer.js';
export { default as Parser } from './Parser.js';
export { default as Tokenizer } from './Tokenizer.js';
export { default as Renderer } from './Renderer.js';
export { default as TextRenderer } from './TextRenderer.js';
export { default as Slugger } from './Slugger.js';
39 changes: 12 additions & 27 deletions src/helpers.js
Expand Up @@ -13,7 +13,7 @@ const escapeReplacements = {
"'": '''
};
const getEscapeReplacement = (ch) => escapeReplacements[ch];
function escape(html, encode) {
export function escape(html, encode) {
if (encode) {
if (escapeTest.test(html)) {
return html.replace(escapeReplace, getEscapeReplacement);
Expand All @@ -29,7 +29,7 @@ function escape(html, encode) {

const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;

function unescape(html) {
export function unescape(html) {
// explicitly match decimal, hex, and named HTML entities
return html.replace(unescapeTest, (_, n) => {
n = n.toLowerCase();
Expand All @@ -44,7 +44,7 @@ function unescape(html) {
}

const caret = /(^|[^\[])\^/g;
function edit(regex, opt) {
export function edit(regex, opt) {
regex = regex.source || regex;
opt = opt || '';
const obj = {
Expand All @@ -63,7 +63,7 @@ function edit(regex, opt) {

const nonWordAndColonTest = /[^\w:]/g;
const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
function cleanUrl(sanitize, base, href) {
export function cleanUrl(sanitize, base, href) {
if (sanitize) {
let prot;
try {
Expand Down Expand Up @@ -93,7 +93,7 @@ const justDomain = /^[^:]+:\/*[^/]*$/;
const protocol = /^([^:]+:)[\s\S]*$/;
const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;

function resolveUrl(base, href) {
export function resolveUrl(base, href) {
if (!baseUrls[' ' + base]) {
// we can ignore everything in base after the last slash of its path component,
// but we might need to add _that_
Expand Down Expand Up @@ -122,9 +122,9 @@ function resolveUrl(base, href) {
}
}

const noopTest = { exec: function noopTest() {} };
export const noopTest = { exec: function noopTest() {} };

function merge(obj) {
export function merge(obj) {
let i = 1,
target,
key;
Expand All @@ -141,7 +141,7 @@ function merge(obj) {
return obj;
}

function splitCells(tableRow, count) {
export function splitCells(tableRow, count) {
// ensure that every cell-delimiting pipe has a space
// before it to distinguish it from an escaped pipe
const row = tableRow.replace(/\|/g, (match, offset, str) => {
Expand Down Expand Up @@ -180,7 +180,7 @@ function splitCells(tableRow, count) {
// Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
// /c*$/ is vulnerable to REDOS.
// invert: Remove suffix of non-c chars instead. Default falsey.
function rtrim(str, c, invert) {
export function rtrim(str, c, invert) {
const l = str.length;
if (l === 0) {
return '';
Expand All @@ -204,7 +204,7 @@ function rtrim(str, c, invert) {
return str.substr(0, l - suffLen);
}

function findClosingBracket(str, b) {
export function findClosingBracket(str, b) {
if (str.indexOf(b[1]) === -1) {
return -1;
}
Expand All @@ -226,14 +226,14 @@ function findClosingBracket(str, b) {
return -1;
}

function checkSanitizeDeprecation(opt) {
export function checkSanitizeDeprecation(opt) {
if (opt && opt.sanitize && !opt.silent) {
console.warn('marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options');
}
}

// copied from https://stackoverflow.com/a/5450113/806777
function repeatString(pattern, count) {
export function repeatString(pattern, count) {
if (count < 1) {
return '';
}
Expand All @@ -247,18 +247,3 @@ function repeatString(pattern, count) {
}
return result + pattern;
}

module.exports = {
escape,
unescape,
edit,
cleanUrl,
resolveUrl,
noopTest,
merge,
splitCells,
rtrim,
findClosingBracket,
checkSanitizeDeprecation,
repeatString
};
22 changes: 11 additions & 11 deletions src/marked.js
@@ -1,19 +1,19 @@
const Lexer = require('./Lexer.js');
const Parser = require('./Parser.js');
const Tokenizer = require('./Tokenizer.js');
const Renderer = require('./Renderer.js');
const TextRenderer = require('./TextRenderer.js');
const Slugger = require('./Slugger.js');
const {
import Lexer from './Lexer.js';
import Parser from './Parser.js';
import Tokenizer from './Tokenizer.js';
import Renderer from './Renderer.js';
import TextRenderer from './TextRenderer.js';
import Slugger from './Slugger.js';
import {
merge,
checkSanitizeDeprecation,
escape
} = require('./helpers.js');
const {
} from './helpers.js';
import {
getDefaults,
changeDefaults,
defaults
} = require('./defaults.js');
} from './defaults.js';

/**
* Marked
Expand Down Expand Up @@ -334,4 +334,4 @@ marked.Tokenizer = Tokenizer;
marked.Slugger = Slugger;
marked.parse = marked;

module.exports = marked;
export default marked;
13 changes: 4 additions & 9 deletions src/rules.js
@@ -1,13 +1,13 @@
const {
import {
noopTest,
edit,
merge
} = require('./helpers.js');
} from './helpers.js';

/**
* Block-Level Grammar
*/
const block = {
export const block = {
newline: /^(?: *(?:\n|$))+/,
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
Expand Down Expand Up @@ -139,7 +139,7 @@ block.pedantic = merge({}, block.normal, {
/**
* Inline-Level Grammar
*/
const inline = {
export const inline = {
escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
url: noopTest,
Expand Down Expand Up @@ -283,8 +283,3 @@ inline.breaks = merge({}, inline.gfm, {
.replace(/\{2,\}/g, '*')
.getRegex()
});

module.exports = {
block,
inline
};

0 comments on commit 82ad1c6

Please sign in to comment.