Skip to content

Commit

Permalink
refactor(node): more robust content body chunking
Browse files Browse the repository at this point in the history
* node can apparently send random newlines mid-sentence
* improve sentence logic
* increase fallback to 100 characters (20 was very few)
  • Loading branch information
almostSouji committed Jul 25, 2024
1 parent c16949c commit 121965b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/functions/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,22 @@ export async function nodeAutoCompleteResolve(res: Response, query: string, user
const headingCode = $(headingCodeSelector).text();
const paragraph = $(paragraphSelector).nextUntil('h4', 'p');

const text = paragraph.text();
const fullSentence = text.split('. ')?.[0];
const partSentence = text.split('.')?.[0];

prepareResponse(
res,
[
`<:node:${EMOJI_ID_NODE}> ${hyperlink(inlineCode(headingCode.length ? headingCode : heading), url.toString())}`,
`${fullSentence ?? partSentence ?? `${truncate(text, 20, '')}..`}.`,
].join('\n'),
{ ephemeral, suggestion: user ? { userId: user, kind: 'documentation' } : undefined },
);
const text = paragraph.text().split('\n').join(' ');
const sentence = text.split(/[!.?](\s|$)/)?.[0];
const effectiveSentence = (sentence ?? truncate(text, 100, '')).trim();

const contentParts = [
`<:node:${EMOJI_ID_NODE}> ${hyperlink(inlineCode(headingCode.length ? headingCode : heading), url.toString())}`,
];

if (effectiveSentence.length) {
contentParts.push(`${effectiveSentence}.`);
}

prepareResponse(res, contentParts.join('\n'), {
ephemeral,
suggestion: user ? { userId: user, kind: 'documentation' } : undefined,
});

return res;
}
Expand Down

0 comments on commit 121965b

Please sign in to comment.