Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Koenig - Expand -- to en dash and --- to em dash
Browse files Browse the repository at this point in the history
refs TryGhost/Ghost#9623
- match `--` and `---` and convert if followed by a space or any character respectively
- include guards to ensure that expansion doesn't occur inside code blocks
  • Loading branch information
kevinansfield committed May 14, 2018
1 parent f791f52 commit 12a9e45
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/koenig-editor/addon/options/text-expansions.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,55 @@ export default function (editor, koenig) {

/* inline markdown ------------------------------------------------------ */

// --\s = en dash –
// ---. = em dash —
// separate to the grouped replacement functions because we're matching on
// the trailing character which can be anything
editor.onTextInput({
name: 'hyphens',
match: /---?.$/,
run(editor) {
let {range} = editor;

let text = editor.range.head.section.textUntil(editor.range.head);

// do not match if we're in code formatting
if (editor.hasActiveMarkup('code') || text.match(/[^\s]?`[^\s]/)) {
return;
}

let ndashMatch = text.match(/[^-]--(\s)$/);
if (ndashMatch) {
let match = ndashMatch[0];
range = range.extend(-(match.length - 1));

if (editor.detectMarkupInRange(range, 'code')) {
return;
}

return editor.run((postEditor) => {
let position = postEditor.deleteRange(range);
postEditor.insertText(position, `–${ndashMatch[1]}`);
});
}

let mdashMatch = text.match(/---([^-])$/);
if (mdashMatch) {
let match = mdashMatch[0];
range = range.extend(-(match.length));

if (editor.detectMarkupInRange(range, 'code')) {
return;
}

return editor.run((postEditor) => {
let position = postEditor.deleteRange(range);
postEditor.insertText(position, `—${mdashMatch[1]}`);
});
}
}
});

function matchStrongStar(editor, text) {
let {range} = editor;
let matches = text.match(/(?:^|\s)\*\*([^\s*]+|[^\s*][^*]*[^\s])\*\*/);
Expand Down

0 comments on commit 12a9e45

Please sign in to comment.