使用 JavaScript API 将自定义函数添加到触笔

add custom function to stylus with javascript api

本文关键字:添加 自定义函数 JavaScript API 使用      更新时间:2023-09-26

我想将charAt js函数添加到手写笔中,我尝试在手写笔文档中发布的示例,如下所示:

var stylus = require('./node_modules/stylus')
, nodes = stylus.nodes
, utils = stylus.utils
, fs = require('fs');
function charAt(string, index) {
  return string.charAt(index);
}

stylus(str)
  .set('filename', 'js-functions')
  .define('charAt', charAt)
  .render(function(err, css){
    if (err) throw err;
    console.log(css);
  });

我将所需路径更改为我的手写笔目录,test.styl里面有以下内容:

use('js-functions.js')

当我运行它时,会出现错误:

ReferenceError: test.styl:1
  > 1| use("js-functions.js")
    2| 
    3| 
    4| 
str is not defined

这里发生了什么?,我错过了什么?,对于刚接触节点和手写笔的人来说,文档不能很好地解释它。

创建一个文件"test.js",如下所示:

var plugin = function(){
    return function(style){
        style.define('charAt', function (string, index) {
        })
    }
}
module.exports = plugin;

手写笔代码:

use('test.js')