Merge pull request #28315 from overleaf/mj-word-count-cite

[web] Support citations in client side word count

GitOrigin-RevId: d3ba3d6853d87059a202366f0c47bd9d7ba53cd8
This commit is contained in:
Mathias Jakobsen
2025-09-05 11:03:23 +01:00
committed by Copybot
parent 99ae091f02
commit bc154642da
3 changed files with 24 additions and 12 deletions

View File

@@ -165,15 +165,24 @@ export const countWordsInFile = (
context: currentContext,
})
},
Command(nodeRef) {
const child = nodeRef.node.getChild('UnknownCommand')
if (!child) return
Cite(nodeRef) {
// Count as \cite[text]{citation}
const optionalArgs = nodeRef.node.getChildren('OptionalArgument')
for (const arg of optionalArgs) {
// We normally ignore ShortOptionalArg, so we need to iterate it
// explicitly
const child = arg.getChild('ShortOptionalArg')
if (!child) continue
iterateNode(child, 'text')
}
return false
},
UnknownCommand(nodeRef) {
const macro =
nodeRef.node.getChild('$CtrlSeq') ?? nodeRef.node.getChild('$CtrlSym')
if (!macro) return
const grandchild =
child.getChild('$CtrlSeq') ?? child.getChild('$CtrlSym')
if (!grandchild) return
const commandName = content.substring(grandchild.from + 1, grandchild.to)
const commandName = content.substring(macro.from + 1, macro.to)
if (!commandName) return
switch (commandName) {

View File

@@ -65,12 +65,12 @@ describe('word-count', function () {
captionWords: 4,
footnoteCharacters: 8,
footnoteWords: 2,
headCharacters: 296,
headWords: 52,
headCharacters: 305,
headWords: 53,
otherCharacters: 10,
otherWords: 2,
textCharacters: 193,
textWords: 42,
textCharacters: 201,
textWords: 44,
})
})

View File

@@ -115,4 +115,7 @@ $2+3=5$ % 0
$ 2+3 \text{ is equal to } 5 $
\section{citations}
\cite[two words]{example2025} % 2 in text (citekey ignored)
\end{document}