Coder Social home page Coder Social logo

Comments (5)

ishchow avatar ishchow commented on July 30, 2024

From what I understand, you are proposing something like this:

        config.journals = {
            {
                path = "~/journal",
                frequencies = {"daily", "weekly", "monthly"},
                dont_create_freq_folder = true,
            },
        }

One problem I see with your proposal to add an option to remove the frequency folder is that it can lead to file collisions if that option is enabled in a journal with multiple frequencies set.

Suppose a journal is configured with weekly and monthly entries. If you set this option on, the path for the first monthly entry in 2021 would be "journal/2021/01.md" which is the same as the first weekly entry "journal/2021/01.md". Having separate folders for the frequencies prevents collisions: "journal/weekly/2021/01.md", "journal/monthly/2021/02.md".

weekly = {
transform = function(curr_date, offset)
local weekday = curr_date:getweekday()
if weekday < 2 then
curr_date:adddays(weekday - 7)
else
curr_date:adddays(2 - weekday)
end
return curr_date:adddays(7 * offset)
end,
template = function(entry_date)
local week_end_date = entry_date:copy():adddays(6)
return entry_date:fmt("# Week %W of %Y: ")
.. entry_date:fmt("%A, %B %d, %Y - ")
.. week_end_date:fmt("%A, %B %d, %Y")
end,
formatpath = function(entry_date)
return entry_date:fmt(util.join_path({"%Y", "%W.md"}))
end,
},
monthly = {
transform = function(curr_date, offset)
curr_date:setday(1)
return curr_date:addmonths(offset)
end,
template = function(entry_date)
return entry_date:fmt("# %B, %Y")
end,
formatpath = function(entry_date)
return entry_date:fmt(util.join_path({"%Y", "%m.md"}))
end,
},

I'm want to avoid special configuration options if possible since they require more code to handle the special cases and they aren't always the most flexible.

But I think your use case can easily be supported like this:

  1. Remove this line:
    table.insert(parts, frequency_name)
  2. Add the frequency name to formatpath by default for all frequencies
        -- Daily format path
        formatpath = function(entry_date)
            return entry_date:fmt(util.join_path({"daily", "%Y", "%m", "%d.md"}))
        end

I will be keeping the default behavior since it's less risky and don't want to break existing users.

This way, you could override the configuration in your dotfiles to remove the frequency name directory from the path.

For example, you can globally override the daily frequency to remove the frequency name like this:

local config = require("deardiary.config")
local util = require("deardiary.util")
config.frequencies.daily.template = function(entry_date)
     return entry_date:fmt(util.join_path({"%Y", "%m", "%d.md"}))
end

This will make all the daily entries go to the root of the journal but entries for other frequencies will still be in a separate folder (by default):

journal/
    weekly/
        2021/
            01.md
    monthly/
        2021/
            01.md
     2021/ # Daily entries
         01/
             01.md
             02.md

This approach is also more flexible since it can allow users of this plugin to namespace their entries however they want. They could choose a flat file structure just by overriding the callbacks (either globally or per journal).

Example:

journal/
    weekly-2021-01.md
    monthly-2021-01.md
    2021-01-01.md # Of course, you could add the daily prefix if you wanted
    2021-01-02.md

@frandsoh What do you think? I can make a pr if you are good with this.

from nvim-deardiary.

frandsoh avatar frandsoh commented on July 30, 2024

From what I understand, you are proposing something like this:

        config.journals = {
            {
                path = "~/journal",
                frequencies = {"daily", "weekly", "monthly"},
                dont_create_freq_folder = true,
            },
        }

How about only making the option available for the specific frequencies? Make the frequency folder by default unless the user config for the e.g. daily frequency specifically says freq_folder = false. Would that make sense/be reasonable?

My setup would look something like this:

config.journals = {
  {
    path = "~/journals",
    frequecies = {
      daily = {
        freq_folder = false,
        formatpath = function(entry_date)
          return entry_date:fmt("%Y-%m-%d.md")
        end,
        template = function(entry_date)
          return entry_date:fmt("# %Y-%m-%d")
        end
      },
      ...
    }
  }
}

