将所有根目录相对URL替换为文档相对URL的Gulp任务

px9o7tmv  于 2022-12-08  发布在  Gulp
关注(0)|答案(2)|浏览(186)

There are 4 distinct URL types;

  1. Absolutehttp://www.example.com/images/icons.png
  2. Document-Relative../images/icons.png
  3. Root-Relative/images/icons.png
  4. Protocol-Relative//www.example.com/images/icons.png
    I have a large static file site (html, css, js) which is built with Jigsaw . This framework takes PHP templates and compiles them into static HTML. I am also using Gulp tasks to compile assets (sass, js..etc).
    Using Jigsaw's build process I can either have the site built with full Absolute paths/urls ( http://example.com/path-to/page ) or Root-Relative ( /path-to/page ).
    This is great but now the client wants the site to use Document-Relative as they are now hosting the site in a subdirectory with a URL pointing to that subdirectory.
    E.g. http://example.com would point to http://xx.server.com/hosted-files/directory
    My issue is that Jigsaw doesn't allow for Document-Relative URLs. Is there a gulp/node script I can use to convert all references (image sources, links, css paths..etc)? Or is there another solution (e.g. using .htacccess)?

TLDR;

I need to replace any Absolute or Root-Relative references in multiple HTML files and directories with Document-Relative paths and URLs. Or is there another solution (e.g. using .htacccess)?

gz5pxeao

gz5pxeao1#

我已经设法解决了我自己的问题,我觉得这是一个“黑客”修复。
我基本上已经创建了一个自定义的gulp插件,用文档相对路径替换URL/路径等。

gulpfile.js-relative-urls任务在所有其他任务完成后运行。

const relative = require('./tasks/document-relative');
gulp.task('relative-urls', function() {
    return gulp.src('build/**/*.html')
        .pipe( relative({
            directory: 'build',
            url: 'http://localhost:8000',
        }) )
        .pipe( gulp.dest('build') );
});

./tasks/文档相关.js-插件

'use strict';

const fs            = require('fs');
const PluginError   = require('plugin-error');
const through       = require('through2');

const PLUGIN_NAME   = 'document-relative';

let count = 0;

module.exports = function(options) {

    // Remove slashes from beginning and end of string
    const strip_slashes = (string) => {
        return string ? string.replace(/^\/|\/$/g, '') : null;
    }

    // Users options object
    options = options || {};

    // Cleanup options
    const base_dir  = strip_slashes(options.directory);
    const url       = strip_slashes(options.url) + '/';

    return through({
        objectMode: true,
        writable: true,
        readable: true
    },
    function(file, enc, callback) {
        count++;

        // Check for null file
        if (file.isNull()) {
            return callback(null, file);
        }

        if (file.isStream()) {
            this.emit('error', new PluginError(PLUGIN_NAME, 'Stream not supported!'));
            return callback(null, file);
        }

        if (file.isBuffer()) {

            // Get contents of this file
            let html        = file.contents.toString(enc);

            // This files full path (/home/usr/project/build/page/example/index.html)
            const path      = file.path;

            // Path task was run from (/home/usr/project/)
            const cwd       = file.cwd+( base_dir ? '/'+base_dir : '' );

            // Project specific path (/page/example/index.html)
            const relative  = path.replace(cwd, '');

            // Get array of directories ['page', 'example', 'index.html']
            let paths       = strip_slashes(relative).split('/');

            // Remove last item ['page', 'example']
            paths.pop();

            // Add ../ for nth number of paths in array
            let rel_path    = paths.length === 0 ? '' : ('../'.repeat(paths.length));

            // Replace dom attributes (e.g. href="/page/example")
            html = html.replace( /(?:(?!="\/\/)="\/)/g, '="'+rel_path );

            // Replace inline background (e.g. background: url('/image/something.jpg'))
            html = html.replace( /url\(\'\//g, 'url(\''+rel_path );
            html = html.replace( /url\('\//g, 'url(''+rel_path );

            // If user defined URL, match and remove
            if (url && url.length) {
                html = html.replace( new RegExp(url, 'g'), rel_path );
            }

            // Overwrite file
            fs.writeFileSync(file.path, html, {
                encoding: enc,
                flag:'w'
            });

            return callback();
        }
    });
};

这基本上打开了我的build文件夹中的所有.html文件,计算每个文件的路径深度(/folder1/folder2/index.html),并将urlhttp://localhost:8000)的任何示例替换为../,重复计算出的路径数。

jbose2ul

jbose2ul2#

节点具有path.relative。

    • 阅读Levi Coles自己的answer,了解urldirectory的来源。*
const path = require("path");

// create a regular expression from your url property.
const domain_expression = new RegExp(url);

// Once you have an offending 'href' you can do this.
// - Here 'href' is Absolute, but this same code would work
//   with Root-Relative paths too.
const href = "http://localhost:8000/images/icons.png";
const file_index = href.lastIndexOf("/") + 1;
const file_component = href.substring(file_index);
const root_relative = href.replace(domain_expression, "");
const relative_href = path.relative(`${directory}/${root_relative}`, directory);
const _href = relative_href + file_component;
// _href = ../../icons.png

相关问题