npm.io
3.7.0 • Published yesterday

yoastseo

Licence
GPL-3.0
Version
3.7.0
Deps
9
Size
13.8 MB
Vulns
0
Weekly
0
Stars
2.0K

YoastSEO.js

YoastSEO.js is a text analysis and assessment library in JavaScript. This library is used in the Yoast SEO plugin for WordPress to analyze and assess the content of a post or page. This library can generate metrics about a text and assess these metrics to give you an assessment which can be used to improve the text.

Screenshot of the assessment of the given text

Documentation

Installation

You can install YoastSEO.js using npm:

npm install yoastseo

Or using yarn:

yarn add yoastseo

Usage

You can either use YoastSEO.js using the web worker API or use the internal components directly.

Entry points

The package exposes three supported public entry points:

Import Provides Use it for
yoastseo The core analysis library: Paper, the assessors, the web-worker API (AnalysisWebWorker, AnalysisWorkerWrapper), AbstractResearcher, helpers, and related building blocks. Orchestrating analysis.
yoastseo/researcher A getResearcher( language ) factory that returns the language-specific Researcher class (falling back to the default, language-agnostic Researcher for unsupported languages). Resolving a per-language Researcher (Node, custom bundlers, web workers).
yoastseo/contract The serializable input/output contract: toPaper/paperDtoSchema and toResultDto/resultDtoSchema, for validating and mapping analysis input and output. Exchanging documented, serializable shapes with the engine from non-WordPress consumers. See CONTRACT.md.

Why separate entry points? The split is intentional. Each language Researcher transitively pulls in that language's data — function words, stemmers, transition words, and so on. Re-exporting the factory from the package root would bundle every language (~2.4 MB) into whatever bundle imports the root. That especially hurts consumers that load yoastseo as a bundler external — where the package root is provided once as a shared global (or shared chunk) rather than bundled into each consumer. (Yoast SEO for WordPress does this, exposing the root as the window.yoast.analysis global, but any webpack/Rollup setup can configure yoastseo as an external the same way.) Keeping getResearcher on its own entry keeps that shared root lean and lets consumers load only the languages they need. The yoastseo/contract entry follows the same principle: it isolates the contract's runtime dependency (zod) so only consumers that import the contract pay for it, keeping the shared root free of it.

Deep imports such as yoastseo/build/... and yoastseo/src/... reach internal modules. They work, but they are implementation details — not part of the supported surface — so prefer the entry points above.

Resolving a language Researcher (the language codes are listed under Supported languages; see the getResearcher factory for the full set):

// CommonJS
const getResearcher = require( "yoastseo/researcher" );
// ESM
import getResearcher from "yoastseo/researcher";

const DutchResearcher = getResearcher( "nl" ); // Returns the class, not an instance.
const researcher = new DutchResearcher();

Bundle-size note. getResearcher statically references every supported language, so a bundler that follows this entry includes all of them (~2.4 MB of language data) — the map cannot be tree-shaken down to one language. Size-sensitive consumers that only ever need a single language can instead deep-import just that one (yoastseo/build/languageProcessing/languages/<lang>/Researcher), accepting that deep paths are internal and unsupported.

Web Worker API

Because a web worker must be a separate script in the browser, you first need to create a script to use inside the web worker:

import { AnalysisWebWorker } from "yoastseo";
import getResearcher from "yoastseo/researcher";

const EnglishResearcher = getResearcher( "en" );
const worker = new AnalysisWebWorker( self, new EnglishResearcher() );
// Any custom registration should be done here (or send messages via postMessage to the wrapper).
worker.register();

Then, in a different script, you have the following code:

import { AnalysisWorkerWrapper, Paper } from "yoastseo";

// `url` needs to be the full URL to the script for the browser to know where to load the worker script from.
// This should be the script created by the previous code-snippet.
const url = "https://my-site-url.com/path-to-webworker-script.js"

const worker = new AnalysisWorkerWrapper( new Worker( url ) );

worker.initialize( {
    logLevel: "TRACE", // Optional, see https://github.com/pimterry/loglevel#documentation
} ).then( () => {
    // The worker has been configured, we can now analyze a Paper.
    const paper = new Paper( "Text to analyze", {
        keyword: "analyze",
    } );

    return worker.analyze( paper );
} ).then( ( results ) => {
    console.log( 'Analysis results:' );
    console.log( results );
} ).catch( ( error ) => {
    console.error( 'An error occurred while analyzing the text:' );
    console.error( error );
} );

