Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lua/obsidian/completion/refs.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local util = require "obsidian.util"
local ts_util = require "obsidian.treesitter"

local M = {}

Expand All @@ -13,6 +14,10 @@ M.RefType = {
---@param input string
---@return string|?, string|?, obsidian.completion.RefType|?
local find_search_start = function(input)
if ts_util.in_mathzone() then
return nil
end

for i = string.len(input), 1, -1 do
local substr = string.sub(input, i)
if vim.startswith(substr, "]") or vim.endswith(substr, "]") then
Expand Down
66 changes: 66 additions & 0 deletions lua/obsidian/treesitter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
local M = {}

local ts = require "vim.treesitter"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
local ts = require "vim.treesitter"
local ts = vim.treesitter


local MATH_NODES = {
displayed_equation = true,
inline_formula = true,
math_environment = true,
}

local TEXT_NODES = {
text_mode = true,
label_definition = true,
label_reference = true,
}

local CODE_BLOCK_NODES = {
fenced_code_block = true,
indented_code_block = true,
}

function M.in_text(check_parent)
local node = ts.get_node { ignore_injections = false }
while node do
local node_type = node:type()
-- if in code block, always consider it as text
if CODE_BLOCK_NODES[node_type] then
return true
end
if node_type == "text_mode" then
if check_parent then
local parent = node:parent()
if parent and MATH_NODES[parent:type()] then
return false
end
end
return true
end
if MATH_NODES[node_type] then
return false
end
node = node:parent()
end
return true
end

function M.in_mathzone()
local node = ts.get_node { ignore_injections = false }
while node do
local node_type = node:type()
-- if in code block, don't consider it math.
if CODE_BLOCK_NODES[node_type] then
return false
end
if TEXT_NODES[node_type] then
return false
end
if MATH_NODES[node_type] then
return true
end
node = node:parent()
end
return false
end

return M
19 changes: 19 additions & 0 deletions test/fixtures/notes/note_with_MathJax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
id: note_with_MathJax
---

# Inline and Display Modes

By the law of large numbers we have that $\widehat{\mathbb{V}[\theta_{i}]} \to \mathbb{V}[\theta_{i}]$, hence
$$
\begin{flalign*}
\hat{\beta}_{1} &\approx \beta_{1} + \frac{\frac{1}{n} \sum_{i=1}^{n} \left( X_{i} - \mu_{X} \right) U_{i}}{\frac{1}{n} \sum_{i=1}^{n} \left( X_{i} - \mu_{X} \right)^{2}} &&\\
\mathbb{V} \left[ \hat{\beta}_{1} \right] &= \mathbb{V} \left[ \beta_{1} + \frac{\frac{1}{n} \sum_{i=1}^{n} \left( X_{i} - \mu_{X} \right) U_{i}}{\sigma_{X}^{2}} \right] &&\\
&= \mathbb{V} \left[ \beta_{1} \right] + \mathbb{V} \left[ \frac{\frac{1}{n} \sum_{i=1}^{n} \left( X_{i} - \mu_{X} \right) U_{i}}{\sigma_{X}^{2}} \right] &&\\
&= \left( \frac{1}{\sigma_{X}^{2}} \right)^{2} \cdot \left( \frac{1}{n} \right)^{2} \cdot \underbrace{\mathbb{V} \left[ \sum_{i=1}^{n} \left( X_{i} - \mu_{X} \right) U_{i} \right]}_{\textcolor{yellow}{*_{1}}} &&\\
&= \left( \frac{1}{\sigma_{X}^{2}} \right)^{2} \cdot \left( \frac{1}{n} \right)^{2} \cdot n \mathbb{V} \left[ \left( X_{i} - \mu_{X} \right) U_{i} \right] &&\\
&= \frac{1}{n} \cdot \frac{\mathbb{V} \left[ \left( X_{i} - \mu_{X} \right) U_{i} \right]}{\left( \sigma_{X}^{2} \right)^{2}} &&\\
\end{flalign*}
$$

Therefore, $\hat{\beta}_{1} \overset{\text{CLT}}{\sim} \mathcal{N} \left( \beta_{1}, \sigma_{\hat{\beta}_{1}}^{2} \right)$, where $\sigma_{\hat{\beta}_{1}}^{2} = \frac{\mathbb{V} \left[ \left( X_{i} - \mu_{X} \right) U_{i} \right]}{n \cdot \left( \sigma_{X}^{2} \right)^{2}}$.