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

Reference issue #173

Open
viethung0823 opened this issue May 30, 2024 · 0 comments
Open

Reference issue #173

viethung0823 opened this issue May 30, 2024 · 0 comments

Comments

@viethung0823
Copy link

function updateFrontMatter(template, field, value, isArray = false) {
	const matterValue = matter(template);
	if (isArray) {
		matterValue.data[field] = [...matterValue.data[field], ...value.split(",").map((v) => v.trim())];
	} else {
		matterValue.data[field] = value;
	}
	const updatedTemplate = matter.stringify(matterValue.content, matterValue.data);
	return updatedTemplate;
}
// the templates are the same at first
let firstTemplate = '---\nfield: ["original value"]\n---\ncontent';
let secondTemplate = '---\nfield: ["original value"]\n---\ncontent';
for (let i = 0; i < 2; i++) {
	firstTemplate = updateFrontMatter(firstTemplate, "field", `new value for firstTemplate ${i}`, true);
	console.log("firstTemplate \n", firstTemplate);
}
secondTemplate = updateFrontMatter(secondTemplate, "field", "new value for secondTemplate", true);
console.log("secondTemplate", secondTemplate);

LOG:

firstTemplate 
 ---
field:
  - original value
  - new value for firstTemplate 0
---
content

firstTemplate 
 ---
field:
  - original value
  - new value for firstTemplate 0
  - new value for firstTemplate 1
---
content

secondTemplate ---
field:
  - original value
  - new value for firstTemplate 0
  - new value for secondTemplate
---
content

Can anyone explain for me why at 2nd template value it has value from the 1st one? The line matterValue from const matterValue = matter(template); always return a new ref right?

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