From f0d5d260daa87770a5e31ddc3726b251e285a0ec Mon Sep 17 00:00:00 2001 From: JeremiasEh <38432973+JeremiasEh@users.noreply.github.com> Date: Wed, 29 May 2019 04:53:42 +0200 Subject: [PATCH] add new option namedCodesToUnicode (#236) (#253) * add new option namedCodesToUnicode (#236) * removed empty line from compiler constructor --- README.md | 32 ++++++++++++++++++++++++++++++++ index.js | 7 +++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fdf94a00..ec35261b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ The most lightweight, customizable React markdown component. - [options.overrides - Rendering Arbitrary React Components](#optionsoverrides---rendering-arbitrary-react-components) - [options.createElement - Custom React.createElement behavior](#optionscreateelement---custom-reactcreateelement-behavior) - [options.slugify](#optionsslugify) + - [options.namedCodesToUnicode](#optionsnamedcodestounicode) - [Getting the smallest possible bundle size](#getting-the-smallest-possible-bundle-size) - [Usage with Preact](#usage-with-preact) - [Gotchas](#gotchas) @@ -358,6 +359,37 @@ compiler('# 中文', { slugify: str => str });

中文

``` +#### options.namedCodesToUnicode + +By default only a couple of named html codes are converted to unicode characters: + +* `&` (`&`) +* `'` (`'`) +* `>` (`>`) +* `<` (`<`) +* ` ` (` `) +* `"` (`"`) + +Some projects require to extend this map of named codes and unicode characters. To customize this list with additional html codes pass the option namedCodesToUnicode as object with the code names needed as in the example below: + +```jsx +This text is ≤ than this text.; + +// or + +compiler('This text is ≤ than this text.', namedCodesToUnicode: { + le: '\u2264', + ge: '\u2265', +}); + +// renders: + +

This text is ≤ than this text.

+``` + ### Getting the smallest possible bundle size Many development conveniences are placed behind `process.env.NODE_ENV !== "production"` conditionals. When bundling your app, it's a good idea to replace these code snippets such that a minifier (like uglify) can sweep them away and leave a smaller overall bundle. diff --git a/index.js b/index.js index 4f0f92b5..a6f75e68 100644 --- a/index.js +++ b/index.js @@ -716,6 +716,9 @@ export function compiler(markdown, options) { options = options || {}; options.overrides = options.overrides || {}; options.slugify = options.slugify || slugify; + options.namedCodesToUnicode = options.namedCodesToUnicode + ? {...namedCodesToUnicode, ...options.namedCodesToUnicode} + : namedCodesToUnicode; const createElementFn = options.createElement || React.createElement; @@ -1420,8 +1423,8 @@ export function compiler(markdown, options) { content: capture[0] // nbsp -> unicode equivalent for named chars .replace(HTML_CHAR_CODE_R, (full, inner) => { - return namedCodesToUnicode[inner] - ? namedCodesToUnicode[inner] + return options.namedCodesToUnicode[inner] + ? options.namedCodesToUnicode[inner] : full; }), };