UNPKG

sanity-plugin-seo-tools

Version:

> Upgrading from V2 to V3 is a breaking change. The `seo-tools` input has been replaced by a SEO pane which needs to be set-up using a custom desk structure as per the installation guide. Secondly, the previously automatically provided fields (focus keywo

73 lines (72 loc) 3.44 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import Jed from 'jed'; import get from 'just-safe-get'; import pixelWidth from 'string-pixel-width'; import yoastseoDefaultConfig from 'yoastseo/src/config/content/default.js'; import { AssessmentCategory } from '../constants'; var IgnoredAssessments = [ 'UrlLengthAssessment', 'TaxonomyTextLengthAssessment' ]; var AssessmentClassConfigKeyMap = { FleschReadingEaseAssessment: 'fleschReading', SentenceLengthInTextAssessment: 'sentenceLength', }; export var getYoastInsightsForContent = function (YoastSEO, html, options) { var _a; var paper = new YoastSEO.Paper(html, __assign(__assign({ keyword: options.keyword, url: options.url, permalink: options.permalink, title: options.title }, (options.title && { titleWidth: pixelWidth(options.title, { font: "arial", size: 20 }), })), { synonyms: options.synonyms.join(","), description: options.description, locale: options.langCulture.replace("-", "_") })); var researcher = new YoastSEO.Researcher(paper); var i18n = new Jed({ domain: 'js-text-analysis', locale_data: { 'js-text-analysis': { '': {} } }, }); console.debug('YoastSEO.assessments', YoastSEO.assessments); return Object.entries(YoastSEO.assessments).reduce(function (acc, _a) { var category = _a[0], assessments = _a[1]; Object.entries(assessments).forEach(function (_a) { var name = _a[0], value = _a[1]; if (!IgnoredAssessments.includes(name)) { if (typeof value === 'object' && (value === null || value === void 0 ? void 0 : value.hasOwnProperty('getResult'))) { var result = value.getResult(paper, researcher, i18n); if (result === null || result === void 0 ? void 0 : result.text) { acc[category].push(__assign(__assign({}, result), { rating: YoastSEO.helpers.scoreToRating(result.score) })); } } else if (typeof value === 'function') { var configKey = get(AssessmentClassConfigKeyMap, name, null); var config = configKey ? get(yoastseoDefaultConfig, configKey, {}) : {}; var result = new value(config).getResult(paper, researcher, i18n); if (result === null || result === void 0 ? void 0 : result.text) { acc[category].push(__assign(__assign({}, result), { rating: YoastSEO.helpers.scoreToRating(result.score) })); } } } }); if (Object.prototype.hasOwnProperty.call(acc, category)) { acc[category].sort(function (a, b) { if (a.rating === 'feedback') return -1; if (b.rating === 'feedback') return 1; return a.score < b.score ? -1 : 1; }); } return acc; }, (_a = {}, _a[AssessmentCategory.SEO] = [], _a[AssessmentCategory.READABILITY] = [], _a)); };