8000 Fix: New File Always Defaults to .md Instead of Selected Format (e.g., JSON) by jyothisjoy123 · Pull Request #5711 · Zettlr/Zettlr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: New File Always Defaults to .md Instead of Selected Format (e.g., JSON) #5711

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

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

jyothisjoy123
Copy link
@jyothisjoy123 jyothisjoy123 commented Apr 3, 2025

Description

This PR resolves the issue where new files always defaulted to .md. Now users can create new files with .tex, .json, .yaml, or .md extensions. Unsupported extensions will throw an error in the logs.

Changes

1.Added support for new file types (.tex, .json, .yaml, .md).
2.If an unsupported file extension is chosen, an error is logged.
3.No breaking changes to the existing file creation flow.

Additional information

Relates to #5703

Tested on:

macOS 15.3.2
Node.js v22.14.0

NB: Since this is a bug fix, I wasn’t sure if an entry in CHANGELOG.md is required. Let me know if I should add one, and I’d be happy to update it!

Copy link
boring-cyborg bot commented Apr 3, 2025

Thank you for opening your first PR! 🎉 We would like to thank you already for your contribution. If everything checks out, we'll make sure to review the PR as soon as possible and give feedback. Please note a few things:

  • For every PR, the automated pipeline will run the linter and unit tests on
    your code. Should these fail, we will not merge your PR.
  • Activate your notifications and check your inbox regularly to respond to our
    reviews and/or comments.

We are glad for every improvement that makes its way into the app, so we look forward to collaborating on merging this PR.

@nathanlesage
Copy link
Member

Thank you for opening this! I have two things I would like to clarify before doing a code review:

  1. You seem to have removed and added things here and there, not necessarily with changing functionality. What is the reason? (I was especially confused in cases where you changed code comments)
  2. In issue New File Always Defaults to .md Instead of Selected Format (e.g., JSON)" #5703 we have discussed adding another variable, %ext, but if I see it correctly, you have simply removed the filename extension from the pattern, correct? Why is this?

In general, it helps reviewing PRs if you only change what is actually necessary. Currently it's a bit hard to disentangle what you have changed code-wise due to the various other changes you introduced. But again, thank you so far, I'm sure we can turn this into a great PR in no time!

Copy link
Member
@nathanlesage nathanlesage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long with the review. I believe this PR is going in the right direction. Most of my comments are relatively simple. There are a few things I'm unsure about and would like you to clarify some of the changes you did — I am not too familiar with the exact logic in this command because I haven't looked at it in quite some time, so I'm happy to listen to your justifications!

Finally, there are now a few linter/test errors that would need to be fixed (see the check annotations).

@@ -72,24 +70,47 @@ export default class FileNew extends ZettlrCommand {
dirpath = arg.path
isFallbackDir = false
} else if (openDirectory !== null && await this._app.fsal.isDir(openDirectory)) {
// Choose the openDirectory
// Explicitly provided directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo this comment change

) {
// The user wishes/needs to confirm the filename and/or the directory
const chosenPath = await this._app.windows.saveFile(path.join(dirpath, generatedName))
let defaultFileName = generatedName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not reuse the generatedName variable here?

}
break
case 'yaml':
if (!defaultFileName.endsWith('.yaml')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a check if the name ends with .yml which is also a valid extension for YAML files. We can still default to .yaml below, that's absolutely fine.

10000
// There's no valid file extension given. We have to add one. By default
// we assume Markdown, but let ourselves be guided by the given type.
// Check if the file already has an extension, and don't add the default extension if it does
if (!path.extname(filename)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo the changes in the if/switch construction here.

First, if(!path.extname(filename)) does not pass the linter, you'd need to explicitly compare it to a string w/o implicit boolean conversion. Second, this construction should've already worked. Or is there something problematic with how it works right now? Did I overlook something?

if (await this._app.fsal.pathExists(absPath)) {
// Ask before overwriting
if (!await this._app.windows.shouldOverwriteFile(filename)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this comment

// First create the file
await this._app.fsal.writeTextFile(absPath, '')

// And directly thereafter, open the file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this comment. (In general, please do not delete comments unless they become invalid due to any change)

pattern = uuid()
}

ext = ext || 'md'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ext is not optional, so this case is unnecessary. You can remove this.

* @return {string} The new filename.
*/
export default function generateFilename (filenamePattern: string, idGenPattern: string): string {
export default function generateFilename (filenamePattern: string, idGenPattern: string, ext: string ): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to strongly type the extension:

Suggested change
export default function generateFilename (filenamePattern: string, idGenPattern: string, ext: string ): string {
export default function generateFilename (filenamePattern: string, idGenPattern: string, ext: 'md'|'json'|'yaml'|'tex' ): string {

}

ext = ext || 'md'
pattern = pattern.replace(/%ext/g, ext)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You never introduced this %ext variable, so this change won't do much. I suggest to use instead uuid() + '.' + ext construction. (Or maybe with template strings, that might look nicer).

@@ -21,6 +21,6 @@ import replaceStringVariables from './replace-string-variables'
*
* @return {String} The final string after replacements.
*/
export default function generateId (pattern: string = '%Y%M%D%h%m%s'): string {
export default function generateId (pattern: string = '%Y%M%D%h%m') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo this change, you removed the (for this change unaffected) %s-variable, and removed the return type.

@jyothisjoy-a1911465
Copy link

Thank you @nathanlesage for the detailed evaluation. Working on it and will update the requested changes.

jyothisjoy-a1911465 and others added 4 commits June 16, 2025 23:33
Added %ext to FileNamePattern. This is used by function generateFileName, where it replaces %id and %ext.
@jyothisjoy123
Copy link
Author

@nathanlesage I have reviewed your suggestions and updated the file. I found that the core issue was in-fact from the config file, where the filenamepattern was set to %id and not %id.%ext. Therefore, I undo the other changes and found that my fix passed all the test cases. yarn test showed 221 passed tests.

However, after resolving the merge conflict, I found that current develop branch does not run. yarn start results in following error

node:events:496
      throw er; // Unhandled 'error' event
      ^

Error: spawn electron-forge ENOENT
    at ChildProcess._handle.onexit (node:internal/child_process:285:19)
    at onErrorNT (node:internal/child_process:483:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
    at onErrorNT (node:internal/child_process:483:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn electron-forge',
  path: 'electron-forge',
  spawnargs: [
    'start',
    '--',
    '--data-dir="/Users/jyothisjoy/projects/Zettlr/resources/test-cfg"'
  ]
}

Node.js v22.14.0

How do you wish me to approach this situation? Do you want me to undo the "merge commit" and force push changes to this branch?

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

Successfully merging this pull request may close these issues.

3 participants
0