Browse Source

Make dashes a switchable typographer category

pull/762/head
Kyle E. Mitchell 4 years ago
parent
commit
3f0af6ba35
  1. 2
      lib/index.js
  2. 33
      lib/rules_core/replacements.js
  3. 18
      test/misc.js

2
lib/index.js

@ -154,7 +154,7 @@ function normalizeLinkText(url) {
* replacement](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js) +
* quotes beautification (smartquotes). Or use an Object to enable
* specific replacements, like `{rare: true, intellectualProperty:
* true, quotes: true}`.
* true, quotes: true, dashes: true}`.
* - __quotes__ - `“”‘’`, String or Array. Double + single quotes replacement
* pairs, when typographer enabled and smartquotes on. For example, you can
* use `'«»„“'` for Russian, `'„“‚‘'` for German, and

33
lib/rules_core/replacements.js

@ -15,7 +15,9 @@
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
// - miltiplication 2 x 4 -> 2 × 4
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/;
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,/;
var DASHES_RE = /--/;
// Workaround for phantomjs - need regex without /g flag,
// or root check will fail every second time
@ -66,7 +68,29 @@ function replace_rare(inlineTokens) {
// .., ..., ....... -> …
// but ?..... & !..... -> ?.. & !..
.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..')
.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',')
.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',');
}
}
if (token.type === 'link_open' && token.info === 'auto') {
inside_autolink--;
}
if (token.type === 'link_close' && token.info === 'auto') {
inside_autolink++;
}
}
}
function replace_dashes(inlineTokens) {
var i, token, inside_autolink = 0;
for (i = inlineTokens.length - 1; i >= 0; i--) {
token = inlineTokens[i];
if (token.type === 'text' && !inside_autolink) {
if (DASHES_RE.test(token.content)) {
token.content = token.content
// em-dash
.replace(/(^|[^-])---(?=[^-]|$)/mg, '$1\u2014')
// en-dash
@ -94,6 +118,7 @@ module.exports = function replace(state) {
var optionIsObject = typeof typographerOption === 'object';
var replaceRare = !optionIsObject || typographerOption.rare;
var replaceIP = !optionIsObject || typographerOption.intellectualProperty;
var replaceDashes = !optionIsObject || typographerOption.dashes;
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
@ -103,6 +128,10 @@ module.exports = function replace(state) {
replace_scoped(state.tokens[blkIdx].children);
}
if (replaceDashes && DASHES_RE.test(state.tokens[blkIdx].content)) {
replace_dashes(state.tokens[blkIdx].children);
}
if (replaceRare && RARE_RE.test(state.tokens[blkIdx].content)) {
replace_rare(state.tokens[blkIdx].children);
}

18
test/misc.js

@ -364,6 +364,7 @@ describe('typographer switches', function () {
it('Should support disabling IP symbols', function () {
var md = markdownit({
typographer: {
dashes: true,
quotes: true,
rare: true,
intellectualProperty: false
@ -378,6 +379,7 @@ describe('typographer switches', function () {
it('Should support disabling quote replacements', function () {
var md = markdownit({
typographer: {
dashes: true,
quotes: false,
rare: true,
intellectualProperty: true
@ -392,6 +394,7 @@ describe('typographer switches', function () {
it('Should support disabling rare replacements', function () {
var md = markdownit({
typographer: {
dashes: true,
quotes: true,
rare: false,
intellectualProperty: true
@ -403,6 +406,21 @@ describe('typographer switches', function () {
);
});
it('Should support disabling dash replacements', function () {
var md = markdownit({
typographer: {
dashes: false,
quotes: true,
rare: true,
intellectualProperty: true
}
});
assert.strictEqual(
md.render('(R) --- it\'s like (c) for trademarks!!!!!!!'),
'<p>® --- it’s like © for trademarks!!!</p>\n'
);
});
});

Loading…
Cancel
Save