function generateLinkReport()

in extension.ts [83:139]


function generateLinkReport() {
    // Get the current document
    let document = window.activeTextEditor.document;
    // Create an output channel for displaying broken links
    let outputChannel = window.createOutputChannel("Checked links");
    // Show the output channel in column three
    outputChannel.show(ViewColumn.Three);
    
    // Get all Markdown style lnks in the document
    getLinks(document).then((links) => {
        // Loop over the links
        links.forEach(link => {
            // Get the line number, because internally it's 0 based, but not in display
            let lineNumber = link.lineText.lineNumber + 1;
            
            // Is it an HTTPS link or a relative link?
            if(isHttpLink(link.address)) {
                // And check if they are broken or not.
                brokenLink(link.address, {allowRedirects: true}).then((answer) => {
                    // Also check for country code
                    if(hasCountryCode(link.address)) {
                        outputChannel.appendLine(`Warning: ${link.address} on line ${lineNumber} contains a country code.`);
                    }
                    // Log to the outputChannel
                    if(answer) {
                        outputChannel.appendLine(`Error: ${link.address} on line ${lineNumber} is unreachable.`);
                    } else {
                        outputChannel.appendLine(`Info: ${link.address} on line ${lineNumber}.`);
                    }
                });
            } else {
                if(isFtpLink(link.address)) {
                    // We don't do anything with FTP
                    outputChannel.appendLine(`Info: ${link.address} on line ${lineNumber} is an FTP link.`);
                } else {
                    // Must be a relative path, but might not be, so try it...
                    try {
                        // Find the directory from the path to the current document
                        let currentWorkingDirectory = path.dirname(document.fileName);
                        // Use that to resolve the full path from the relative link address
                        // The `.split('#')[0]` at the end is so that relative links that also reference an anchor in the document will work with file checking.
                        let fullPath = path.resolve(currentWorkingDirectory, link.address).split('#')[0];
                        // Check if the file exists and log appropriately
                        if(fs.existsSync(fullPath)) {
                            outputChannel.appendLine(`Info: ${link.address} on line ${lineNumber}.`);
                        } else {
                            outputChannel.appendLine(`Error: ${link.address} on line ${lineNumber} does not exist.`);
                        }
                    } catch (error) {
                        // If there's an error, log the link
                        outputChannel.appendLine(`Error: ${link.address} on line ${lineNumber} is not an HTTP/s or relative link.`);
                    }
                }
            }
        });
    });
}