There is a basic example over here, which also contains a basic setup with Webpack. There is also a more involved example over here, which has a basic React implementation.

Usage of internal components

If you want to have a more bare-bones API or are in an environment without access to Web Worker, you can use the internal objects:

import { AbstractResearcher, Paper } from "yoastseo";

const paper = new Paper( "Text to analyze", {
    keyword: "analyze",
} );
const researcher = new AbstractResearcher( paper );

console.log( researcher.getResearch( "wordCountInText" ) );

There is a basic example of this setup over here.

Running a single assessment

If you only need the result of one assessment, use runAssessment instead of constructing an assessor. It runs the single-assessment slice of Assessor.assess(): it wires the researcher to the paper, builds the HTML tree when one is needed (so tree-dependent assessments such as text length or keyphrase density work, not just data-only ones), gates on applicability, stamps the result identifier, and isolates errors to a -1 result.

import { Paper, runAssessment, assessments } from "yoastseo";
import getResearcher from "yoastseo/researcher";

// 1. Construct the assessment with its (optional) config.
const textLength = new assessments.seo.TextLengthAssessment();

// 2. Build the Paper to analyze. (Pass `productData` here for the e-commerce assessments.)
const paper = new Paper( "Text to analyze", {
    keyword: "analyze",
} );

// 3. Resolve a language Researcher and instantiate it. One is required even for
//    assessments that don't read it, because building the tree needs its language data.
const EnglishResearcher = getResearcher( "en" );
const researcher = new EnglishResearcher();

// 4. Run the assessment.
const result = runAssessment( textLength, paper, researcher );

if ( result === null ) {
    // The assessment was not applicable to this paper and was skipped.
} else {
    console.log( result.getScore(), result.getText() );
}

For a data-only assessment that does not read the tree, pass { buildTree: false } as the fourth argument to skip the tree-build cost.

Supported languages

SEO analysis

Function word support, which is used for internal linking, insights, and keyphrase-related analysis, is available in the following languages:

English, German, Dutch, French, Spanish, Italian, Portuguese, Russian, Polish, Swedish, Hungarian, Indonesian, Arabic, Hebrew, Farsi, Turkish, Norwegian, Czech, Slovak, Greek, Japanese

Readability analysis

| Language | Transition words | Flesch reading ease | Passive voice | Sentence beginnings | Sentence length1 | |------------ |------------------ |--------------------- |--------------- |--------------------- |----------------------------- | | English | | | | | | | German | | | | | | | Dutch | | | | | | | French | | | | | | | Spanish | | | | | | | Italian | | | | | | | Portuguese | | | | | | | Russian | | | | | | | Catalan | | 3 | 3 | 3 | | | Polish | | 2 | | | | | Swedish | | 2 | | | | | Hungarian | | 2 | | | | | Indonesian | | 2 | | | | | Arabic | | 2 | | | | | Hebrew | | 2 | | | | | Farsi | | 2 | | | | | Turkish | | 2 | | | | | Norwegian | | 2 | | | | | Czech | | 2 | | | | | Slovak | | 2 | | | | | Greek | | 2 | | | | | Japanese | | 2 | 4 | | |

1 This means the default upper limit of 20 words has been verified for this language, or the upper limit has been changed.

2 There is no existing Flesch reading ease formula for these languages.

3 This means that the functionality for this assessment is currently not available for these languages.

4 The Passive voice check for Japanese is not implemented since the structure is the same as the potential form and can additionally be used for an honorific purpose. Identifying whether a verb is in its passive, honorific or potential form is problematic without contextual information.

The following readability assessments are available for all languages:

  • sentence length (with a default upper limit of 20 words, see1 above)
  • paragraph length
  • subheading distribution
  • text presence
Inclusive language analysis

The inclusive language analysis is currently available in English.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

npm test

Or using yarn:

yarn test

Generate coverage using the --coverage flag.

Code style

To test your code style:

yarn lint

Testing with Yoast SEO

In the YoastSEO.js directory, run:

npm link

Or using yarn:

yarn link

Then, in the project directory where you want to test the package, assuming you have a complete development version, run:

npm link yoastseo

Or using yarn:

yarn link yoastseo

If you want to unlink, simply do:

npm unlink yoastseo

Or using yarn:

yarn unlink yoastseo

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email security [at] yoast.com instead of using the issue tracker.

Credits

License

We follow the GPL. Please see the License file for more information.

Keywords