Skip to content

Commit 15436b2

Browse files
committed
fix: add special case if single quote follows a double quote directly
1 parent e90ed16 commit 15436b2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/smartQuotes.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const isSeparatorOrWhitespace = (char) => /\s|[>\-–—]/.test(char)
1414
const shouldBeOpeningQuote = (text, indexCharBefore) => indexCharBefore < 0 || isSeparatorOrWhitespace(text[indexCharBefore])
1515
const shouldBeClosingQuote = (text, indexCharBefore) => !!text[indexCharBefore] && !isSeparatorOrWhitespace(text[indexCharBefore])
1616
const hasCharAfter = (textArr, indexCharAfter) => !!textArr[indexCharAfter] && !isWhitespace(textArr[indexCharAfter])
17+
const shouldBeSingleOpeningQuote = (text, indexCharBefore) => !!text[indexCharBefore] && isDoubleQuote(text[indexCharBefore])
1718

1819
export const replaceQuote = (range, index, quoteType) => {
1920
const startContainer = range?.startContainer
@@ -56,7 +57,11 @@ export const applySmartQuotes = (range, config, char, target, cursorOffset) => {
5657
const textArr = [...range.startContainer.textContent]
5758
let newTextNode
5859

59-
if (shouldBeClosingQuote(textArr, offset - 2)) {
60+
// Special case for a single quote following a double quote,
61+
// which should be transformed into a single opening quote
62+
if (isCharSingleQuote && shouldBeSingleOpeningQuote(textArr, offset - 2)) {
63+
newTextNode = replaceQuote(range, offset - 1, singleQuotes[0])
64+
} else if (shouldBeClosingQuote(textArr, offset - 2)) {
6065
if (isCharSingleQuote) {
6166
// Don't transform apostrophes
6267
if (hasCharAfter(textArr, offset)) {

0 commit comments

Comments
 (0)