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

When publishing a draft, all the front-matter configuration is overwritten to defaults #5155

Closed
5 tasks done
neverbot opened this issue Feb 3, 2023 · 8 comments · Fixed by #5472
Closed
5 tasks done
Labels
bug Something isn't working

Comments

@neverbot
Copy link

neverbot commented Feb 3, 2023

Check List

Please check followings before submitting a new issue.

Expected behavior

When publishing a draft, if the draft have some front-matter configuration specified, the published post should keep it. Or at least it will make more sense to me. Maybe not the date, I guess.

Actual behavior

When publishing a draft, all the front-matter post configuration is overwritten to defaults. i.e. tags/categories are removed and only the default ones are set.

How to reproduce?

$ npx hexo new draft "Test"
INFO  Validating config
INFO  Created: ~/something/something/source/_drafts/test.md

$ ls -lah source/_drafts/test*
-rw-r--r--   1 something  something    72B Feb  3 12:47 source/_drafts/test.md

source/_drafts/test:
total 0
drwxr-xr-x   2 something  something    64B Feb  3 12:47 .
drwxr-xr-x  11 something  something   352B Feb  3 12:47 ..

The created draft should have the defaults, in my case:

---
title: Test
tags:
  - blog
author: something
---

Edit it, modifying tags:

---
title: Test
tags:
  - a
  - b
author: something
---

Publish it:

$ npx hexo publish test
INFO  Validating config
INFO  Published: ~/something/something/source/_posts/2023/02/03/test.md

The published post has the default values again (content of the post is ok, of course):

---
title: Test
tags:
  - blog
author: something
date: 2023-02-03 12:49:38
---

Is the problem still there under "Safe mode"?

Yes.

Environment & Settings

Node.js & npm version(node -v && npm -v)

$ node -v && npm -v
v16.17.1
8.15.0

Hexo and Plugin version(npm ls --depth 0)

hexo-site@0.0.0 /something/something
├── hexo-asset-link@2.2.1
├── hexo-fontawesome@2.3.0
├── hexo-generator-archive@2.0.0
├── hexo-generator-category@2.0.0
├── hexo-generator-feed@3.0.0
├── hexo-generator-index@3.0.0
├── hexo-generator-tag@2.0.0
├── hexo-migrator-wordpress@2.1.2
├── hexo-renderer-ejs@2.0.0
├── hexo-renderer-markdown@1.6.0
├── hexo-renderer-stylus@2.1.0
├── hexo-server@3.0.0
├── hexo-statistics-charts@3.3.1
├── hexo-tag-instagram@1.1.0
├── hexo-theme-landscape@0.0.3
└── hexo@6.3.0

Others

$ npx hexo version
INFO  Validating config
hexo: 6.3.0
hexo-cli: 4.3.0
os: darwin 22.1.0 13.0.1

node: 16.17.1
v8: 9.4.146.26-node.22
uv: 1.43.0
zlib: 1.2.11
brotli: 1.0.9
ares: 1.18.1
modules: 93
nghttp2: 1.47.0
napi: 8
llhttp: 6.0.9
openssl: 1.1.1q+quic
cldr: 41.0
icu: 71.1
tz: 2022a
unicode: 14.0
ngtcp2: 0.1.0-DEV
nghttp3: 0.1.0-DEV
@Dowe-dong

This comment was marked as off-topic.

@stevenjoezhang
Copy link
Member

The front-matter configuration should be kept by the following code:

Object.assign(data, yfmParse(content));

It's strange that the tags are overwritten to defaults...

@neverbot
Copy link
Author

It's strange that the tags are overwritten to defaults...

Do you mean you weren't able to reproduce or you can reproduce but don't know why it's happening?

@Dowe-dong

This comment was marked as off-topic.

@stevenjoezhang
Copy link
Member

@neverbot I cannot reproduce. The source code of Hexo also suggests that the front-matter should not be overwritten.

@D-Sketon
Copy link
Member

cannot reproduce

@neverbot
Copy link
Author

neverbot commented Apr 28, 2023

Reproduced just now with a fresh installation of hexo without any other plugin or theme. Just install hexo, edit the scaffolds, create a draft and publish it. Maybe it was not well explained (my bad), when I mentioned "default" values, I mean what you specify in the scaffolds:

scaffolds/draft.md and scaffolds/post.md have the same content, adding a default tag:

---
title: {{ title }}
tags:
  - blog
author: neverbot
---

After creating the draft, edit the tags to

---
title: test
tags:
  - blog
  - a
  - b
author: neverbot
---

or

---
title: test
tags:
  - a
  - b
author: neverbot
---

It will not preserve the a and b tags, doesn't matter if I include the default blog tag or not.

I think what should be expected is the tags added in the post scaffold and the tags added in the edited draft to be... maybe merged on publish?

@D-Sketon
Copy link
Member

hexo/lib/hexo/post.ts

Lines 302 to 309 in 725422a

// Parse front-matter
const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : load(frontMatter);
Object.keys(data)
.filter(key => !preservedKeys.includes(key) && obj[key] == null)
.forEach(key => {
obj[key] = data[key];
});

maybe keys like tag and author that are not wrapped in {{}} in the scaffold should be merged after parsing the front-matter🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants