Skip to content

md img render part improvement of hexo-render-marked  #216

@ChanryLimmy

Description

@ChanryLimmy

Background

u know when we use other md editor write a post, the img with relative path is like this ![makabaka](postname/1.jpg), but here in hexo-render-marked i must use ![makabaka](1.jpg) if the config like this:

post_asset_folder: true
marked:
  prependRoot: true
  postAsset: true

it means there must be one side between md editor & hexo can't see the pic.

My solution

i opened node_modules -> hexo-render-marked -> lib -> render.js and change the image part (between the *** lines):

  // Prepend root to image path
  image(href, title, text) {
    const { hexo, options } = this;
    const { relative_link } = hexo.config;
    const { lazyload, prependRoot, postPath } = options;

    if (!/^(#|\/\/|http(s)?:)/.test(href) && !relative_link && prependRoot) {
      if (!href.startsWith('/') && !href.startsWith('\\') && postPath) {
        const PostAsset = hexo.model('PostAsset');
        // findById requires forward slash
        // ************************************************************
        const pp = require('path');
        const ppp = pp.join(postPath, '../');
        // const asset = PostAsset.findById(join(postPath, href.replace(/\\/g, '/')));
        const asset = PostAsset.findById(join(ppp, href.replace(/\\/g, '/')));
        // ************************************************************
        // asset.path is backward slash in Windows
        if (asset) href = asset.path.replace(/\\/g, '/');
      }
      href = url_for.call(hexo, href);
    }

    let out = `<img src="${encodeURL(href)}"`;
    if (text) out += ` alt="${text}"`;
    if (title) out += ` title="${title}"`;
    if (lazyload) out += ' loading="lazy"';

    out += '>';

    return out;
  }
}

it works. so the following is my suggests about this product experience

Feature Request

  • provide a better implement cuz im not good at this language
  • provide a config choice 4 this problem

tks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions