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

Completion and ObsidianFollowLink not working for heading in chinese #669

Open
hsnotebook opened this issue Jul 29, 2024 · 5 comments · May be fixed by #679
Open

Completion and ObsidianFollowLink not working for heading in chinese #669

hsnotebook opened this issue Jul 29, 2024 · 5 comments · May be fixed by #679
Labels
bug Something isn't working

Comments

@hsnotebook
Copy link

hsnotebook commented Jul 29, 2024

🐛 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 hsnotebook added the bug Something isn't working label Jul 29, 2024
@epwalsh
Copy link
Owner

epwalsh commented Jul 30, 2024

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

@hsnotebook
Copy link
Author

hsnotebook commented Jul 31, 2024

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
Copy link
Author

hsnotebook commented Aug 1, 2024

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
Copy link
Author

hsnotebook commented Aug 9, 2024

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

@RobberFox
Copy link

RobberFox commented Dec 16, 2024

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

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
3 participants