posthtml / posthtml-img-autosize

Auto setting the width and height of <img>
MIT License
28 stars 8 forks source link

feature_request(paths): support relative paths for any valid folder structure #26

Open Kristinita opened 2 years ago

Kristinita commented 2 years ago

1. Summary

It would be nice, if posthtml-img-autosize will support relative paths for any valid path structure.

2. MCVE

See this configuration on the KiraPosthtmlPaths2022 branch of my demo GitHub repository.

It would be nice, if posthtml-img-autosize will successfully work for this configuration.

  1. Folder structure:

    .travis.yml
    Gruntfile.coffee
    package.json
    \---output
        |   KiraRootFile.html
        |
        +---assets
        |       NeliaLeaves.jpg
        |
        \---KiraFolder
            |   KiraFolderFile.html
            |
            \---KiraSubfolder
                    KiraSubfolderFile.html
  2. KiraRootFile.html:

    <!doctype html>
    <html>
    
    <head>
        <title>posthtml-img-autosize relative paths problem</title>
    </head>
    
    <body>
        <img alt="Nelia leaves" src="assets/NeliaLeaves.jpg">
    </body>
    
    </html>
    
  3. KiraFolderFile.html:

    <!doctype html>
    <html>
    
    <head>
        <title>posthtml-img-autosize relative paths problem</title>
    </head>
    
    <body>
    -   <img alt="Nelia leaves" src="assets/NeliaLeaves.jpg">
    +   <img alt="Nelia leaves" src="../assets/NeliaLeaves.jpg">
    </body>
    
    </html>
    
  4. KiraSubfolderFile.html:

    <!doctype html>
    <html>
    
    <head>
        <title>posthtml-img-autosize relative paths problem</title>
    </head>
    
    <body>
    -   <img alt="Nelia leaves" src="assets/NeliaLeaves.jpg">
    +   <img alt="Nelia leaves" src="../../assets/NeliaLeaves.jpg">
    </body>
    
    </html>
    
  5. Any NeliaLeaves.jpg image

  6. Part of package.json:

    {
        "devDependencies": {
            "coffeescript": "^2.7.0",
            "grunt": "^1.5.3",
            "grunt-posthtml": "^0.5.2",
            "posthtml-img-autosize": "^0.1.6"
        }
    }
  7. Gruntfile.coffee:

    module.exports = (grunt) ->
    
        grunt.loadNpmTasks 'grunt-posthtml'
    
        grunt.initConfig
    
            posthtml:
                options:
                    use: [
                        require('posthtml-img-autosize')(processEmptySize: true)
                    ]
                target:
                    files: [
                        expand: true
                        cwd: './output'
                        src: '**/*.html'
                        dest: '.'
                        ext: '.outputgrunt.html'
                    ]
    
  8. Part of .travis.yml:

    install:
    - npm install -g grunt-cli
    - npm install
    
    script:
    - grunt posthtml --verbose --stack
    

3. Behavior

3.1. Desired

Successful adding width and height for <img> in all three HTML files above like here:

<!doctype html>
<html>

<head>
    <title>Kira Goddess!</title>
</head>

<body>
-   <img alt="Nelia leaves" src="assets/NeliaLeaves.jpg">
+   <img alt="Nelia leaves" src="assets/NeliaLeaves.jpg" width="620" height="1080">
</body>

</html>

3.2. Current

Travis build:

Running tasks: posthtml

Running "posthtml" task

Running "posthtml:target" (posthtml) task

Verifying property posthtml.target exists in config…OK

Files: output/KiraRootFile.html -> KiraRootFile.outputgrunt.html

Files: output/KiraFolder/KiraFolderFile.html -> KiraFolder/KiraFolderFile.outputgrunt.html

Files: output/KiraFolder/KiraSubfolder/KiraSubfolderFile.html -> KiraFolder/KiraSubfolder/KiraSubfolderFile.outputgrunt.html

Options: use=[null], singleTags=[], closingSingleTag="default", skipParse=null, sync=null

Reading output/KiraRootFile.html…OK

Reading output/KiraFolder/KiraFolderFile.html…OK

Reading output/KiraFolder/KiraSubfolder/KiraSubfolderFile.html…OK

Warning: ENOENT: no such file or directory, open '/home/travis/build/Kristinita/SashaGruntDebugging/assets/NeliaLeaves.jpg' Use --force to continue.

4. Not helped

4.1. posthtml-cli

I’m not getting the desired behavior, if I use posthtml-cli instead of grunt-posthtml. See 4.3 and 5.1 sections of my comment from previous issue.

4.2. “root” option

It doesn’t work for the path structure as in my MCVE. As @Scrum said in 2020:

At the moment, relative paths are supported, but it should be borne in mind that they are resolved relative to the running process + root options and not relative to the html file

5. Environment

  1. Operating system:

    1. Local — Microsoft Windows [Version 10.0.19041.1415]
    2. Travis CI — Ubuntu 22.04 LTS Jammy Jellyfish
  2. Node.js 18.8.0

  3. grunt-cli v1.3.2

  4. posthtml-cli 0.8.0

Thanks.