Merge pull request #3863 from overleaf/hb-clear-content-outside-heading-def

Remove content outside of the title commands for outline

GitOrigin-RevId: 4361521b5e1ae8adc853ed4fb250cbeeaebb1d71
This commit is contained in:
Miguel Serrano 2021-03-31 11:26:39 +02:00 committed by Copybot
parent afc15335c7
commit 7a36373a0f
2 changed files with 25 additions and 2 deletions

View file

@ -50,6 +50,7 @@ function matchOutline(content) {
} }
const DISPLAY_TITLE_REGEX = new RegExp('([^\\\\]*)\\\\([^{]+){([^}]+)}(.*)') const DISPLAY_TITLE_REGEX = new RegExp('([^\\\\]*)\\\\([^{]+){([^}]+)}(.*)')
const END_OF_TITLE_REGEX = new RegExp('^([^{}]*?({[^{}]*?}[^{}]*?)*)}')
/* /*
* Attempt to improve the display of the outline title for titles with commands. * Attempt to improve the display of the outline title for titles with commands.
* Either skip the command (for labels) or display the command's content instead * Either skip the command (for labels) or display the command's content instead
@ -61,11 +62,19 @@ const DISPLAY_TITLE_REGEX = new RegExp('([^\\\\]*)\\\\([^{]+){([^}]+)}(.*)')
*/ */
function matchDisplayTitle(title) { function matchDisplayTitle(title) {
const closingBracketPosition = title.indexOf('}') const closingBracketPosition = title.indexOf('}')
if (closingBracketPosition < 0) { if (closingBracketPosition < 0) {
// simple title (no commands) // simple title (no commands)
return title return title
} }
// if there is anything outside the title def on the line, remove it
// before proceeding
const titleOnlyMatch = title.match(END_OF_TITLE_REGEX)
if (titleOnlyMatch) {
title = titleOnlyMatch[1]
}
const titleMatch = title.match(DISPLAY_TITLE_REGEX) const titleMatch = title.match(DISPLAY_TITLE_REGEX)
if (!titleMatch) { if (!titleMatch) {
// no contained commands; strip everything after the first closing bracket // no contained commands; strip everything after the first closing bracket

View file

@ -47,13 +47,27 @@ describe('OutlineParser', function() {
\\section{Label after \\label{foo}} \\section{Label after \\label{foo}}
\\section{Label \\label{foo} between} \\section{Label \\label{foo} between}
\\section{TT \\texttt{Bar}} \\section{TT \\texttt{Bar}}
` \\section{plain title}
`
const outline = matchOutline(content) const outline = matchOutline(content)
expect(outline).to.deep.equal([ expect(outline).to.deep.equal([
{ line: 2, title: ' Label before', level: 40 }, { line: 2, title: ' Label before', level: 40 },
{ line: 3, title: 'Label after ', level: 40 }, { line: 3, title: 'Label after ', level: 40 },
{ line: 4, title: 'Label between', level: 40 }, { line: 4, title: 'Label between', level: 40 },
{ line: 5, title: 'TT Bar', level: 40 } { line: 5, title: 'TT Bar', level: 40 },
{ line: 6, title: 'plain title', level: 40 }
])
})
it('removes spurious commands after title definition', function() {
const content = `
\\section{Plain title} more text \\href{link}{link}
\\section{\\label{foo} Label before} more text \\href{link}{link}
`
const outline = matchOutline(content)
expect(outline).to.deep.equal([
{ line: 2, title: 'Plain title', level: 40 },
{ line: 3, title: ' Label before', level: 40 }
]) ])
}) })