NLCST Emoji Modifier
The `nlcst-emoji-modifier` package is an NLCST utility designed to classify both standard Unicode emoji (e.g., 👍) and GitHub-style gemoji shortcodes (e.g., `:cat:`) within natural language text by transforming them into `EmoticonNode`s in the syntax tree. The current stable version is 6.0.2. This project, part of the `unified` collective, releases new major versions aligned with Node.js LTS support, dependency updates, and shifts to modern JavaScript module practices, ensuring ongoing compatibility and performance. It functions as a foundational component for advanced linguistic analysis, often used implicitly by higher-level plugins like `retext-emoji`. Its key differentiator lies in its specific integration within the NLCST ecosystem for detailed emoji and emoticon processing.
Common errors
-
TypeError: emojiModifier is not a function
cause Attempting to use CommonJS `require()` syntax to import `nlcst-emoji-modifier` which is an ESM-only package.fixConvert your file to an ES module (e.g., by adding `"type": "module"` to `package.json` or renaming to `.mjs`) and use `import { emojiModifier } from 'nlcst-emoji-modifier'`. -
SyntaxError: Cannot use import statement outside a module
cause Using ES module `import` syntax in a CommonJS context without proper configuration.fixEnsure your Node.js environment or build setup treats your files as ES modules. This might involve setting `"type": "module"` in your `package.json` or explicitly using `.mjs` file extensions. -
Error [ERR_REQUIRE_ESM]: require() of ES Module [path] not supported. Instead change the require of [path] to a dynamic import() or change 'type': 'module' in your package.json or use a .mjs extension.
cause Trying to `require()` this package (ESM-only) from a CommonJS module.fixYou must use `import()` or convert your consuming module to ESM. For example, `import { emojiModifier } from 'nlcst-emoji-modifier'`. -
Property 'Emoticon' does not exist on type 'typeof import("nlcst-emoji-modifier")'cause TypeScript error when trying to import the `Emoticon` type from `nlcst-emoji-modifier` after version 6.0.0.fixThe `Emoticon` type was moved. Import it from `nlcst-emoticon-modifier` instead: `import type { Emoticon } from 'nlcst-emoticon-modifier'`. -
UnhandledPromiseRejectionWarning: Error: [Error message about Node.js version]
cause Running `nlcst-emoji-modifier` v6.0.0+ on an unsupported Node.js version (<16).fixUpgrade your Node.js runtime to version 16 or newer. Verify your `package.json` `engines` field if distributing your package.
Warnings
- breaking Version 6.0.0 requires Node.js 16 or higher. Older Node.js versions are no longer supported.
- breaking The package transitioned to ESM-only starting from v5.0.0, and explicitly uses the `exports` field since v6.0.0. This significantly changes how it's imported.
- breaking The `Emoticon` TypeScript type was removed from `nlcst-emoji-modifier` in v6.0.0. It is now exported by `nlcst-emoticon-modifier`.
- breaking The `emojiModifier` function now explicitly yields `undefined` (returns nothing). If you were relying on a return value, your code will need adjustment.
- breaking Version 4.0.0 updated `unist-util-visit`, which could be a breaking change for TypeScript users who didn't explicitly expect TypeScript in their dependents.
- gotcha This utility is a low-level NLCST modifier. For a higher-level, easier-to-use abstraction that integrates this and other related utilities, consider `retext-emoji`.
Install
-
npm install nlcst-emoji-modifier -
yarn add nlcst-emoji-modifier -
pnpm add nlcst-emoji-modifier
Imports
- emojiModifier
const emojiModifier = require('nlcst-emoji-modifier')import { emojiModifier } from 'nlcst-emoji-modifier' - Emoticon
import type { Emoticon } from 'nlcst-emoji-modifier'import type { Emoticon } from 'nlcst-emoticon-modifier' - emojiModifier (Deno/Browser)
import {emojiModifier} from 'https://esm.sh/nlcst-emoji-modifier@6'
Quickstart
import {emojiModifier} from 'nlcst-emoji-modifier'
import {ParseEnglish} from 'parse-english'
import {inspect} from 'unist-util-inspect'
const english = new ParseEnglish()
english.tokenizeSentencePlugins.unshift(emojiModifier)
console.log(inspect(english.parse('It’s raining :cat:s and :dog:s.')))
// Expected output (simplified):
// RootNode
// └─ ParagraphNode
// └─ SentenceNode
// ├─ WordNode: "It’s"
// ├─ WhiteSpaceNode: " "
// ├─ WordNode: "raining"
// ├─ WhiteSpaceNode: " "
// ├─ EmoticonNode: ":cat:"
// ├─ WordNode: "s"
// ├─ WhiteSpaceNode: " "
// ├─ WordNode: "and"
// ├─ WhiteSpaceNode: " "
// ├─ EmoticonNode: ":dog:"
// └─ WordNode: "s."