Phrase keys and target keys in core, so the page can record a decline itself
The normalised phrase a decline records (nolink:<phrase>) and a target's identity move from server/links to src/core/links.ts, which the browser can import; the server modules re-export them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
f9ce8faf6b
commit
428f7290ee
3 files changed
+38
-23
No files matched your search
+3
-15
@@ -9,8 +9,11 @@
|
|||||||
* lookups per node bounded.
|
* lookups per node bounded.
|
||||||
*/
|
*/
|
||||||
import type { Mark } from "../../src/core/marks";
|
import type { Mark } from "../../src/core/marks";
|
||||||
|
import { phraseKey } from "../../src/core/links";
|
||||||
import { UNLISTED, listed, rank, singular } from "./words";
|
import { UNLISTED, listed, rank, singular } from "./words";
|
||||||
|
|
||||||
|
export { phraseKey };
|
||||||
|
|
||||||
export type Kind = "forced" | "name" | "term" | "noun";
|
export type Kind = "forced" | "name" | "term" | "noun";
|
||||||
|
|
||||||
export interface Candidate {
|
export interface Candidate {
|
||||||
@@ -97,21 +100,6 @@ const low = (w: string) => w.toLowerCase();
|
|||||||
/** An adverb in -ly ("roughly", "independently"), when the stem is a word. */
|
/** An adverb in -ly ("roughly", "independently"), when the stem is a word. */
|
||||||
const adverb = (w: string) => /ly$/i.test(w) && w.length > 5 && listed(low(w)) && rank(low(w)) < UNLISTED && (listed(low(w).slice(0, -2)) || listed(low(w).slice(0, -3) + "e") || /ally$/i.test(w));
|
const adverb = (w: string) => /ly$/i.test(w) && w.length > 5 && listed(low(w)) && rank(low(w)) < UNLISTED && (listed(low(w).slice(0, -2)) || listed(low(w).slice(0, -3) + "e") || /ally$/i.test(w));
|
||||||
|
|
||||||
/** Normalised form of a phrase, for comparing phrases with each other and with titles. */
|
|
||||||
export function phraseKey(s: string): string {
|
|
||||||
return s
|
|
||||||
.normalize("NFKD")
|
|
||||||
.replace(/\p{M}/gu, "")
|
|
||||||
.toLowerCase()
|
|
||||||
.replace(/['’]s\b/g, "")
|
|
||||||
.replace(/[-–‑_/]/g, " ")
|
|
||||||
.replace(/[^\p{L}\p{N}\s]/gu, "")
|
|
||||||
.split(/\s+/)
|
|
||||||
.filter(Boolean)
|
|
||||||
.filter((w, i, all) => !(i === 0 && all.length > 1 && (w === "the" || w === "a" || w === "an")))
|
|
||||||
.map(singular)
|
|
||||||
.join(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
function variants(text: string): string[] {
|
function variants(text: string): string[] {
|
||||||
const out = [text];
|
const out = [text];
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
*/
|
*/
|
||||||
import { readFileSync } from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
|
import { singular } from "../../src/core/links";
|
||||||
|
|
||||||
|
export { singular };
|
||||||
|
|
||||||
let ranks: Map<string, number> | null = null;
|
let ranks: Map<string, number> | null = null;
|
||||||
|
|
||||||
@@ -37,11 +40,3 @@ export function rank(word: string): number {
|
|||||||
/** Whether a lower-case word is in the list at all. */
|
/** Whether a lower-case word is in the list at all. */
|
||||||
export const listed = (word: string) => load().has(word.toLowerCase());
|
export const listed = (word: string) => load().has(word.toLowerCase());
|
||||||
|
|
||||||
/** A light English singular: enough for "cephalopods", "octopuses", "theories", "boxes". */
|
|
||||||
export function singular(w: string): string {
|
|
||||||
if (w.length <= 3) return w;
|
|
||||||
if (/ies$/i.test(w) && w.length > 4) return w.slice(0, -3) + (w[w.length - 1] === "S" ? "Y" : "y");
|
|
||||||
if (/(uses|sses|shes|ches|xes|zes)$/i.test(w)) return w.slice(0, -2);
|
|
||||||
if (/s$/i.test(w) && !/(ss|us|is|ous)$/i.test(w) && !/ies$/i.test(w)) return w.slice(0, -1);
|
|
||||||
return w;
|
|
||||||
}
|
|
||||||
@@ -237,3 +237,35 @@ export function applyLinks(doc: Doc, specs: LinkSpec[], meta: { message?: string
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** What a target is, for telling one from another: an article by title, a node by id, a page by address. */
|
||||||
|
export const targetKey = (r: Ref) => (r.kind === "wiki" ? wikiKey(r.lang, r.title) : r.kind === "node" ? `node:${r.id}` : r.url);
|
||||||
|
|
||||||
|
// ---- phrases ----
|
||||||
|
|
||||||
|
/** A light English singular: enough for "cephalopods", "octopuses", "theories", "boxes". */
|
||||||
|
export function singular(w: string): string {
|
||||||
|
if (w.length <= 3) return w;
|
||||||
|
if (/ies$/i.test(w) && w.length > 4) return w.slice(0, -3) + (w[w.length - 1] === "S" ? "Y" : "y");
|
||||||
|
if (/(uses|sses|shes|ches|xes|zes)$/i.test(w)) return w.slice(0, -2);
|
||||||
|
if (/s$/i.test(w) && !/(ss|us|is|ous)$/i.test(w) && !/ies$/i.test(w)) return w.slice(0, -1);
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalised form of a phrase, for comparing phrases with each other and with titles: what link
|
||||||
|
* discovery matches titles with and what a declined link records in its node's `nolink`.
|
||||||
|
*/
|
||||||
|
export function phraseKey(s: string): string {
|
||||||
|
return s
|
||||||
|
.normalize("NFKD")
|
||||||
|
.replace(/\p{M}/gu, "")
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/['’]s\b/g, "")
|
||||||
|
.replace(/[-–‑_/]/g, " ")
|
||||||
|
.replace(/[^\p{L}\p{N}\s]/gu, "")
|
||||||
|
.split(/\s+/)
|
||||||
|
.filter(Boolean)
|
||||||
|
.filter((w, i, all) => !(i === 0 && all.length > 1 && (w === "the" || w === "a" || w === "an")))
|
||||||
|
.map(singular)
|
||||||
|
.join(" ");
|
||||||
|
}
|
||||||
Reference in new issue
Block a user