Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing JSON object to ReLaXed: wiki needs to be updated! #175

Open
ghost opened this issue Mar 2, 2020 · 5 comments
Open

Passing JSON object to ReLaXed: wiki needs to be updated! #175

ghost opened this issue Mar 2, 2020 · 5 comments

Comments

@ghost
Copy link

ghost commented Mar 2, 2020

There is a problem with passing variables to ReLaXed with the shown example. I propose a small change on the wiki. We could also discuss better solutions. 😄

Note: Passing a JSON file works fine.

Steps

Tested on Windows and Linux.

1.  Install ReLaXed.

2.  Create `mydoc.pug` with the following content:

p Name - #{name}

3.  Execute the current example from the wiki:

relaxed mydoc.pug --build-once --locals {"name": "Marc"}

2 Problems:

1st problem

We would get an error like this:

SyntaxError: Unexpected number in JSON at position 1
    at JSON.parse (<anonymous>)
    at parseJson (C:\Users\user\AppData\Roaming\npm\node_modules\relaxedjs\src\parseLocals.js:22:17)
    at parseLocals (C:\Users\user\AppData\Roaming\npm\node_modules\relaxedjs\src\parseLocals.js:48:10)
    at Object.<anonymous> (C:\Users\user\AppData\Roaming\npm\node_modules\relaxedjs\src\index.js:84:14)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
ReLaXed error: Could not parse locals JSON, see error above.
Launching ReLaXed...

Processing \mydoc.pug...
... HTML generated in 0.1s
... Document loaded in 1.0s
... Network idled in 0.2s
... PDF written in 0.3s (27.2 KB)
... Done in 1.62s

Because of it, no data is passed to our file.

2nd problem

A file named Marc} is created instead of mydoc.pdf. This is a simple syntax fix, see below.

Fixes

First, simply add quotes around the JSON object to get the right output file name. Now the reason behind the JSON parsing error is how the data is given through Commander.js.

With our example, it looks like this: {name:Marc}

The latter is not a valid JSON object (missing quotes on keys) and would be of wrong type if the value was a string, which is the case here. The input style is also the case for other CLI tools like Caporal.js and Yargs.js since they are also subject to the Strong Quoting's principle.

The solution proposed is to simply to escape quotes. This is how the JSON object has to be passed:

"{\"name\":\"Marc\"}"

So on the wiki, the command should be:

relaxed mydoc.pug --build-once --locals "{\"name\":\"Marc\"}"

Trick

A neat trick to obtain the result is to stringify twice the JSON object before passing it as a parameter to ReLaXed:

JSON.stringify(JSON.stringify(data)))
@Zulko
Copy link
Member

Zulko commented Mar 2, 2020

Hey thanks for this, can you add this to the wiki? (not sure if you need us to give you some kind of write access or not)

@ghost
Copy link
Author

ghost commented Mar 2, 2020

Yeah I think I need permission to do this. Might be restricted to collaborators only. Otherwise you can simply edit it. ☺️

@mike-gee
Copy link

Thank you for sharing! Should add to wiki.

@DanielRuf
Copy link
Contributor

Anyone can edit the wiki so please feel free to add the missing information.

@mike-gee
Copy link

Hi @DanielRuf I don't believe anybody can edit the wiki.

As @LanguageAgnostic notes, it is restricted to collaborators only.

Let me know if this is a mistake and I would be happy to correct the wiki! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants