Use tokenClassNodeProp to access the node type

This commit is contained in:
Benji Grant 2024-08-21 10:30:24 +10:00
parent a46567ca3f
commit 8679d8bc52
No known key found for this signature in database
GPG key ID: 669D43F2AB2F4F1B
2 changed files with 15 additions and 2 deletions

5
.changes/node-type.md Normal file
View file

@ -0,0 +1,5 @@
---
"obsidian-css-inlay-colors": patch:fix
---
Swap to a more robust method of determining the node type in live preview mode

View file

@ -1,4 +1,4 @@
import { syntaxTree } from '@codemirror/language'
import { syntaxTree, tokenClassNodeProp } from '@codemirror/language'
import type { Range } from '@codemirror/state'
import {
Decoration,
@ -8,6 +8,7 @@ import {
type ViewUpdate,
WidgetType,
} from '@codemirror/view'
import type { NodeProp } from '@lezer/common'
import { type Color, formatHex, parse } from 'culori'
import { formatColor } from './formatColor'
@ -93,7 +94,9 @@ const createColorWidgets = (view: EditorView, colorPickerEnabled: boolean) => {
from,
to,
enter: (node) => {
if (node.name.includes('inline-code')) {
if (
node.type.prop(tokenClassNodeProp)?.split(' ').includes('inline-code')
) {
const text = view.state.sliceDoc(node.from, node.to)
// Not a valid color
@ -123,3 +126,8 @@ const createColorWidgets = (view: EditorView, colorPickerEnabled: boolean) => {
return Decoration.set(widgets)
}
// Codemirror does not correctly export a type for this constant, but it does exist
declare module '@codemirror/language' {
const tokenClassNodeProp: NodeProp<string>
}