Skip to content

Commit

Permalink
add new option namedCodesToUnicode (#236) (#253)
Browse files Browse the repository at this point in the history
* add new option namedCodesToUnicode (#236)

* removed empty line from compiler constructor
  • Loading branch information
tangleMesh authored and quantizor committed May 29, 2019
1 parent dab7b5d commit f0d5d26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -358,6 +359,37 @@ compiler('# 中文', { slugify: str => str });
<h1 id="中文">中文</h1>
```

#### options.namedCodesToUnicode

By default only a couple of named html codes are converted to unicode characters:

* `&` (`&amp;`)
* `'` (`&apos;`)
* `>` (`&gt;`)
* `<` (`&lt;`)
* ` ` (`&nbsp;`)
* `"` (`&quot;`)

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
<Markdown options={{ namedCodesToUnicode: {
le: '\u2264',
ge: '\u2265',
} }}>This text is &le; than this text.</Markdown>;

// or

compiler('This text is &le; than this text.', namedCodesToUnicode: {
le: '\u2264',
ge: '\u2265',
});

// renders:

<p>This text is ≤ than this text.</p>
```

### 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.
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}),
};
Expand Down

0 comments on commit f0d5d26

Please sign in to comment.