Home > OS >  Script markdown file to scan and test embedded HTTPS
Script markdown file to scan and test embedded HTTPS

Time:12-19

Newbie to .md/javascripting: I would like to write a script to read (process) a .md file and scan the file for HTTPS. Then, either test the HTTPS request for false / True results and log either to a file.

I'm thinking a javascript to read the file. then write the script locate the embedded https, test and log.

Does that sound feasible?

researched the .md and javascript commands. Seeking advice.

CodePudding user response:

Here's an example of how you could implement this:

const fs = require('fs');
const https = require('https');

// Read the contents of the file
const fileContents = fs.readFileSync('path/to/file.md', 'utf8');
// Use a regular expression to find all the HTTPS links in the file
const httpsLinks = fileContents.match(/https:\/\/[^\s] /g);
// Create an array to store the results
const results = [];
// Create a function to send an HTTPS request
function sendHttpsRequest(link) {
    return new Promise((resolve, reject) => {
        https
            .get(link, (res) => {
                // Resolve the promise with the status code if the request is successful
                resolve(res.statusCode);
            })
            .on('error', (error) => {
                // Reject the promise with the error if the request fails
                reject(error);
            });
    });
}
// Loop through all the HTTPS links and send an HTTPS request for each one
for (const link of httpsLinks) {
    try {
        // Send the HTTPS request and wait for the response
        const statusCode = await sendHttpsRequest(link);
        // Add the link and status code to the results array if the request was successful
        results.push({ link, statusCode });
    } catch (error) {
        // Add the link and error to the results array if the request failed
        results.push({ link, error });
    }
}
// Write the results to a file
fs.writeFileSync('results.txt', JSON.stringify(results));

This code will read the .md file, scan it for HTTPS links using a regular expression, send HTTPS requests to each link, and log the response status code or any errors that occurred during the request. It will write the results to a file once all HTTPS requests have completed.

CodePudding user response:

If you are new in the role of Software Developer I will give you few advices on "how to take up" this particular problem.

First of all try not to use solutions like @Mohamed Mrad posted. You will not get much knowledge if this won't be your own problem solving.

Try to think about the problem: "I would like to read .md file. And then search for any https URL inside. Than run this URL and get results. Than I would like to save them into a file.".

Than we could impose a few restrictions:

  • I would like to make this app a CLI command.
  • Likely in the future I would like to use it in a web application.
  • I want to log errors while trying to get response from URL. And save it in the same file.

Now we can start thinking about solution. Try cut the idea into pieces:

  1. Program gets a path to the .md file.
  2. I must check if a path is string and if it is not empty.
  3. If empty then I should return message about empty path - error.
  4. Having path we must load a file.
  5. If file does not exists return message about it - error.
  6. Having loaded file we have to read the file. But how to do it? Consider reading line by line or a whole file to find url. Read about available methods and find one fits for your case.
  7. Reading a file would be the best to find all URLs first.
  8. If there are no URLs return information about it.
  9. Iterate over found URLs.
  10. If URL return response you could try to save it to a file. But it could be huge. Think about what would you like to save? Maybe save just URL, response code (eg. 200 - success)? Save what you want.
  11. If URL returns unsuccessful response save it to (with response code like 404 - not found, 500 - internal server error).
  12. Save results to a file. Read how to do it in documentation.
  • Related