Refactor interview parsing and answer splitting logic in Step4Report component

- Enhanced the `parseInterview` function to handle single-platform answers more effectively, ensuring default answers are set correctly.
- Improved the answer splitting logic to robustly identify numbered sections, accommodating various formats and ensuring cleaner output.
- Updated the handling of answer parts to remove unnecessary prefixes and trailing whitespace, enhancing overall readability.
This commit is contained in:
666ghj 2025-12-16 15:57:02 +08:00
parent 5f228357d5
commit 1659665a0b

View file

@ -727,8 +727,16 @@ const parseInterview = (text) => {
interview.redditAnswer = redditMatch[1].trim() interview.redditAnswer = redditMatch[1].trim()
} }
// //
if (!twitterMatch && !redditMatch) { //
if (!twitterMatch && redditMatch) {
// Reddit twitterAnswer
interview.twitterAnswer = interview.redditAnswer
} else if (twitterMatch && !redditMatch) {
// Twitter redditAnswer
interview.redditAnswer = interview.twitterAnswer
} else if (!twitterMatch && !redditMatch) {
//
interview.twitterAnswer = answerText interview.twitterAnswer = answerText
} }
} }
@ -954,37 +962,51 @@ const InterviewDisplay = {
const splitAnswerByQuestions = (answerText, questionCount) => { const splitAnswerByQuestions = (answerText, questionCount) => {
if (!answerText || questionCount <= 0) return [answerText] if (!answerText || questionCount <= 0) return [answerText]
// ( "1." "2." ) // "."
//
// - "1. \n" ++++
// - "\n\n2. \n" +++++
// 使++
const numberPattern = /(?:^|[\r\n]+)(\d+)\.\s+/g
const matches = []
let match
while ((match = numberPattern.exec(answerText)) !== null) {
matches.push({
num: parseInt(match[1]),
index: match.index,
fullMatch: match[0]
})
}
//
if (matches.length <= 1) {
// 1. \n 1.
const cleaned = answerText.replace(/^\d+\.\s+/, '').trim()
return [cleaned || answerText]
}
//
const parts = [] const parts = []
let remaining = answerText for (let i = 0; i < matches.length; i++) {
const current = matches[i]
for (let i = 1; i <= questionCount; i++) { const next = matches[i + 1]
const nextNum = i + 1
//
const nextPattern = new RegExp(`\\n\\s*${nextNum}\\.\\s+|\\n\\s*${nextNum}、|\\n\\s*${nextNum}|\\n\\s*\\(${nextNum}\\)`)
const match = remaining.match(nextPattern)
if (match) { const startIdx = current.index + current.fullMatch.length
// const endIdx = next ? next.index : answerText.length
const splitIdx = match.index
let currentPart = remaining.substring(0, splitIdx).trim() let part = answerText.substring(startIdx, endIdx).trim()
// //
currentPart = currentPart.replace(new RegExp(`^\\s*${i}\\.\\s*|^\\s*${i}、|^\\s*${i}|^\\s*\\(${i}\\)`), '').trim() part = part.replace(/[\r\n]+$/, '').trim()
parts.push(currentPart) parts.push(part)
remaining = remaining.substring(splitIdx).trim()
} else if (i === questionCount) {
//
let currentPart = remaining.replace(new RegExp(`^\\s*${i}\\.\\s*|^\\s*${i}、|^\\s*${i}|^\\s*\\(${i}\\)`), '').trim()
parts.push(currentPart)
}
} }
// //
if (parts.length === 0 || parts.every(p => !p)) { if (parts.length > 0 && parts.some(p => p)) {
return [answerText] return parts
} }
return parts return [answerText]
} }
// //