nlcst Literal Node Checker
nlcst-is-literal is a focused utility within the `unified` ecosystem, designed to determine if a `WordNode` within an NLCST (Natural Language Concrete Syntax Tree) parent node is semantically considered a "literal." This check typically involves analyzing surrounding punctuation, such as quotes, parentheses, or dashes, that enclose the word. The package is currently stable at version `3.0.0` and follows the `unified` collective's release cadence, typically aligning major versions with Node.js LTS updates and significant architectural shifts like the transition to ESM. It's primarily used by tools that process natural language, such as spell-checkers or linting tools, to correctly identify and potentially exclude words that are meant as examples or explicitly quoted text rather than standard vocabulary, rather than actual words to be evaluated.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ...nlcst-is-literal/index.js from ... is not supported.
cause Attempting to use `require()` on `nlcst-is-literal`, which is an ES Module.fixChange `const { isLiteral } = require('nlcst-is-literal')` to `import { isLiteral } from 'nlcst-is-literal'` and ensure your project supports ESM. -
TypeError: (0 , nlcst_is_literal__WEBPACK_IMPORTED_MODULE_0__.isLiteral) is not a function
cause Incorrect import syntax, typically when a bundler (like Webpack) processes an ESM module incorrectly, or when trying to access a named export as a default.fixEnsure you are using `import { isLiteral } from 'nlcst-is-literal'` and not attempting to import it as a default export or through a path that mangles the named export. -
ReferenceError: isLiteral is not defined
cause The `isLiteral` function was not successfully imported or is being used outside its scope.fixVerify your `import { isLiteral } from 'nlcst-is-literal'` statement is correct and at the top level of your module, and that the file is treated as an ES Module.
Warnings
- breaking Version 3.0.0 requires Node.js 16 or later. Older Node.js versions are no longer supported.
- breaking Version 3.0.0 transitioned to use the `exports` field in `package.json`. Direct imports of internal module paths (e.g., `nlcst-is-literal/index.js`) might break.
- breaking Version 2.0.0 made the package ESM-only. It cannot be imported using CommonJS `require()` statements.
- gotcha The `isLiteral` function determines literal status based on the presence and type of surrounding punctuation nodes in the NLCST tree. Users should be familiar with NLCST structures to correctly interpret its behavior.
Install
-
npm install nlcst-is-literal -
yarn add nlcst-is-literal -
pnpm add nlcst-is-literal
Imports
- isLiteral
const { isLiteral } = require('nlcst-is-literal')import { isLiteral } from 'nlcst-is-literal' - isLiteral
import {isLiteral} from 'https://esm.sh/nlcst-is-literal@3' - isLiteral
import type { Node } from 'nlcst'; /* ... */ isLiteral(parent: Node, index: number)
Quickstart
import { read } from 'to-vfile';
import { ParseEnglish } from 'parse-english';
import { visit } from 'unist-util-visit';
import { toString } from 'nlcst-to-string';
import { isLiteral } from 'nlcst-is-literal';
async function processDocument() {
const exampleText = `
The word “foo” is meant as a literal.
The word «bar» is meant as a literal.
The word (baz) is meant as a literal.
The word, qux, is meant as a literal.
The word — quux — is meant as a literal.
This sentence includes a regular word.
`;
// Create a mock vfile for the example text
const file = {
path: 'example.txt',
contents: exampleText,
toString() { return this.contents; }
};
const tree = new ParseEnglish().parse(String(file));
console.log('Literal words identified:');
visit(tree, 'WordNode', function (node, index, parent) {
if (isLiteral(parent, index)) {
console.log(`- ${toString(node)}`);
}
});
}
processDocument().catch(console.error);