-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
wrapEmoji
fixes and improvements (#43)
- Loading branch information
Showing
8 changed files
with
72 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// ts-check | ||
|
||
import { html, wrapEmoji } from './src/html.js' | ||
import { html } from './src/html.js' | ||
import { strip } from './src/strip.js' | ||
import { wrapEmoji } from './src/wrapEmoji.js' | ||
|
||
export { html, strip, wrapEmoji } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// @ts-check | ||
|
||
'use strict' | ||
|
||
import emojiRegex from 'emoji-regex' | ||
|
||
/** | ||
* RegExp to wrap all emoji in <span> tags. | ||
*/ | ||
const EMOJI_RX = emojiRegexPattern() | ||
|
||
/** | ||
* Template to use when replacing emoji. | ||
* (wmE = WebMUDs Emoji) | ||
*/ | ||
const EMOJI_REPLACEMENT_TEMPLATE = '<span class="wmE">$1</span>' | ||
|
||
/** | ||
* Wraps all emoji in a custom <span> tag. | ||
* | ||
* @param {string} text | ||
* @return {string} | ||
*/ | ||
export function wrapEmoji (text) { | ||
return text.replace(EMOJI_RX, EMOJI_REPLACEMENT_TEMPLATE) | ||
} | ||
|
||
/** | ||
* Helper that extracts the RegExp from emoji-regex | ||
* and returns a new RegExp with capture enabled. | ||
*/ | ||
function emojiRegexPattern () { | ||
const patternFromLib = emojiRegex().toString() | ||
const lastSlashIndex = patternFromLib.lastIndexOf('/') | ||
const pattern = patternFromLib.slice(1, lastSlashIndex) | ||
const flags = patternFromLib.slice(lastSlashIndex + 1) | ||
|
||
return new RegExp(`((?:${pattern})+)`, flags) | ||
} |
This file contains 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
This file contains 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