Skip to content

Commit

Permalink
Sanitize double quoted input file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Dec 7, 2024
1 parent 6b37c0d commit ddec989
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/outline/structure/latex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type * as Ast from '@unified-latex/unified-latex-types'
import { lw } from '../../lw'
import { type TeXElement, TeXElementType } from '../../types'
import { resolveFile } from '../../utils/utils'
import { InputFileRegExp } from '../../utils/inputfilepath'
import { InputFileRegExp, sanitizeInputFilePath } from '../../utils/inputfilepath'


import { argContentToStr } from '../../utils/parser'
Expand Down Expand Up @@ -168,7 +168,7 @@ async function parseNode(
...attributes
}
} else if (node.type === 'macro' && ['input', 'InputIfFileExists', 'include', 'SweaveInput', 'subfile', 'loadglsentries', 'markdownInput'].includes(node.content)) {
const arg0 = argContentToStr(node.args?.[0]?.content || [])
const arg0 = sanitizeInputFilePath(argContentToStr(node.args?.[0]?.content || []))
const subFile = await resolveFile([ path.dirname(filePath), path.dirname(lw.root.file.path || ''), ...config.texDirs ], arg0)
if (subFile) {
element = {
Expand All @@ -182,8 +182,8 @@ async function parseNode(
}
}
} else if (node.type === 'macro' && ['import', 'inputfrom', 'includefrom'].includes(node.content)) {
const arg0 = argContentToStr(node.args?.[0]?.content || [])
const arg1 = argContentToStr(node.args?.[1]?.content || [])
const arg0 = sanitizeInputFilePath(argContentToStr(node.args?.[0]?.content || []))
const arg1 = sanitizeInputFilePath(argContentToStr(node.args?.[1]?.content || []))
const subFile = await resolveFile([ arg0, path.join(path.dirname(lw.root.file.path || ''), arg0 )], arg1)
if (subFile) {
element = {
Expand All @@ -197,8 +197,8 @@ async function parseNode(
}
}
} else if (node.type === 'macro' && ['subimport', 'subinputfrom', 'subincludefrom'].includes(node.content)) {
const arg0 = argContentToStr(node.args?.[0]?.content || [])
const arg1 = argContentToStr(node.args?.[1]?.content || [])
const arg0 = sanitizeInputFilePath(argContentToStr(node.args?.[0]?.content || []))
const arg1 = sanitizeInputFilePath(argContentToStr(node.args?.[1]?.content || []))
const subFile = await resolveFile([ path.dirname(filePath) ], path.join(arg0, arg1))
if (subFile) {
element = {
Expand Down
19 changes: 14 additions & 5 deletions src/utils/inputfilepath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,33 @@ export class InputFileRegExp {
* @param rootFile
*/
static async parseInputFilePath(match: MatchPath, currentFile: string, rootFile: string): Promise<string | undefined> {

const rawTexDirs = vscode.workspace.getConfiguration('latex-workshop').get('latex.texDirs') as string[]
const texDirs = rawTexDirs.map((texDir) => {return replaceArgumentPlaceholders('', '')(texDir)})

const matchedDir = sanitizeInputFilePath(match.directory)
const matchedPath = sanitizeInputFilePath(match.path)
/* match of this.childReg */
if (match.type === MatchType.Child) {
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], match.path)
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], matchedPath)
}

/* match of this.inputReg */
if (match.type === MatchType.Input) {
if (match.matchedString.startsWith('\\subimport') || match.matchedString.startsWith('\\subinputfrom') || match.matchedString.startsWith('\\subincludefrom')) {
return resolveFile([path.dirname(currentFile)], path.join(match.directory, match.path))
return resolveFile([path.dirname(currentFile)], path.join(matchedDir, matchedPath))
} else if (match.matchedString.startsWith('\\import') || match.matchedString.startsWith('\\inputfrom') || match.matchedString.startsWith('\\includefrom')) {
return resolveFile([match.directory, path.join(path.dirname(rootFile), match.directory)], match.path)
return resolveFile([matchedDir, path.join(path.dirname(rootFile), matchedDir)], matchedPath)
} else {
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], match.path)
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], matchedPath)
}
}
return
}
}

export function sanitizeInputFilePath(filePath: string): string {
if (filePath.startsWith('"') && filePath.endsWith('"')) {
return filePath.slice(1, -1)
}
return filePath
}

0 comments on commit ddec989

Please sign in to comment.