Skip to content

Commit

Permalink
Merge pull request #581 from audioverse-org/refactor/transcript
Browse files Browse the repository at this point in the history
refactor: use paragraph version of the transcript
  • Loading branch information
alangumer authored Oct 3, 2024
2 parents b16dbb1 + c52f7db commit 15eb73a
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions src/components/molecules/transcript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,6 @@ import { RecordingContentType } from '~src/__generated__/graphql';

import styles from './transcript.module.scss';

// @see https://stackoverflow.com/a/37400795/168581
function splitText(text: string): string[] {
const paragraphs: string[] = [];
const sentenceRegex = /.*?[.!?]+(\s+|$)/g;
const sentences = text.match(sentenceRegex);

let paragraph = '';
sentences?.forEach((sentence, index) => {
paragraph += sentence;

if (paragraph.length >= 200 || index === sentences.length - 1) {
paragraphs.push(paragraph);
paragraph = '';
}
});

return paragraphs.length === 0 ? [text] : paragraphs;
}

export default function Transcript({
text,
recordingContentType,
Expand All @@ -36,8 +17,10 @@ export default function Transcript({
const isManuallyCreatedTranscript = text.includes('<p');
const __html = isManuallyCreatedTranscript
? text
: splitText(text)
.map((t) => `<p>${t}</p>`)
: text
.replace(/^\n+/, '')
.split('\n')
.map((paragraph) => `<p>${paragraph}</p>`)
.join('');

return (
Expand Down

0 comments on commit 15eb73a

Please sign in to comment.