Foam: Saving a file generates a new link reference section every time

Created on 11 Jul 2020  Â·  9Comments  Â·  Source: foambubble/foam

Possibly related to #20

When I save a file that contains [[wiki-links]], the generated markdown references appear at the bottom of the file. If I save the file again (even if there are no changes, just pressing cmd+s), _another_ identical generated section appears below the first one. Here is a video.

If I > Save without formatting, no link references are generated.

Also, if I delete the actual [[wiki-links]] from the page and then save, this still happens (with references generated for the links that are no longer there). I have to quit and re-open VS Code to make it realize there are no links there anymore, at which point saving does not generate more references.

System Info

macOS 10.14.6
VS Code 1.47.0
Foam for VSCode 0.1.7
Markdown All in One 3.1.0
Markdown Links 0.6.0
Markdown Notes 0.0.11
Prettier 5.1.3

bug foam-vscode

Most helpful comment

Note, I've just discovered a new way of causing this same effect. Have Format on Save set to run Prettier, and your settings are "singleQuote": true.

This will "fix" the double quotes in the begin/end comment blocks and make the reference list regenerate every time.

Workaround for the time being is to set either of these settings in your .vscode/settings.json:

"prettier.singleQuote": false

Or:

"editor.formatOnSave": false

All 9 comments

Wow, that's broken! I'm on same OS, VS Code and Foam versions, and I'm not
seeing this issue.

The link syntax you're using, using double square brackets [label][file] is
not something that we officially support. The way you'd do it right now
would be either as wiki links [[file]], or as a standard markdown link
[label](file.md). Wiki links should generate reference definitions, and
the markdown link should not (since it's already markdown).

Could you test if the [[file]] syntax works?

Otherwise, let me try to land https://github.com/foambubble/foam/pull/83 today
and release a new version to see if that fixes the problem.

Cheers,
Jani

On Sat, Jul 11, 2020 at 8:45 PM Ian J Sikes notifications@github.com
wrote:

Possibly related to #20 https://github.com/foambubble/foam/issues/20

When I save a file that contains [[wiki-links]], the generated markdown
references appear at the bottom of the file. If I save the file again (even
if there are no changes, just pressing cmd+s), another identical
generated section appears below the first one. Here is a video
https://share.getcloudapp.com/Z4uY999w.

If I > Save without formatting, no link references are generated.

Also, if I delete the actual [[wiki-links]] from the page and then
save, this still happens (with references generated for the links that are
no longer there). I have to quit and re-open VS Code to make it realize
there are no links there anymore, at which point saving does not generate
more references.
System Info

macOS 10.14.6
VS Code 1.47.0
Foam for VSCode 0.1.7
Markdown All in One 3.1.0
Markdown Links 0.6.0
Markdown Notes 0.0.11
Prettier 5.1.3

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/foambubble/foam/issues/94, or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAJF53OHW3SDT2S6B6ELW4DR3C6M3ANCNFSM4OXOFMMQ
.

Ah woops I used a bad example file. The wiki link in the video is the one that says [[how-to-run-a-dungeon]] at the top. Here is a smaller example with _only_ wiki links.

I figured out the cause. I have the files.insertFinalNewline VS Code setting enabled to automatically add an empty newline to the end of files that don't have one because Git. So when I save the file, it seems like:

  1. VS Code adds a newline if there isn't one
  2. Foam checks if the last lines of the file are the generated references (I am assuming this is how it works, haven't looked at the code yet).
  3. Generate new reference if there isn't one at the end of the file.

But on subsequent saves, VS Code will add the newline _after_ the references, which seems to cause Foam to not find them and generate new ones.

I disabled the setting in my workspace settings and the problem is gone.

Glad you figured it out!

Foam checks if the last lines of the file are the generated references

This is not how it works, it should look from the bottom of the file and find the last instance.

We're working on pretty broad rewrite of how we generate markdown links to make the entire feature more robust and less intrusive, so I'll make sure we'll test for trailing newlines.

Adding some extra info on this...
Foam regenerates the link references on the file when you have the file opened and save it, either by using a hotkey or going to the File menu and clicking on the Save option.

_However_, if you don't have the file open and have changes in it (and maybe in other files) and use the hotkey or 'Save As' option. It saves the file but doesn't trigger the link regeneration process.

That's what I've used as a workaround and to sidestep the 'having to close and reopen VS Code' issue.

@juanfrank77 hah, that's clever! Let's hope we can get this fixed properly in the coming week so you can stop having to hack around this :)

Note, I've just discovered a new way of causing this same effect. Have Format on Save set to run Prettier, and your settings are "singleQuote": true.

This will "fix" the double quotes in the begin/end comment blocks and make the reference list regenerate every time.

Workaround for the time being is to set either of these settings in your .vscode/settings.json:

"prettier.singleQuote": false

Or:

"editor.formatOnSave": false

I noticed this as well when I set up prettier to format line lengths. Could there be an option to wrap the autogenerated link references like so (though, in some generic way)?

<!-- prettier-ignore-start -->
[//begin]: # "Autogenerated link references for markdown compatibility"
[inbox]: ../inbox "Inbox"
[//end]: # "Autogenerated link references"
<!-- prettier-ignore-end -->

Adding "prettier.singleQuote": false to the template would be helpful in the mean time!

this is a great idea @mykter - I have made the change in the template https://github.com/foambubble/foam-template/commit/991970ff2c861b1af53812f2ccd668e42fda0964

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sahil48 picture sahil48  Â·  3Comments

amorriscode picture amorriscode  Â·  6Comments

jsjoeio picture jsjoeio  Â·  5Comments

fmagin picture fmagin  Â·  7Comments

doodlewhale picture doodlewhale  Â·  4Comments