Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4105938
formatter.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
formatter.js
View Options
/**
* @module formatter
* @private
*/
/**
* Improves the plain text extracts
*
* @param {string} plainTextExtract
* @param {string} title
* @return {Array}
*/
export
function
formatPlainTextExtract
(
plainTextExtract
,
title
)
{
let
extract
=
plainTextExtract
;
if
(
plainTextExtract
===
undefined
)
{
return
[];
}
// After cleaning the extract it may have been blanked
if
(
extract
.
length
===
0
)
{
return
[];
}
extract
=
makeTitleInExtractBold
(
extract
,
title
);
return
extract
;
}
/**
* Converts the extract into a list of elements, which correspond to fragments
* of the extract. Fragments that match the title verbatim are wrapped in a
* `<b>` element.
*
* Using the bolded elements of the extract of the page directly is covered by
* [T141651](https://phabricator.wikimedia.org/T141651).
*
* Extracted from `mw.popups.renderer.article.getProcessedElements`.
*
* @param {string} extract
* @param {string} title
* @return {Array} A set of HTML Elements
*/
function
makeTitleInExtractBold
(
extract
,
title
)
{
const
elements
=
[],
boldIdentifier
=
`<bi-
${
Math
.
random
()
}
>`
,
snip
=
`<snip-
${
Math
.
random
()
}
>`
;
title
=
title
.
replace
(
/\s+/g
,
' '
).
trim
();
// Remove extra white spaces
const
escapedTitle
=
mw
.
util
.
escapeRegExp
(
title
);
// eslint-disable-next-line security/detect-non-literal-regexp
const
regExp
=
new
RegExp
(
`(^|\\s)(
${
escapedTitle
}
)(|$)`
,
'i'
);
// Remove text in parentheses along with the parentheses
extract
=
extract
.
replace
(
/\s+/
,
' '
);
// Remove extra white spaces
// Make title bold in the extract text
// As the extract is html escaped there can be no such string in it
// Also, the title is escaped of RegExp elements thus can't have "*"
extract
=
extract
.
replace
(
regExp
,
`$1
${
snip
}${
boldIdentifier
}
$2
${
snip
}
$3`
);
extract
=
extract
.
split
(
snip
);
extract
.
forEach
(
(
part
)
=>
{
if
(
part
.
indexOf
(
boldIdentifier
)
===
0
)
{
const
highlightNode
=
document
.
createElement
(
'b'
);
highlightNode
.
textContent
=
part
.
slice
(
boldIdentifier
.
length
);
elements
.
push
(
highlightNode
);
}
else
{
elements
.
push
(
document
.
createTextNode
(
part
)
);
}
}
);
return
elements
;
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Aug 18, 19:13 (2 w, 4 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
44/36/9ee1c7d367c36866260424b59c66
Default Alt Text
formatter.js (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment