File size: 1,351 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// @flow
import truncate from 'shared/truncate';
import type { RawContentState } from 'draft-js';
const nthIndexOf = (string, pattern, n) => {
var i = -1;
while (n-- && i++ < string.length) {
i = string.indexOf(pattern, i);
if (i < 0) break;
}
return i;
};
export default (state: RawContentState): string => {
const textBlocks = state.blocks.filter(
({ type }) =>
type === 'unstyled' ||
type.indexOf('header') === 0 ||
type.indexOf('list') > -1
);
const text = textBlocks
.map((block, index) => {
switch (block.type) {
case 'unordered-list-item':
return `• ${block.text}`;
case 'ordered-list-item': {
const number = textBlocks.reduce((number, b, i) => {
if (i >= index) return number;
if (b.type !== 'ordered-list-item') return number;
return number + 1;
}, 1);
return `${number}. ${block.text}`;
}
default:
return block.text;
}
})
.join('\n')
// Replace multiple line breaks with a single one
.replace(/[\r\n]+/g, '\n');
const indexOfThirdLineBreak = nthIndexOf(text, '\n', 3);
const cut = text.substr(
0,
indexOfThirdLineBreak > -1 ? indexOfThirdLineBreak : text.length
);
return truncate(cut !== text ? `${cut} …` : cut, 280);
};
|