Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scoring/src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ function textMatches(text: string, words: string[]): boolean {

function clampText(text: string, limit: number): string {
const clean = text.replace(/\s+/g, " ").trim();
return clean.length > limit ? `${clean.slice(0, limit - 1)}…` : clean;
if (clean.length <= limit) return clean;

let end = limit - 1;
const boundaryCodePoint = clean.codePointAt(end - 1);
// If the final retained code unit starts a surrogate pair, omit the whole pair.
if (boundaryCodePoint !== undefined && boundaryCodePoint > 0xffff) end--;
return `${clean.slice(0, end)}…`;
}

export function parseReadmeFeatures(markdown: string): ReadmeFeatures {
Expand Down
30 changes: 30 additions & 0 deletions scoring/test/score.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,36 @@ Install the package.
expect(features.prompt_summary).toContain("A clear overview");
});

const introPrefix = "文".repeat(348);
const sectionPrefix = "a".repeat(218);
const titlePrefix = "a".repeat(1491);
const prefixWithEmoji = `${"文".repeat(347)}🚀`;

it.each([
["an intro", `# Project\n\n${introPrefix}🚀end`, `Intro: ${introPrefix}… Sections:`],
[
"a section",
`# Project\n\nOverview.\n\n## Usage\n\n${sectionPrefix}🚀end`,
`Usage: ${sectionPrefix}… Signals: usage`,
],
["the full summary", `# ${titlePrefix}🚀end`, `Title: ${titlePrefix}…`],
[
"an intro where the emoji fits before the ellipsis",
`# Project\n\n${prefixWithEmoji}end`,
`Intro: ${prefixWithEmoji}… Sections:`,
],
[
"an intro at the length limit",
`# Project\n\n${introPrefix}🚀`,
`Intro: ${introPrefix}🚀 Sections:`,
],
])("preserves Unicode in %s", (_part, markdown, expected) => {
const { prompt_summary } = parseReadmeFeatures(markdown);
expect(prompt_summary).not.toMatch(/[\uD800-\uDFFF]/u);
expect(prompt_summary).toContain(expected);
expect(prompt_summary.length).toBeLessThanOrEqual(1500);
});

it("treats Quick Start as a usage signal", () => {
const features = parseReadmeFeatures(`
# Project
Expand Down
Loading