export function findDocumentLinks()

in src/services/htmlLinks.ts [83:142]


export function findDocumentLinks(document: TextDocument, documentContext: DocumentContext): DocumentLink[] {
	const newLinks: DocumentLink[] = [];

	const scanner = createScanner(document.getText(), 0);
	let token = scanner.scan();
	let lastAttributeName: string | undefined = undefined;
	let afterBase = false;
	let base: string | undefined = void 0;
	const idLocations: { [id: string]: number | undefined } = {};

	while (token !== TokenType.EOS) {
		switch (token) {
			case TokenType.StartTag:
				if (!base) {
					const tagName = scanner.getTokenText().toLowerCase();
					afterBase = tagName === 'base';
				}
				break;
			case TokenType.AttributeName:
				lastAttributeName = scanner.getTokenText().toLowerCase();
				break;
			case TokenType.AttributeValue:
				if (lastAttributeName === 'src' || lastAttributeName === 'href') {
					const attributeValue = scanner.getTokenText();
					if (!afterBase) { // don't highlight the base link itself
						const link = createLink(document, documentContext, attributeValue, scanner.getTokenOffset(), scanner.getTokenEnd(), base);
						if (link) {
							newLinks.push(link);
						}
					}
					if (afterBase && typeof base === 'undefined') {
						base = normalizeRef(attributeValue);
						if (base && documentContext) {
							base = documentContext.resolveReference(base, document.uri);
						}
					}
					afterBase = false;
					lastAttributeName = undefined;
				} else if (lastAttributeName === 'id') {
					const id = normalizeRef(scanner.getTokenText());
					idLocations[id] = scanner.getTokenOffset();
				}
				break;
		}
		token = scanner.scan();
	}
	// change local links with ids to actual positions
	for (const link of newLinks) {
		const localWithHash = document.uri + '#';
		if (link.target && strings.startsWith(link.target, localWithHash)) {
			const target = link.target.substr(localWithHash.length);
			const offset = idLocations[target];
			if (offset !== undefined) {
				const pos = document.positionAt(offset);
				link.target = `${localWithHash}${pos.line + 1},${pos.character + 1}`;
			}
		}
	}
	return newLinks;
}