-
-
Notifications
You must be signed in to change notification settings - Fork 19
Add the deep fragmenter record-building interface #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aileen
wants to merge
1
commit into
main
Choose a base branch
from
deep-fragmenter-record-policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/algolia-fragmenter/lib/create-algolia-records.d.mts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { type CreateAlgoliaRecordsOptions } from './policy.mjs'; | ||
| import { type GhostContent } from './projection.mjs'; | ||
| import { type AlgoliaRecord } from './records.mjs'; | ||
| /** | ||
| * Turns Ghost content into complete final Algolia records: projection, HTML extraction, legacy | ||
| * anchor grouping, fallback records, deep links, identifiers, ranking metadata, and | ||
| * deterministic record-size handling. | ||
| * | ||
| * The whole batch is validated before any record is returned. A deterministic policy, Ghost | ||
| * content, or record-size problem throws one {@link FragmenterError} carrying every issue in | ||
| * input order; a partial batch is never returned. | ||
| * | ||
| * @throws {FragmenterError} `INVALID_POLICY`, `INVALID_GHOST_CONTENT`, or `RECORD_TOO_LARGE`. | ||
| */ | ||
| export declare const createAlgoliaRecords: (ghostContent: readonly GhostContent[], options?: CreateAlgoliaRecordsOptions) => readonly AlgoliaRecord[]; | ||
| //# sourceMappingURL=create-algolia-records.d.mts.map |
1 change: 1 addition & 0 deletions
1
packages/algolia-fragmenter/lib/create-algolia-records.d.mts.map
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
packages/algolia-fragmenter/lib/create-algolia-records.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { extract } from '@tryghost/algolia-html-extractor'; | ||
| import { FragmenterError } from './errors.mjs'; | ||
| import { groupFragmentsByAnchor } from './grouping.mjs'; | ||
| import { resolvePolicy } from './policy.mjs'; | ||
| import { prepareGhostContent } from './projection.mjs'; | ||
| import { createContentRecords } from './records.mjs'; | ||
| /** | ||
| * Turns Ghost content into complete final Algolia records: projection, HTML extraction, legacy | ||
| * anchor grouping, fallback records, deep links, identifiers, ranking metadata, and | ||
| * deterministic record-size handling. | ||
| * | ||
| * The whole batch is validated before any record is returned. A deterministic policy, Ghost | ||
| * content, or record-size problem throws one {@link FragmenterError} carrying every issue in | ||
| * input order; a partial batch is never returned. | ||
| * | ||
| * @throws {FragmenterError} `INVALID_POLICY`, `INVALID_GHOST_CONTENT`, or `RECORD_TOO_LARGE`. | ||
| */ | ||
| export const createAlgoliaRecords = (ghostContent, options) => { | ||
| const policy = resolvePolicy(options); | ||
| if (!policy.ok) { | ||
| throw new FragmenterError('INVALID_POLICY', policy.issues); | ||
| } | ||
| const prepared = prepareGhostContent(ghostContent, policy.policy); | ||
| if (!prepared.ok) { | ||
| throw new FragmenterError('INVALID_GHOST_CONTENT', prepared.issues); | ||
| } | ||
| const records = []; | ||
| const issues = []; | ||
| for (const content of prepared.contents) { | ||
| const groups = groupFragmentsByAnchor(extract(content.html)); | ||
| const contentRecords = createContentRecords(content, groups); | ||
| records.push(...contentRecords.records); | ||
| issues.push(...contentRecords.issues); | ||
| } | ||
| if (issues.length > 0) { | ||
| throw new FragmenterError('RECORD_TOO_LARGE', issues); | ||
| } | ||
| return records; | ||
| }; | ||
| //# sourceMappingURL=create-algolia-records.mjs.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| export type FragmenterErrorCode = 'INVALID_POLICY' | 'INVALID_GHOST_CONTENT' | 'RECORD_TOO_LARGE'; | ||
| export type PolicyIssueReason = 'invalid-shape' | 'unknown-property' | 'unknown-source' | 'repeated-source' | 'repeated-output' | 'invalid-alias' | 'protected-collision' | 'container-collision' | 'canonical-collision' | 'reserved-collision'; | ||
| export type PolicyIssue = Readonly<{ | ||
| kind: 'policy'; | ||
| reason: PolicyIssueReason; | ||
| path: string; | ||
| message: string; | ||
| }>; | ||
| export type GhostContentIssueReason = 'invalid-shape' | 'missing' | 'wrong-type'; | ||
| export type ExpectedValueType = 'string' | 'number' | 'boolean' | 'object' | 'array'; | ||
| export type GhostContentIssue = Readonly<{ | ||
| kind: 'content'; | ||
| reason: GhostContentIssueReason; | ||
| path: string; | ||
| index: number | null; | ||
| contentId: string | null; | ||
| expected: ExpectedValueType; | ||
| message: string; | ||
| }>; | ||
| export type RecordSizeIssue = Readonly<{ | ||
| kind: 'size'; | ||
| reason: 'record-too-large'; | ||
| path: string; | ||
| index: number; | ||
| contentId: string; | ||
| objectID: string; | ||
| anchor: string | null; | ||
| position: number | null; | ||
| bytes: number; | ||
| limit: number; | ||
| excess: number; | ||
| message: string; | ||
| }>; | ||
| export type FragmenterIssue = PolicyIssue | GhostContentIssue | RecordSizeIssue; | ||
| /** | ||
| * The single public error for every deterministic policy, Ghost content, or record size | ||
| * problem found while building Algolia records. It never carries a partial record batch. | ||
| */ | ||
| export declare class FragmenterError extends Error { | ||
| readonly code: FragmenterErrorCode; | ||
| readonly issues: readonly FragmenterIssue[]; | ||
| constructor(code: FragmenterErrorCode, issues: readonly FragmenterIssue[]); | ||
| } | ||
| //# sourceMappingURL=errors.d.mts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| const MESSAGE_ISSUE_LIMIT = 5; | ||
| const describeIssues = (code, issues) => { | ||
| const listed = issues.slice(0, MESSAGE_ISSUE_LIMIT).map(issue => issue.message); | ||
| const remaining = issues.length - listed.length; | ||
| const suffix = remaining > 0 ? `; and ${remaining} more` : ''; | ||
| const count = `${issues.length} issue${issues.length === 1 ? '' : 's'}`; | ||
| return `${code}: ${count}. ${listed.join('; ')}${suffix}`; | ||
| }; | ||
| /** | ||
| * The single public error for every deterministic policy, Ghost content, or record size | ||
| * problem found while building Algolia records. It never carries a partial record batch. | ||
| */ | ||
| export class FragmenterError extends Error { | ||
| code; | ||
| issues; | ||
| constructor(code, issues) { | ||
| super(describeIssues(code, issues)); | ||
| this.name = 'FragmenterError'; | ||
| this.code = code; | ||
| this.issues = Object.freeze([...issues]); | ||
| } | ||
| } | ||
| //# sourceMappingURL=errors.mjs.map |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Document that
fieldsis required, and that content issueindex/contentIdcan benull.Two documentation gaps exist against the code in this cohort:
contentProjection.fieldsis mandatory.resolveFieldsinpackages/algolia-fragmenter/src/policy.mtspushes aninvalid-shapeissue whenfieldsis absent, so{contentProjection: {customRanking: [...]}}throwsINVALID_POLICY. Line 42 does not state this.indexand the Ghost content id.createBatchShapeIssue()inpackages/algolia-fragmenter/src/projection.mtssets both tonull, andreadContentIdreturnsnullwhenidis absent or empty.📝 Proposed documentation fix
Also applies to: 69-69
🤖 Prompt for AI Agents