mixu / markdown-styles

Markdown to static HTML generator and multiple CSS themes for Markdown
1.85k stars 251 forks source link

Asset helper produces backslashes in Windows #33

Open gene-pavlovsky opened 8 years ago

gene-pavlovsky commented 8 years ago

This line in my layout template: <link href="{{asset 'css/bootstrap.min.css'}}" rel="stylesheet"> Becomes this when generate-md is used on Windows: <link href="..\assets/css/bootstrap.min.css" rel="stylesheet">

Shouldn't URLs always use forward slash? Culprit seems to be lib/set-output-path.js

Mevrael commented 8 years ago

+1, same on Windows 10 for me. In any OS URLs should always have only forward slash "/". Anyway I don't see any advantages of using custom functions when you always can type path with leading slash "/assets/css/custom.css"

gene-pavlovsky commented 8 years ago

@Mevrael relative paths can be useful when deploying files not in web server's root. E.g. I'm working on http://www.example.com and deploying everything in it's root, but on my work computer I'm using a local web server and since I work on more than one website, my files reside are on http://localhost/www.example.com, using relative paths it works same on both my local dev server and on the production server.

Workaround - using a custom layout, added this helpers/asset.js:

var handlebars = require('handlebars');

module.exports = function(context, options) {
  var output = '';
  // data.root is the original item (e.g. the value from the stream)
  // it will have an assetsRelative path from the setOutputPath function
  output += options.data.root.assetsRelative + '/';
  // Get rid of any leading slash on the context
  context = context.replace(/^\//, '');
  output += context;

  return new handlebars.SafeString(output.replace(/\\/g, '/'));
};

The output.replace(...) in the last line changes all backslashes to forward slashes.