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

Preview Templates not working #777

Open
cunka opened this issue Aug 12, 2023 · 7 comments
Open

Preview Templates not working #777

cunka opened this issue Aug 12, 2023 · 7 comments
Labels

Comments

@cunka
Copy link

cunka commented Aug 12, 2023

Trying to modify the VSCode Asciidoctor "Preview" through the use of a Nunjuck Template.

There is a Setting in VScode called Asciidoc > Preview:Templates that says if this folder is left empty then templates will be discovered relative to the asciidoc file.

So if I have the nunjucks file and the asciidoc file together it should work.
It doesnt.

If I set up a folder to place the templates and add that path to the Setting in VScode *Asciidoc > Preview:Templates, it still doesnt work.

I have folder in my project called templates.
My setting in VSCode then for *Asciidoc > Preview:Templates I tried:

templates
//templates
.//templates

Nunjucks has been installed in VScode terminal using $ npm i nunjucks

Files are:
paragraph.njk

<p class="paragraph">Brian Did This : {{ node.getContent() | safe }}</p>

test.adoc

= TEST DOCUMENT

Paragraphs don't require any special markup in AsciiDoc.
A paragraph is just one or more lines of consecutive text.

I expected the nunjucks paragraph template to be found and modify the VSCode Asciidoctor "Preview" where a paragraph was used.

@cunka cunka added the 🐞 bug label Aug 12, 2023
@ggrossetie
Copy link
Member

Only an absolute path seems to work.
As far as I know, nunjucks won't be available in the VS Code context so only plain JavaScript:

paragraph.js

module.exports = ({ node }) => `<p class="paragraph">Brian did this: ${node.getContent()}</p>`

Settings

C:\path\to\templates

@ggrossetie
Copy link
Member

In short:

The setting description is wrong:

List of local paths to custom templates to use from the preview. Relative paths are interpreted relative to the folder open in the Explorer. If there is no open folder, they are interpreted relative to the location of the AsciiDoc file. All \ need to be written as \\.

I don't think "All \ need to be written as \\." is true.
We should recommend to use forward slash since it supported on Windows and VS Code is using URI.
We should append the workspace directory and the location of the AsciiDoc file if the path is relative.
We should disable the template cache (using template_cache) otherwise the user needs to restart the VS Code extension when a changes is made on a template file.

We should mention that only plain JavaScript is supported.
It's possible to support additional template engine but we will need to include them in the VS Code extension bundle (thus increasing the size of the bundle)

@cunka
Copy link
Author

cunka commented Aug 13, 2023

Thanks I got this to work with your help. (Javascript "paragraph" you supplied)
Out of the box in VSCode supporting Javascript only is fine.

There is a few caveats we may need to document with all this though..

Firstly, in Settings in VSCode, there is the "User" and "Workspace" settings.
I was modifying the User settings and it wasn't working. Only works with the "Workspace" settings.

As you mentioned, doing the \\ doesnt work, it has to be \.
The reason is the settings.json file makes the paths \\. If you make it \\ the json file ends up putting in \\\\.

@ggrossetie
Copy link
Member

As you mentioned, doing the \ doesnt work, it has to be .
The reason is the settings.json file makes the paths \. If you make it \ the json file ends up putting in \\.

\ character must be escaped in a JSON file (i.e., general rule). I think we should recommend to use forward slash / (instead of stating that \ must be escaped if the user is editing the settings.json directly)

Firstly, in Settings in VSCode, there is the "User" and "Workspace" settings.
I was modifying the User settings and it wasn't working. Only works with the "Workspace" settings.

I don't think this is intended. I will have a look.

@lask79
Copy link
Contributor

lask79 commented Feb 3, 2024

I also can see that only absolute paths are working. Isn't there a way to implement a simple way that the configured relative paths can be configured to use ${workspaceFolder}/templates or even better if it can be defined that the vscode extension automatically finds the templates under .asciidoctor/templates?

It is really annoying to manually set the templates path for each developer after checking out the repository and I think that could be easily improved.

@ggrossetie
Copy link
Member

Isn't there a way to implement a simple way that the configured relative paths can be configured to use ${workspaceFolder}/templates

We could support interpolation (i.e., replace ${workspaceFolder} by the actual workspace folder path). It might be better than trying to resolve the relative path since it's unclear if the path is relative to the workspace folder (root) or to the AsciiDoc file. Maybe relative paths should be resolved relative to the AsciiDoc file and if you want to resolve it relative to the workspace folder then you need to use ${workspaceFolder} ?

Alternatively, we could use dot (.), tilde (~), or tilde plus (~+) similarly to what https://gitlab.com/antora/expand-path-helper#user-content-usage does.

  • dot (.) relative to AsciiDoc file
  • tilde (~) relative to $HOME
  • tilde plus (~+) relative to workspace folder

@mojavelinux thoughts?

@lask79
Copy link
Contributor

lask79 commented Feb 3, 2024

Providing a convention like mentioned here #843, might even be the easier solution. When already starting with conventions for extensions, why not doing the same for templates?

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

No branches or pull requests

3 participants