Search code examples
jekyll

How to use repo files outside of the root directory in _data and assets folders?


I am trying to create a GitHub Pages site for an existing repo which already contains all the files that would belong in the assets/images and _data folders, i.e. hundreds of images and .json files. After adding a MySite Jekyll directory, I want my repo to look like this:

.
├── Images/
│   ├── imageFolder1
│   ├── imageFolder2
│   └── etc...
├── Data/
│   ├── data1.json
│   ├── data2.json
│   └── etc...
└── MySite/
    ├── _data/
    │   ├── <shortcut to data1.json here>
    │   └── <shortcut to data2.json here>
    └── assets/
        ├── images/
        │   ├── <shortcut to imageFolder1 here>
        │   └── <shortcut to imageFolder2 here>
        ├── css
        └── js

How can I make my image and json files accessible to Jekyll without having to duplicate them?

I tried using a symbolic link, both directly to the file and to the parent directory, but neither worked, despite disabling safe mode in configuration settings.


Solution

  • I suggest use the Symbolic Links method it's can be an essay and no need to create a Jekyll Plugin or any other scripts

    Symbolic links allow you to create shortcuts to files or directories outside your site's root directory.

    how to apply:-

    1)Open your terminal or command prompt, navigate to your MySite directory, and create symbolic links

    Unix/Linux/macOS:

    ln -s ../../Images/imageFolder1 assets/images/imageFolder1
    ln -s ../../Images/imageFolder2 assets/images/imageFolder2
    ln -s ../../Data/data1.json _data/data1.json
    ln -s ../../Data/data2.json _data/data2.json
    

    For Windows:

    mklink /D assets\images\imageFolder1 ..\..\Images\imageFolder1
    mklink /D assets\images\imageFolder2 ..\..\Images\imageFolder2
    mklink _data\data1.json ..\..\Data\data1.json
    mklink _data\data2.json ..\..\Data\data2.json
    

    2)Disable Safe Mode: Ensure that Jekyll is not running in safe mode. Set safe: false in your _config.yml file.

    3)Run Jekyll: Start your Jekyll server. If there are issues, try running Jekyll with Jekyll serve --no-watch.