Przeglądaj źródła

Parse labels from environment options (#24189)

GitOrigin-RevId: e51eed7521f6e32e614f8b38092a0b0219f7f186
Alf Eaton 1 rok temu
rodzic
commit
fe4f41501f

+ 7 - 2
services/web/app/src/Features/Metadata/MetaHandler.mjs

@@ -23,14 +23,19 @@ async function extractMetaFromDoc(lines) {
   }
 
   const labelRe = /\\label{(.{0,80}?)}/g
+  const labelOptionRe = /\blabel={?(.{0,80}?)[\s},\]]/g
   const packageRe = /^\\usepackage(?:\[.{0,80}?])?{(.{0,80}?)}/g
   const reqPackageRe = /^\\RequirePackage(?:\[.{0,80}?])?{(.{0,80}?)}/g
 
   for (const rawLine of lines) {
     const line = getNonCommentedContent(rawLine)
 
-    for (const pkg of lineMatches(labelRe, line)) {
-      docMeta.labels.push(pkg)
+    for (const label of lineMatches(labelRe, line)) {
+      docMeta.labels.push(label)
+    }
+
+    for (const label of lineMatches(labelOptionRe, line)) {
+      docMeta.labels.push(label)
     }
 
     for (const pkg of lineMatches(packageRe, line, ',')) {

+ 2 - 1
services/web/frontend/js/features/source-editor/languages/latex/metadata.ts

@@ -2,6 +2,7 @@ import { EditorView } from '@codemirror/view'
 import { Transaction, Text } from '@codemirror/state'
 
 const metadataChangeRe = /\\(documentclass|usepackage|RequirePackage|label)\b/
+const optionChangeRe = /\b(label)=/
 
 export const metadata = () => [
   // trigger metadata reload if edited line contains metadata-related commands
@@ -26,7 +27,7 @@ export const metadata = () => [
             const toLine = doc.lineAt(to).number
 
             for (const line of doc.iterLines(fromLine, toLine + 1)) {
-              if (metadataChangeRe.test(line)) {
+              if (metadataChangeRe.test(line) || optionChangeRe.test(line)) {
                 needsMetadataUpdate = true
                 return
               }

+ 3 - 1
services/web/test/unit/src/Metadata/MetaHandlerTests.mjs

@@ -19,6 +19,8 @@ describe('MetaHandler', function () {
       '\\label{ccc}%bar', // ccc should be in the returned labels
       '\\label{ddd} % bar', // ddd should be in the returned labels
       '\\label{ e,f,g }', // e,f,g should be in the returned labels
+      '\\begin{lstlisting}[label=foo, caption={Test}]', // foo should be in the returned labels
+      '\\begin{lstlisting}[label={lst:foo},caption={Test}]', // lst:foo should be in the returned labels
     ]
 
     this.docs = {
@@ -85,7 +87,7 @@ describe('MetaHandler', function () {
       )
 
       expect(result).to.deep.equal({
-        labels: ['aaa', 'ccc', 'ddd', 'e,f,g'],
+        labels: ['aaa', 'ccc', 'ddd', 'e,f,g', 'foo', 'lst:foo'],
         packages: {
           foo: this.packageMapping.foo,
           baz: this.packageMapping.baz,