vscode-zoterolens/src/reference.ts

41 lines
1.4 KiB
TypeScript

import {
CodeLensProvider,
TextDocument,
CodeLens,
Range,
Command
} from "vscode";
export interface Reference {
document: TextDocument;
citekey: string;
pagenr: number | null;
range: Range;
}
// TODO match citations in brackets [@citation] inline @citatinon,
// but also page number in [see @citation p.23] and
// inlince @citation [p.23] (so brackets after inline)
// TODO possibly use https://github.com/martinring/markdown-it-citations/blob/ba82a511de047a2438b4ac060c4c71b5a5c82da9/src/index.ts#L43
export function findReferences(document: TextDocument): Reference[] {
const matches: Reference[] = [];
for (let lineNr = 0; lineNr < document.lineCount; lineNr++) {
const line = document.lineAt(lineNr);
let match: RegExpExecArray | null;
let regex = /(?<=@)([\w\.]+)[, ]*\[?(?:[p]{0,2}\.)?(\d+)?(?:-+\d+)?\]?/g;
regex.lastIndex = 0;
const text = line.text;//.substring(0, 1000);
while ((match = regex.exec(text))) {
const result = {
document: document,
citekey: match[1],
pagenr: match[2] ? parseInt(match[2]) : null,
range: new Range(lineNr, match.index, lineNr, match.index + match[0].length)
} as Reference;
// if (result) {
matches.push(result);
// }
}
}
return matches;
}