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

Files with "\r\n" as line endings, causing different update issues #701

Open
AndreasSachs opened this issue Jul 12, 2024 · 0 comments
Open

Comments

@AndreasSachs
Copy link

I'm trying to update a field through the manageFieldAtCursorCommand. The note was previously opened in source mode by clicking a link from another note. Positioning the cursor on a field that is defined in the corresponding field class results in "No field with definition at this position" message.
When I edit the opened note in the editor or switch to reading mode and back to source mode, I can update the fields with metadata menu without issues.
It seems to be related to newly opened notes only

Looking into build() and buildSections functions I noticed, that for notes where the update works the line const content = await this.plugin.app.vault.read(this.file); has "\n" as line endings. For notes where the update does not work the same line returns the content with "\r\n" line endings. As result the buildSections() function cannot recognize the frontmatter correctly as I also set the scope to be frontmatter only.

As a bad workaround:
I've changed the build() function to:

	async build() {
		const content = await this.plugin.app.vault.read(this.file);
		let line_ending = '\n';
		if (content.includes('\r\n')) {
			line_ending = '\r\n';
		}
		const lines = content.split(line_ending);
                ...

and buildSections()

	buildSections(content) {
		var _a, _b;
		const yamlLines = [];
		let inFrontmatter = false;
		let inStartingBlankLines = false;
		let frontmatterStart;
		let frontmatterEnd;
		let startsWithText = false;
		let previousLineIsCode = false;
		let line_ending = '\n';
		if (content.includes('\r\n')) {
			line_ending = '\r\n';
		}
		for (const [i, rawLine] of content.split(line_ending).entries()) {
			if (frontmatterEnd === void 0) {
			...

This might be related to other update issues that I have also experienced like:
#612

@AndreasSachs AndreasSachs changed the title app.vault.read(this.file) sometimes returns content with "\n" and sometimes with "\r\n", causing different update issues Files with "\r\n" as line endings, causing different update issues Jul 12, 2024
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

1 participant