Generate a summarized search result string. Zero dependency utils.
npm install search-summary
import { generateSummary } from 'search-summary';
const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';
const config = {
ellipsisToken: '...',
keywordModifier: (keyword) => `**${keyword}**`
};
generateSummary(text, 'ipsum', config);
// 'Lorem **ipsum** dolor sit amet, consectetur adipiscing...'
generateSummary(text, 'Ut enim', config);
// '...olore magna aliqua. **Ut enim** ad minim veniam, quis ...'
generateSummary(text, 'commodo', config);
// '...si ut aliquip ex ea **commodo** consequat.'
generateSummary(text, 'dummy keyword', config);
// undefined
Or you can get an object.
import { generateSummaryEntity } from 'search-summary';
generateSummaryEntity(text, 'ipsum');
// {
// isBeforeEllipsed: false,
// beforeText: 'Lorem ',
// keyword: 'ipsum',
// afterText: ' dolor sit amet, consectetur adipiscing',
// isAfterEllipsed: true
// }
// example: default config
generateSummary(text, keyword, {
maxLength: 50,
beforeLength: 20,
ellipsisToken: '...',
keywordModifier: (keyword: string) => keyword,
});
generateSummaryEntity(text, keyword, {
maxLength: 50,
beforeLength: 20,
});
Or you can pass the config in advance.
import { generateSummaryFactory, generateSummaryEntityFactory } from 'search-summary';
const generateSummary = generateSummaryFactory(config);
generateSummary(text, keyword);
const generateSummaryEntity = generateSummaryEntityFactory(config);
generateSummaryEntity(text, keyword);
MIT © kyonenya