edit anywhere | write gist files | git file tree | Search

This code updates a GitHub Gist with modified web page content, likely scraped from a URL, and tracks changes made to the HTML.

Run example

npm run import -- "save git"

save git

var importer = require('../Core')
var updateGist = importer.import("write gist files")

async function gitSave(url, data, gist) {
    if(!gist) return {}
    if(typeof url == 'string') {
        url = new URL(url);
    }
    //console.log(url)
    var host = url.hostname.replace(/[^a-z0-9_-]/ig, '_')
    var file = url.pathname.replace(/[^a-z0-9_-]/ig, '_')
    
    // check if the file exists
    const saved = (await getGist(gist)).files
    var acl
    if(typeof saved[file] === 'undefined') {
        var files = await loadScraped(url)
        var changes = {}
        changes[file] = {content: files[file]}
        if(files[file]) {
            await updateGist(gist, files)
        }
    }
    
    // add changes to gist
    var changes = {}
    changes[file] = {content: data}
    await updateGist(gist, files)
    
    // diff the HTML for changes
    const changes = []
    if(saved && saved[host + '-acl.json']) {
        var acl = files[host + '-acl.json'] || []
        if(typeof acl === 'string') {
            acl = [acl]
        }
        var bodyPrevious = selectDom('//body', saved[file])
        var bodyNew = selectDom('//body', data)
        acl.forEach(i => {
            var before = selectDom([i], bodyPrevious)
            var after = selectDom([i], bodyNew)
            
        })
    }
    
    // save the changes to spreadsheet
    
}

module.exports = gitSave

What the code could have been:

/**
 * Saves data to a gist on GitHub.
 * 
 * @param {string} url - The URL of the gist.
 * @param {object} data - The data to save to the gist.
 * @param {string} gist - The ID of the gist to update.
 * @returns {Promise} - The updated gist.
 */
const importer = require('../Core')
const { writeGistFiles } = importer.import();
const { getGist, loadScraped } = require('../Core');

async function gitSave(url, data, gist) {
    if (!gist) return {};

    // Normalize the URL
    const normalizedUrl = new URL(url);
    const host = normalizedUrl.hostname.replace(/[^a-z0-9_-]/ig, '_');
    const file = normalizedUrl.pathname.replace(/[^a-z0-9_-]/ig, '_');

    // Check if the file exists in the gist
    const gistFiles = await getGist(gist);
    if (!gistFiles.files[file]) {
        // Load the scraped data from the URL
        const scrapedData = await loadScraped(normalizedUrl);
        // Update the gist with the scraped data
        await writeGistFiles(gist, scrapedData);
    }

    // Update the gist with the new data
    await writeGistFiles(gist, { [file]: data });

    // Diff the HTML for changes
    const acl = gistFiles.files[`${host}-acl.json`];
    if (acl) {
        // Parse the ACL as an array of selectors
        const aclSelectors = typeof acl ==='string'? [acl] : acl;
        // Select the previous and new body elements
        const bodyPrevious = selectDom('//body', gistFiles.files[file]);
        const bodyNew = selectDom('//body', data);
        // Compare the previous and new body elements
        aclSelectors.forEach(selector => {
            const previous = selectDom(selector, bodyPrevious);
            const newSelection = selectDom(selector, bodyNew);
            // TODO: Implement diff logic here
        });
    }
}

// TODO: Consider moving the diff logic to a separate function
// TODO: Add error handling for loading and updating the gist

module.exports = gitSave;

This code defines a function gitSave that updates a GitHub Gist with modified content, likely from a web page scraping process.

Here's a breakdown:

  1. Dependencies:

    • It imports a module updateGist responsible for updating the Gist content.
  2. gitSave Function:

    • Takes url, data (presumably the updated content), and gist (Gist information) as arguments.
    • Extracts the hostname and filename from the URL.
    • Checks if the file already exists in the Gist.
    • If the file doesn't exist, it loads content from the URL using loadScraped (not shown in the snippet), creates a new file entry, and updates the Gist.
    • Updates the Gist with the provided data for the specified file.
    • Performs a diff comparison between the previous and new HTML content, likely to track changes.
    • Finally, it aims to save the changes to a spreadsheet (implementation not shown).
  3. Export:

    • The gitSave function is exported as a module.

Overall Purpose:

This code snippet appears to be part of a system that scrapes web pages, modifies their content, and then saves the changes to a GitHub Gist. It also attempts to track and potentially report the changes made to the HTML content.