Merge pull request #19090 from overleaf/mj-web-metadata

[web] Ignore commented content when parsing metadata

GitOrigin-RevId: 78f9b0d6549e60fca4ba8929beb677341d885655
This commit is contained in:
Mathias Jakobsen 2024-06-25 09:05:16 +01:00 committed by Copybot
parent 64d9792fe3
commit e48e4293a6
2 changed files with 24 additions and 2 deletions

View file

@ -84,7 +84,8 @@ module.exports = MetaHandler = {
const labelRe = MetaHandler.labelRegex()
const packageRe = MetaHandler.usepackageRegex()
const reqPackageRe = MetaHandler.ReqPackageRegex()
for (const line of Array.from(lines)) {
for (const rawLine of Array.from(lines)) {
const line = MetaHandler._getNonCommentedContent(rawLine)
let labelMatch
let clean, messy, packageMatch
while ((labelMatch = labelRe.exec(line))) {
@ -128,4 +129,18 @@ module.exports = MetaHandler = {
}
return projectMeta
},
/**
* Trims comment content from line
* @param {string} rawLine
* @returns {string}
*/
_getNonCommentedContent(rawLine) {
const commentStart = /(?:^%)|(?:[^\\]%)/
const match = rawLine.match(commentStart)
if (match) {
return rawLine.slice(0, match.index)
}
return rawLine
},
}

View file

@ -187,7 +187,14 @@ describe('MetaHandler', function () {
describe('getMetaForDoc', function () {
beforeEach(function () {
this.fakeLines = ['\\usepackage{abc}', 'one', '\\label{aaa}', 'two']
this.fakeLines = [
'\\usepackage{abc}',
'one',
'\\label{aaa}',
'two',
// bbb should not be in the returned labels
'commented label % \\label{bbb}',
]
this.fakeMeta = { labels: ['aaa'], packages: ['abc'] }
this.DocumentUpdaterHandler.flushDocToMongo = sinon
.stub()