from nvim-deardiary.

ishchow avatar ishchow commented on July 30, 2024

The freq_folder approach would work in your use case, but it could still lead to collisions if you have a config like this

config.journals = {
  {
    path = "~/journals",
    frequecies = {
      daily = {
        freq_folder = false, -- No collision
      },
      weekly = {
        freq_folder = false, -- Would collide with monthly
      },
      monthly = {
        freq_folder = false, -- Would collide with weekly
      },
      ...
    }
  }
}

You could also have collisions with the formatpath approach I outlined in my comment:

config.journals = {
  {
    path = "~/journals",
    frequencies = {
      daily = {
        formatpath = function(entry_date)
          -- Default: entry_date:fmt(util.join_path({"daily", "%Y", "%m", "%d.md"}))
          return entry_date:fmt("%Y-%m-%d.md")
        end,
      },
      weekly = {
        formatpath = function(entry_date)
          -- Default: entry_date:fmt(util.join_path({"weekly", "%Y", "%W.md"}))
          -- Collides with monthly
          return entry_date:fmt("%Y-%W.md")
        end,
      },
      monthly = {
        formatpath = function(entry_date)
          -- Default: entry_date:fmt(util.join_path({"monthly", "%Y", "%m.md"}))
          -- Collides with weekly
          return entry_date:fmt("%Y-%m.md")
        end,
      },
      ...
    }
  }
}

However, the formatpath approach is flexible enough that you could have a completely flat file structure (avoiding the freq folder entirely for all entries) and avoid collisions by adding prefixes:

config.journals = {
  {
    path = "~/journals",
    frequencies = {
      daily = {
        formatpath = function(entry_date)
          -- Default: entry_date:fmt(util.join_path({"daily", "%Y", "%m", "%d.md"}))
          return entry_date:fmt("%Y-%m-%d.md") -- won't collide, don't need prefix
        end,
      },
      weekly = {
        formatpath = function(entry_date)
          -- Default: entry_date:fmt(util.join_path({"weekly", "%Y", "%W.md"}))
          return entry_date:fmt("weekly-%Y-%W.md")
        end,
      },
      monthly = {
        formatpath = function(entry_date)
          -- Default: entry_date:fmt(util.join_path({"monthly", "%Y", "%m.md"}))
          return entry_date:fmt("monthly-%Y-%m.md")
        end,
      },
      ...
    }
  }
}

Or you could just have the daily entries appear directly under journal/ but keep weekly and monthly entries in separate folders. This is exactly how the 'freq_folder` approach would work in the config you gave.

config.journals = {
  {
    path = "~/journals",
    frequencies = {
      daily = {
        formatpath = function(entry_date)
          return entry_date:fmt("%Y-%m-%d.md")
        end,
      },
      "weekly",
      "monthly",
      ...
    }
  }
}

I think I will implement the formatpath approach since it's a bit more flexible than the freq_folder approach. One of the reasons I made this plugin was to give the user the ability to completely control the filesystem layout of their journals. So I prefer an approach that is more inline with that goal.

To avoid potential issues with collisions, I will document recommendations to namespace entries of different frequencies in the help file.

from nvim-deardiary.

ishchow avatar ishchow commented on July 30, 2024

@frandsoh Thank you for your suggestion! I would've never thought the frequency folder creation would be a thing that some users want to change.

I've merged the pr with the formatpath approach.

You can use this config and it should fulfill your use case.

config.journals = {
  {
    path = "~/journals",
    frequecies = {
      daily = {
        formatpath = function(entry_date)
          return entry_date:fmt("%Y-%m-%d.md")
        end,
        template = function(entry_date)
          return entry_date:fmt("# %Y-%m-%d")
        end
      },
      ...
    }
  }
}

from nvim-deardiary.

frandsoh avatar frandsoh commented on July 30, 2024

@ishchow Awesome, thank you! 🚀

from nvim-deardiary.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.