替换手写笔中的功能

replacing function in stylus

本文关键字:功能 手写笔 替换      更新时间:2023-09-26

我有带有该代码的手写笔文件:

body
  background url('/templates/main/img/sprites/ico-soc.png') 100px 100px no-repeat

我需要函数(mixin)background它将从图像的引用中删除"/模板/主/"

body
      background url('img/sprites/ico-soc.png') 100px 100px no-repeat

请帮助我)我想也许我走错了方向,有一个简单的方法可以解决问题。 请注意,我需要该函数。不要为目录创建变量!


这就是我正在做的

我做了功能

background()
  background arguments

然后我尝试通过两种方式解决问题1)我试图找到一个替代功能。但失败了。然后我尝试使用文档的决定(函数替换)。但它不适合我的情况。http://learnboost.github.io/stylus/docs/bifs.html#add-propertyname-expr

2)我通过函数use(path)(http://learnboost.github.io/stylus/docs/bifs.html#usepath)在js上编写了我的函数。但是我遇到了一个问题,进入js函数的值不正确。有额外的空间。

风格

.风格

use("add.js")
background()
  background arguments
body
  background url('img/sprites/ico-soc.png') 100px 100px no-repeat

添加.js

    var plugin = function(){
    return function(style){
        style.define('add', function(a) {
            console.log(typeof a);
            console.log(a);
        });
    };
};
module.exports = plugin;

您可以使用 current-property 重新定义url函数并检查它是否从 background 调用。例如:

url(str)
  if current-property && current-property[0] == 'background'
    str = replace('/templates/main/', '', str)
  return unquote('url(' + str + ')')
body
  background: url('/templates/main/img/sprites/ico-soc.png') 100px 100px no-repeat

编译为:

body {
  background: url(img/sprites/ico-soc.png) 100px 100px no-repeat;
}