Coder Social home page Coder Social logo

Relative URLs to root of project about lume HOT 9 CLOSED

lumeland avatar lumeland commented on July 18, 2024
Relative URLs to root of project

from lume.

Comments (9)

oscarotero avatar oscarotero commented on July 18, 2024

this.ctx does not exist in Lume. Where did you see it?

Lume has a url filter to generate urls including the domain or only the path. For example, with nunjucks, you could do:

<img href="{{ 'assets/x.png' | url }}">

from lume.

fserb avatar fserb commented on July 18, 2024

url doesn't work for this case, because it assumes that it's being served on the root of the domain.

this.ctx comes from Nunjucks filters.

from lume.

oscarotero avatar oscarotero commented on July 18, 2024

url use the location configuration as base. If the location is, for example, https://mysite.com/subdirectory, the prefix /subdirectory is prepended to the returned path.

For example: <img src="{{ 'assets/x.png' | url }}"> returns <img src="/subdirectory/assets/x.png">.

Does it work for you?

from lume.

fserb avatar fserb commented on July 18, 2024

Not really, I'd still have to do one build for each location. Maybe a relative_path or relative_url filter?

But mostly, how do you feel about a preprocess plugin entry that happens before rendering?

from lume.

oscarotero avatar oscarotero commented on July 18, 2024

Ok, got it.
Processors are executed after rendering. I think it's good because doing so, they are independent of the template engine used.

There is a inline processor here to inline automatically all css, javascript, images, etc, by searching in the html code all elements with the inline attribute.

I think we can do something similar for your use case, a processor to search all elements using an absolute path in the href/src attributes and convert them to relative. In fact, I have done something like that in another ssg I've created some years ago

from lume.

fserb avatar fserb commented on July 18, 2024

Ahnnn. That could totally work. Good point.

I made a quick implementation of preprocess. But I'd defer to you if you think it's a good idea for this to exist at all.

from lume.

fserb avatar fserb commented on July 18, 2024

Also, for future reference, here are the 3 implementations of rootPath that I ended up trying:

with a tag, abusing Nunchukjs. Use with {{ rootPath() }}/image.png:

site.data("rootPath", function() {
  return this.ctx.url.split('/').filter(x => x).map(_ => '..').join('/') || '.';
});

with a processor doing string substitution. Use with @ROOT@/image.png:

site.process([".njk", ".md", ".html"], (page) => {
  const root = page.data.url.split('/').filter(x => x).map(_ => '..').join('/') || '.';
  page.content = page.content.replaceAll("@ROOT@", root);
});

With a preprocessor (not supported). Use with {{ rootPath }}/image.png:

site.preprocess([".njk", ".md", ".html"], (page) => {
  page.data.rootPath = page.data.url.split('/').filter(x => x).map(_ => '..').join('/') || '.';
});

from lume.

oscarotero avatar oscarotero commented on July 18, 2024

I've created a plugin to convert all urls to relative: https://github.com/lumeland/lume/blob/master/plugins/relative_urls.js

You only have to import to your _config.js file:

import relative from "https://raw.githubusercontent.com/lumeland/lume/master/plugins/relative_urls.js";

site.use(relative())

I have tested it and seems to work fine. Could you check it?

from lume.

fserb avatar fserb commented on July 18, 2024

It works perfectly. Thanks! :)

from lume.

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.