Giter Club home page Giter Club logo

Comments (2)

Maximvdw avatar Maximvdw commented on June 13, 2024

In order to demonstrate my intentions a bit clearer. This is the current post-processing script I created
to achieve the same result

Javascript (nodejs)

async function rewriteLanguagePaths(directory, defaultLanguage = 'en') {
    // Get index files in the directory
    const indexFiles = fs.readdirSync(directory)
        .filter(file => file.startsWith("index"));
    indexFiles.forEach(index => {
        // Language of the index file
        const language = /index-(.*?).html/g.exec(index)[1];
        const oldFilePath = path.join(directory, index);
        const newFilePath = path.join(directory, language, "index.html");
        // Create language directory
        fs.mkdirSync(path.join(directory, language));
        // Copy the file to /${language}/index.html
        fs.copyFileSync(oldFilePath, newFilePath);
        // URL rewriting in the new directory
        let contents = fs.readFileSync(newFilePath, { encoding: 'utf-8' });
        contents = contents.replace(/href="index-([a-z]{2}?).html"/g, 'href="$1/"');
        contents = contents.replace(/(href|src?)=\"((?!http).*?)\"/g, '$1="../$2"');
        contents = contents.replace(/.load\(\"((?!http).*?)\"/g, '.load\("../$1"');
        fs.writeFileSync(newFilePath, contents);

        if (defaultLanguage !== language) {
            // Remove index file if not the default language
            fs.rmSync(oldFilePath);
        } else {
            // Rename the index-*.html file to index.html
            const defaultIndex = path.join(directory, "index.html");
            fs.renameSync(oldFilePath, defaultIndex); 
            let contents = fs.readFileSync(defaultIndex, { encoding: 'utf-8' });
            contents = contents.replace(/href="index-([a-z]{2}?).html"/g, 'href="$1/"');
            fs.writeFileSync(defaultIndex, contents);
        }
    });
}  

This is a slightly more complex example that fixes the webvowl iframe issue in a section (not an ideal solution)

async function rewriteLanguagePaths(directory, defaultLanguage = 'en') {
    // Replacement patterns
    const relativePathPattern = /(href|src?)=\"((?!http|#).*?)\"/g;
    const relativePathSourcePattern = /.load\(\"((?!http).*?)\"/g;
    const languagePathPattern = /href="index-([a-z]{2}?).html"/g;

    // Get section files in the sections directory
    const sectionFiles = fs.readdirSync(path.join(directory, "sections"));
    // Create a mapping for section files with relative URIs
    const sectionMapping = sectionFiles.map(file => {
        const filePath = path.join(directory, "sections", file);
        let contents = fs.readFileSync(filePath, { encoding: 'utf-8' });
        if (relativePathPattern.exec(contents)) {
            contents = contents.replace(relativePathPattern, '$1="../$2"');
            const newFile = file.replace(".html", "_.html");
            fs.writeFileSync(path.join(directory, "sections", newFile), contents);
            return { [file]: newFile }
        } else {
            return { [file]: file };
        }
    }).reduce((a, b) => ({...a, ...b}), {});

    // Get index files in the directory
    const indexFiles = fs.readdirSync(directory)
        .filter(file => file.startsWith("index"));
    indexFiles.forEach(index => {
        // Language of the index file
        const language = /index-(.*?).html/g.exec(index)[1];
        const oldFilePath = path.join(directory, index);
        const newFilePath = path.join(directory, language, "index.html");
        // Create language directory
        fs.mkdirSync(path.join(directory, language));
        // Copy the file to /${language}/index.html
        fs.copyFileSync(oldFilePath, newFilePath);
        // URL rewriting in the new directory
        let contents = fs.readFileSync(newFilePath, { encoding: 'utf-8' });
        contents = contents.replace(languagePathPattern, 'href="$1/"');
        contents = contents.replace(relativePathPattern, '$1="../$2"');
        contents = contents.replace(relativePathSourcePattern, '.load\("../$1"');
        // Replace sections with relative href or src
        Object.keys(sectionMapping).forEach(sectionKey => {
            contents = contents.replace(sectionKey, sectionMapping[sectionKey]);
        });
        fs.writeFileSync(newFilePath, contents);

        if (defaultLanguage !== language) {
            // Remove index file if not the default language
            fs.rmSync(oldFilePath);
        } else {
            // Rename the index-*.html file to index.html
            const defaultIndex = path.join(directory, "index.html");
            fs.renameSync(oldFilePath, defaultIndex); 
            let contents = fs.readFileSync(defaultIndex, { encoding: 'utf-8' });
            contents = contents.replace(languagePathPattern, 'href="$1/"');
            fs.writeFileSync(defaultIndex, contents);
        }
    });
} 

from widoco.

dgarijo avatar dgarijo commented on June 13, 2024

This is a nice suggestion, as the whole hierarchy remains cleaner. I am thinking that this should become the default behavior, but it may cause some issues with existing tools. Probably I did not implement this due to convenience. It may be worth exploring.

from widoco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.