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

How would I go about creating a default tag on new note? #173

Closed
mesa123123 opened this issue Aug 9, 2023 · 10 comments · Fixed by #178
Closed

How would I go about creating a default tag on new note? #173

mesa123123 opened this issue Aug 9, 2023 · 10 comments · Fixed by #178
Labels
documentation Improvements or additions to documentation

Comments

@mesa123123
Copy link

📚 The doc issue

I'm creating notes left and right using this and theres heaps of blank notes in my vault, I want to know which ones are blank so I can go back and rewrite them later on, so I'm thinking everytime I create a note could it have a default tag of "TODO" or something akin?

After reading the code a bit I'm guessing it has something to do with the note_id_func option and inserting something like { title = "title", tags = { "TODO" } }?

Thought I'd ask as I'm a little stumped as my lua is not as sharp as I'd like?

Am I on the right track?

Suggest a potential alternative/fix

No response

@mesa123123 mesa123123 added the documentation Improvements or additions to documentation label Aug 9, 2023
@epwalsh
Copy link
Owner

epwalsh commented Aug 10, 2023

Hey @mesa123123, you could accomplish this by customizing the note_frontmatter_func in your config. From the README, this is what the default function looks like:

  note_frontmatter_func = function(note)
    -- This is equivalent to the default frontmatter function.
    local out = { id = note.id, aliases = note.aliases, tags = note.tags }
    -- `note.metadata` contains any manually added fields in the frontmatter.
    -- So here we just make sure those fields are kept in the frontmatter.
    if note.metadata ~= nil and require("obsidian").util.table_length(note.metadata) > 0 then
      for k, v in pairs(note.metadata) do
        out[k] = v
      end
    end
    return out
  end,

@mesa123123
Copy link
Author

mesa123123 commented Aug 11, 2023

Hey @epwalsh been giving that a crack and my tags aren't changing:

This is my frontmatter function:

 local make_note_frontmatter = function(note)                                                  
      local out = { tags = "todo"}                                                              
      return out                                                                                
  end

so my fontmatter should be something along the lines of:

  ---                                                                                           
  id: "Note"                                                                                   
  aliases:                                                                                      
    - "Note"                                                                                   
  tags:
   - "todo"                                                                    
  ---           

Instead its still:

  ---                                                                                           
  id: "Note"                                                                                   
  aliases:                                                                                      
    - "Note"                                                                                   
  tags: []                                                                                      
  ---

What value would you need to give tags to change it?

Ive tried:

-- Attempt 1
out = { tags = "todo"}
-- Attempt 2
tags = { "todo" }
note.tags.insert(tags)
-- Attempt 3
default_tags = {"todo"}
out = { tags =  default_tags }
-- Attempt 4
default_tags = function()
     return { "todo" }
end
out = {tags = default_tags()}
-- Attempt 5
default_tags = function()
     return { "todo" }
end
out = {tags = default_tags}

Could you tell where I'm going wrong?

@mesa123123
Copy link
Author

mesa123123 commented Aug 11, 2023

Ok so looking through note.lua

I think this is what updates the tags based on the front matter??

       elseif k == "tags" then
          if type(v) == "table" then
            for _, tag in ipairs(v) do
              if type(tag) == "string" then
                table.insert(tags, tag)
              else
                echo.warn(
                  "Invalid tag value found in frontmatter for "
                    .. path
                    .. ". Expected string, found "
                    .. type(tag)
                    .. "."
                )
              end
            end

So knowing that didn't help me too much:

  ---                                                                                           
  id: "DidThisWork?"                                                                            
  aliases:                                                                                      
    - "DidThisWork?"                                                                            
  tags: []                                                                                      
  ---                                                                                           
                                                                                                
W # DidThisWork?  
  local make_note_frontmatter = function(note)                                                  
      local out = { tags = { "todo" } }                                                         
      return out                                                                                
  end                                                                                           
                                                                                                
                                                                                                
  -- Setup                                                                                      
  ----------                                                                                    
  require("obsidian").setup({                                                                   
      dir = "~/Learning",                                                                       
      templates = {                                                                             
          subdir = "templates",                                                                 
          date_format = "%Y-%m-%d-%a",                                                          
          time_format = "%H:%M",                                                                
      },                                                                                        
      mappings = {},                                                                            
      note_id_func = make_note_id,                                                              
      note_frontmatter_func = make_note_frontmatter                                             
  })                                                                                            
  ---------- 

@mesa123123
Copy link
Author

Would calling this method somehow help?

--- Line 86 Note.lua ---
---Add a tag to the note.
---
---@param tag string
note.add_tag = function(self, tag)
  if not self:has_tag(tag) then
    table.insert(self.tags, tag)
  end
end

@epwalsh
Copy link
Owner

epwalsh commented Aug 14, 2023

Hey @mesa123123, sorry for the slow response. I think you're right about calling note:add_tag. This worked for me:

      note_frontmatter_func = function(note)
        note:add_tag "TODO"
        local out = { id = note.id, aliases = note.aliases, tags = note.tags }
        -- `note.metadata` contains any manually added fields in the frontmatter.
        -- So here we just make sure those fields are kept in the frontmatter.
        if note.metadata ~= nil and require("obsidian").util.table_length(note.metadata) > 0 then
          for k, v in pairs(note.metadata) do
            out[k] = v
          end
        end
        return out
      end,

@mesa123123
Copy link
Author

Brilliant, sorry for the slow response on my end too, I'll give that a try now and see if i can recreate it

@mesa123123
Copy link
Author

Doesn't appear to be working sadly! Is there somethig that might be blocking it? I've tried for both link under cursor and regular create functions

epwalsh added a commit that referenced this issue Aug 24, 2023
Fixes #173

The configuration option `note_frontmatter_func` will now be used when
creating new notes.
@epwalsh
Copy link
Owner

epwalsh commented Aug 24, 2023

Ah I see the issue! #178 should fix.

epwalsh added a commit that referenced this issue Aug 24, 2023
Fixes #173

The configuration option `note_frontmatter_func` will now be used when
creating new notes.
flaviosakakibara pushed a commit to flaviosakakibara/obsidian.nvim that referenced this issue Sep 5, 2023
@mesa123123
Copy link
Author

mesa123123 commented Sep 12, 2023

Hey @epwalsh just wondering if you have a similar bug where a daily note upon being saved ends up adding the default tag to the note.
I've tested this on a new note that isn't daily either.

Is there a way to solve this issue?

Let me know! It could just be my config messing things up

@epwalsh
Copy link
Owner

epwalsh commented Sep 20, 2023

Hey @mesa123123, sorry just getting back to this now. Are you saying that when you make a new daily note it's missing the extra tag?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants