فهرست منبع

ob-js: Fix passing multiline variables

* lisp/ob-js.el (org-babel-js-var-to-js): Replace newline characters
  with "\n" in strings.

Let's say I have a multi-line string stored in an example block.

I want to store my CSV in an example block.

  ColA,ColB,ColC
  1,2,3
  4,5,6

I have a JavaScript function that accepts a string named 'csv' and passing in 'my-csv-data'.

  console.log(csv);

When I expand the source block I end up with:

var csv="ColA,ColB,ColC
  1,2,3
  4,5,6";
console.log(csv);

This will not execute correctly because JavaScript does not support newlines in strings.

What I want instead is:

  var csv="ColA,ColB,ColC\n  1,2,3\n  4,5,6";
  console.log(csv);

TINYCHANGE
Peter Moresi 10 سال پیش
والد
کامیت
aa65ac35aa
1فایلهای تغییر یافته به همراه1 افزوده شده و 1 حذف شده
  1. 1 1
      lisp/ob-js.el

+ 1 - 1
lisp/ob-js.el

@@ -113,7 +113,7 @@ Convert an elisp value into a string of js source code
 specifying a variable of the same value."
   (if (listp var)
       (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
-    (format "%S" var)))
+    (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
 
 (defun org-babel-prep-session:js (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."