obsidian.nvim icon indicating copy to clipboard operation
obsidian.nvim copied to clipboard

Completion and `ObsidianFollowLink` not working for heading in chinese

Open hsnotebook opened this issue 1 year ago • 5 comments

🐛 Describe the bug

I write markdown in chinese, and I link to the heading of other file using [[文档#标题1]].

Problem 1: When I press Enter on the link, it promts for creating new note instead of following the link.

Problem 2: When I type [[文档#, the chinese headings do not show in the completion.

I think Problem 1 is caused by ANCHOR_LINK_PATTERN in util.lua. I change util.ANCHOR_LINK_PATTERN = "#[%w%d][^#]*" to util.ANCHOR_LINK_PATTERN = "#[^#]*", and it works.

I have on idea of Problem 2.

Config

  {
    "epwalsh/obsidian.nvim",
    version = "*", -- recommended, use latest release instead of latest commit
    lazy = false,
    dependencies = {
      "nvim-lua/plenary.nvim",
      {
        "nvim-telescope/telescope.nvim",
        opts = { defaults = { file_ignore_patterns = { ".obsidian" } } }
      }
    },
    opts = {
      workspaces = {
        {
          name = "wiki",
          path = "~/wiki",
        },
      },
      daily_notes = {
        folder = "diary",
        date_format = "%Y-%m-%d",
      },
      attachments = { img_folder = "assets/images" },
      follow_url_func = function(url)
        vim.fn.jobstart({ "xdg-open", url }) -- linux
      end,
    },
    keys = {
      { "<leader>ni", "<cmd>ObsidianToday<cr>", desc = "Open Today Note" },
      {
        "<leader>nf",
        function()
          require("telescope.builtin").find_files({ prompt_title = "< Find In Notes >", cwd = "~/wiki/" })
        end,
        desc = "Find Notes",
      },
      {
        "<leader>ns",
        function()
          require("telescope.builtin").live_grep({ prompt_title = "< Search In Notes >", cwd = "~/wiki/" })
        end,
        desc = "Search Notes",
      },
      {
        "<leader>no",
        function()
          local fullPath = vim.api.nvim_buf_get_name(0) -- /home/hs/wiki/xxxxx
          local wikipath = string.sub(fullPath, 15)
          local uri = ("obsidian://open?vault=wiki&file=%s"):format(wikipath)
          vim.fn.jobstart("xdg-open" .. " '" .. uri .. "'")
        end,
        desc = "Open in Obsidian",
      },
    },
  },

Environment

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1720049189
Run "nvim -V1 -v" for more info
Obsidian.nvim v3.9.0 (ae1f76a75c7ce36866e1d9342a8f6f5b9c2caf9b)
Status:
  • buffer directory: nil
  • working directory: /home/hs/wiki
Workspaces:
  ✓ active workspace: Workspace(name='wiki', path='/home/hs/wiki', root='/home/hs/wiki')
Dependencies:
  ✓ plenary.nvim: a3e3bc82a3f95c5ed0d7201546d5d2c19b20d683
  ✓ nvim-cmp: d818fd0624205b34e14888358037fb6f5dc51234
  ✓ telescope.nvim: a0bbec21143c7bc5f8bb02e0005fa0b982edc026
Integrations:
  ✓ picker: TelescopePicker()
  ✓ completion: enabled (nvim-cmp) ✗ refs, ✗ tags, ✗ new
    all sources:
      • nvim_lsp
      • ultisnips
      • buffer
      • path
Tools:
  ✓ rg: ripgrep 14.1.0
Environment:
  • operating system: Linux
Config:
  • notes_subdir: nil%

hsnotebook avatar Jul 29 '24 09:07 hsnotebook

Hey @hsnotebook, sorry you're having trouble. Could you be more specific about problem 2? I screenshot would help me.

epwalsh avatar Jul 30 '24 22:07 epwalsh

image

Thanks for response.

I think it does not regecognize the chinese when it searches candidates for completion. Maybe it is the regex only matching acsii characters. I am not familiar with lua, and I can't find where to fix.

hsnotebook avatar Jul 31 '24 06:07 hsnotebook

Hi @epwalsh. Problem 1 can be resolved too by adding chinese character to the ANCHOR_LINK_PATTERN. util.ANCHOR_LINK_PATTERN = "#[%w%d\u{4e00}-\u{9fff}][^#]*"

hsnotebook avatar Aug 01 '24 01:08 hsnotebook

Hey @epwalsh , Problem 2 is resolved by changing the standardize_anchor function in util.lua from

util.standardize_anchor = function(anchor)
  -- Lowercase everything.
  anchor = string.lower(anchor)
  -- Replace whitespace with "-".
  anchor = string.gsub(anchor, "%s", "-")
  -- Remove every non-alphanumeric character.
  anchor = string.gsub(anchor, "[^#%w_-]", "")
  return anchor
end

to

util.standardize_anchor = function(anchor)
  -- Lowercase everything.
  anchor = string.lower(anchor)
  -- Replace whitespace with "-".
  anchor = string.gsub(anchor, "%s", "-")
  -- Remove every non-alphanumeric character.
  anchor = string.gsub(anchor, "[^#%w_-\u{4e00}-\u{9fff}]", "")
  return anchor
end

hsnotebook avatar Aug 09 '24 09:08 hsnotebook

It also doesn't work for headings in Russian and Armenian. So I guess it's any non-latin language.

And applying the same hack, but for cyrillic and armenian characters sets everything works for me. The only 2 nitpicks are that cmp-nvim shows the headings without spaces between words and order of headings is not based on the order of appearance in the file but is based on the size of the heading (# headings are coming first in the list, then ## headings, etc.)

The only problem is that this method is not scalable, imagine trying to add every alphabet

RobberFox avatar Dec 16 '24 16:12 RobberFox