Last active
May 5, 2024 21:16
-
-
Save baptiste-roullin/db5f57214dae9427bed4e3debbe02229 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Narrow non-breaking space before ? ! ; : | |
espaces fines insécables avant ? ! ; : | |
*/ | |
import markdownIt from 'markdown-it' | |
// Add the following in the main function of your Eleventy config file (.eleventy.js by default). | |
/** | |
* @param md {markdownIt} | |
* @returns {void} */ | |
french_nbsp (md) => { | |
// basic regex | |
const NBSP_DOUBLE_PUNCTUATION = /([^\s]+)(?:\s*)([?!;:])/gu | |
// narrow non-breaking space | |
const NBSP = '\u202F' | |
// Use this if you want regular, wide (dare I say too wide?) breaking spaces. | |
// const NBSP = '\u00A0' | |
md.core.ruler.push('double_punctuation', (state) => { | |
if (!state.md.options.typographer) { return } | |
for (let i = state.tokens.length - 1; i >= 0; i--) { | |
if (state.tokens[i].type !== 'inline') { continue } | |
for (let j = state.tokens[i].children.length - 1; j >= 0; j--) { | |
if (state.tokens[i].children[j].type !== 'text') { continue } | |
state.tokens[i].children[j].content = state.tokens[i].children[j].content.replace(NBSP_DOUBLE_PUNCTUATION, (match, $1, $2, $3, $4) => { | |
return $1 + NBSP + $2 | |
}) | |
} | |
} | |
}) | |
} | |
let options = { | |
html: true, | |
breaks: true, | |
linkify: true, | |
typographer: true, | |
quotes: ['«\u202F', '\u202F»', '‹\u202F', '\u202F›'] | |
} | |
const lib = markdownIt(options).use(french_nbsp) | |
config.setLibrary('md', lib) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment