Content deleted Content added
Polygnotus (talk | contribs) No edit summary |
Polygnotus (talk | contribs) No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 202:
font-size: 13px;
line-height: 1.5;
max-height:
overflow-y: auto;
white-space: pre-wrap;
Line 480:
}
extractClaimText(refElement) {
// Get the paragraph or container
const container = refElement.closest('p, li, td, div, section');
return '';
}
// Get all text content from the
let fullText = '';
const walker = document.createTreeWalker(
container,
false
);
let textNode;
const textNodes = [];
while (textNode = walker.nextNode()) {
node: textNode,
offset: fullText.length
});
fullText
// Find the position of the reference in the full text
let refPosition = -1;
// Look for the reference pattern in the text
const refPattern = new RegExp('\\[\\s*' + refText.replace(/[\[\]]/g, '') + '\\s*\\]');
const
if (refMatch) {
refPosition = refMatch.index;
} else {
// Fallback: find any reference pattern before our element
const allRefs =
if (allRefs)
// Find the last
let searchPos = 0;
const index = fullText.indexOf(ref, searchPos);
if (index !== -1) {
lastRefIndex = index;
searchPos = index + ref.length;
}
}
refPosition = lastRefIndex;
}
}
if (refPosition === -1) {
refPosition = fullText.length;
}
// Extract text before the reference
const textBeforeRef = fullText.substring(0, refPosition).trim();
// Find the last complete sentence
const sentences = textBeforeRef.split(/([.!?]+\s+)/);
if (sentences.length === 1) {
// No sentence breaks found, return the entire text
return textBeforeRef;
}
// Reconstruct the last sentence
let lastSentence = '';
let foundSentenceEnd = false;
// Work backwards through the sentence fragments
for (let i = sentences.length - 1; i >= 0; i--) {
const fragment = sentences[i];
if (fragment.match(/^[.!?]+\s*$/)) {
// This is punctuation + whitespace
if (!foundSentenceEnd) {
foundSentenceEnd = true;
continue;
} else {
// We've found the end of the previous sentence
break;
}
} else {
// This is text content
lastSentence = fragment + lastSentence;
if (foundSentenceEnd) {
// We have a complete sentence
break;
}
}
}
// If we didn't find a proper sentence boundary, fall back to a more aggressive approach
if (!lastSentence.trim() || lastSentence.trim().length < 10) {
// Look for other potential sentence boundaries
const alternativeBreaks = textBeforeRef.split(/([.!?:;]\s+|^\s*[A-Z])/);
if (alternativeBreaks.length > 1) {
lastSentence = alternativeBreaks[alternativeBreaks.length - 1];
} else {
// As a last resort, take a reasonable chunk from the end
const words = textBeforeRef.split(/\s+/);
if (words.length > 15) {
lastSentence = words.slice(-15).join(' ');
} else {
lastSentence = textBeforeRef;
}
}
}
// Clean up the result
lastSentence = lastSentence.trim();
// Ensure it ends with proper punctuation for context
if (lastSentence && !lastSentence.match(/[.!?]$/)) {
// Check if the original text continues with punctuation
const nextChar = fullText.charAt(refPosition - lastSentence.length - 1);
if (nextChar.match(/[.!?]/)) {
lastSentence = nextChar + ' ' + lastSentence;
}
}
// Remove any remaining reference brackets that might have been included
lastSentence = lastSentence.replace(/\[\d+\]/g, '').trim();
return lastSentence;
}
extractReferenceUrl(refElement) {
|