@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
190 lines (188 loc) • 6.75 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/core-data/src/fetch/__experimental-fetch-link-suggestions.ts
var experimental_fetch_link_suggestions_exports = {};
__export(experimental_fetch_link_suggestions_exports, {
default: () => fetchLinkSuggestions,
sortResults: () => sortResults,
tokenize: () => tokenize
});
module.exports = __toCommonJS(experimental_fetch_link_suggestions_exports);
var import_api_fetch = __toESM(require("@wordpress/api-fetch"));
var import_url = require("@wordpress/url");
var import_html_entities = require("@wordpress/html-entities");
var import_i18n = require("@wordpress/i18n");
async function fetchLinkSuggestions(search, searchOptions = {}, editorSettings = {}) {
const searchOptionsToUse = searchOptions.isInitialSuggestions && searchOptions.initialSuggestionsSearchOptions ? {
...searchOptions,
...searchOptions.initialSuggestionsSearchOptions
} : searchOptions;
const {
type,
subtype,
page,
perPage = searchOptions.isInitialSuggestions ? 3 : 20
} = searchOptionsToUse;
const { disablePostFormats = false } = editorSettings;
const queries = [];
if (!type || type === "post") {
queries.push(
(0, import_api_fetch.default)({
path: (0, import_url.addQueryArgs)("/wp/v2/search", {
search,
page,
per_page: perPage,
type: "post",
subtype
})
}).then((results2) => {
return results2.map((result) => {
return {
id: result.id,
url: result.url,
title: (0, import_html_entities.decodeEntities)(result.title || "") || (0, import_i18n.__)("(no title)"),
type: result.subtype || result.type,
kind: "post-type"
};
});
}).catch(() => [])
// Fail by returning no results.
);
}
if (!type || type === "term") {
queries.push(
(0, import_api_fetch.default)({
path: (0, import_url.addQueryArgs)("/wp/v2/search", {
search,
page,
per_page: perPage,
type: "term",
subtype
})
}).then((results2) => {
return results2.map((result) => {
return {
id: result.id,
url: result.url,
title: (0, import_html_entities.decodeEntities)(result.title || "") || (0, import_i18n.__)("(no title)"),
type: result.subtype || result.type,
kind: "taxonomy"
};
});
}).catch(() => [])
// Fail by returning no results.
);
}
if (!disablePostFormats && (!type || type === "post-format")) {
queries.push(
(0, import_api_fetch.default)({
path: (0, import_url.addQueryArgs)("/wp/v2/search", {
search,
page,
per_page: perPage,
type: "post-format",
subtype
})
}).then((results2) => {
return results2.map((result) => {
return {
id: result.id,
url: result.url,
title: (0, import_html_entities.decodeEntities)(result.title || "") || (0, import_i18n.__)("(no title)"),
type: result.subtype || result.type,
kind: "taxonomy"
};
});
}).catch(() => [])
// Fail by returning no results.
);
}
if (!type || type === "attachment") {
queries.push(
(0, import_api_fetch.default)({
path: (0, import_url.addQueryArgs)("/wp/v2/media", {
search,
page,
per_page: perPage
})
}).then((results2) => {
return results2.map((result) => {
return {
id: result.id,
url: result.source_url,
title: (0, import_html_entities.decodeEntities)(result.title.rendered || "") || (0, import_i18n.__)("(no title)"),
type: result.type,
kind: "media"
};
});
}).catch(() => [])
// Fail by returning no results.
);
}
const responses = await Promise.all(queries);
let results = responses.flat();
results = results.filter((result) => !!result.id);
results = sortResults(results, search);
results = results.slice(0, perPage);
return results;
}
function sortResults(results, search) {
const searchTokens = tokenize(search);
const scores = {};
for (const result of results) {
if (result.title) {
const titleTokens = tokenize(result.title);
const exactMatchingTokens = titleTokens.filter(
(titleToken) => searchTokens.some(
(searchToken) => titleToken === searchToken
)
);
const subMatchingTokens = titleTokens.filter(
(titleToken) => searchTokens.some(
(searchToken) => titleToken !== searchToken && titleToken.includes(searchToken)
)
);
const exactMatchScore = exactMatchingTokens.length / titleTokens.length * 10;
const subMatchScore = subMatchingTokens.length / titleTokens.length;
scores[result.id] = exactMatchScore + subMatchScore;
} else {
scores[result.id] = 0;
}
}
return results.sort((a, b) => scores[b.id] - scores[a.id]);
}
function tokenize(text) {
return text.toLowerCase().match(/[\p{L}\p{N}]+/gu) || [];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
sortResults,
tokenize
});
//# sourceMappingURL=__experimental-fetch-link-suggestions.js.map