Coder Social home page Coder Social logo

nothingislost / obsidian-dynamic-highlights Goto Github PK

View Code? Open in Web Editor NEW
128.0 4.0 6.0 65 KB

An experimental Obsidian plugin that highlights all occurrences of the word under the cursor

License: MIT License

JavaScript 1.82% TypeScript 75.20% CSS 22.98%

obsidian-dynamic-highlights's Introduction

Dynamic Highlights

An Obsidian plugin that dynamically highlights text based on selection or search query.

This plugin currently only works in the Source or Live Preview editor modes. For now it will not affect Reading mode or the Legacy Editor.

Selection Highlights

If there is no active selection, all occurences of the word underneath the current cursor position will be highlighted

  • Word matching is case insensitive
  • The word currently under the cursor will be marked with .cm-current-word
  • Occurences of the current word found elsewhere in the document will be marked with cm-matched-word
  • All occurences will recieve the data-contents data attribute which will hold the current word value

If there is an active selection, all occurences of the text inside the selection will be highlighted.

  • String matching is case insensitive
  • The currently selected string will be marked by default with .cm-selection
  • Occurences of the currently selected string found elsewhere in the document will be marked with cm-matched-string
  • All matches will recieve the data-contents data attribute which will hold the selected string value

Persistent Highlights

Persistent highlights are created by defining a search query and associating a CSS class name and color. Once defined, any string that matches the search query will be marked with the associated CSS class and will receive background color that matches the chosen color.

Search queries can be written using regular expressions as long as you toggle the regex option for the query.

You can define as many unique highlighters as you'd like as long as the class names are unique. Performance may be impacted when creating a large number of complex regex queries so be mindful of your regex complexity.

Examples

You can import the examples with the Import Button at the top right of the plugin's settings.

Visual Linting

Highlights double spaces, empty list markers, double list markers, preceding spaces, and trailing spaces.

{
  "Mini-Linting": {
    "class": "Mini-Linting",
    "color": "#A70F0F38",
    "regex": true,
    "query": " {2,}(?!\\|| |$)|- - |^\\s*- \\n|^ +(?![0-9-`])",
    "mark": [
      "match"
    ],
    "css": ".cm-line .Mini-Linting {\n  background: none;\n}\n\n.cm-line:not(.cm-active) .Mini-Linting {\n  outline: 1px solid var(--text-error);\n}"
  },
}
Strike Out Filler Words to avoid in Writing
{
  "Filler-Words": {
    "class": "Filler-Words",
    "color": "#2D801838",
    "regex": true,
    "query": "\\b([Aa] bit|[Aa]bsolutely|[Aa]ctually|[Aa]nd all that|[Aa]nd so forth|[Aa]nyway|[Bb]asically|[Cc]ertainly|[Cc]learly|[Cc]ompletely|[Dd]efinitely|[Ee]ffectively|[Ee]ntirely|[Ee]ssentially|[Ee]vidently|[Ee]xtremely|[Ff]airly|[Ff]rankly|[Ff]requently|[Gg]enerally|[Hh]opefully|[Kk]ind of|[Ll]argely|[Ll]iterally|[Mm]ore or less|[Mm]ostly|[Oo]ccasionally|[Oo]ften|[Oo]verall|[Pp]articularly|[Pp]erhaps|[Pp]ossibly|[Pp]ractically|[Pp]recisely|[Pp]resumably|[Pp]retty|[Pp]rimarily|[Pp]robably|[Pp]urely|[Qq]uite|[Rr]arely|[Rr]ather|[Rr]eally|[Rr]elatively|[Ss]eriously|[Ss]ignificantly|[Ss]imply|[Ss]lightly|[Ss]omehow|[Ss]ort of|[Ss]pecifically|[Ss]trongly|[Ss]upposedly|[Ss]urely|[Tt]he fact that|[Tt]otally|[Tt]ruly|[Tt]ypically|[Uu]ltimately|[Uu]sually|[Vv]ery|[Vv]irtually|[Ww]idely)\\b",
    "mark": [
      "match"
    ],
    "css": ".cm-line .Filler-Words{\n\ttext-decoration: line-through;\n\tbackground: none;\n\tcolor: var(--text-muted);\n}\n\n/* where to disable */\n.HyperMD-quote.cm-line .Filler-Words,\n.pdf-annotations .cm-line .Filler-Words {\n\ttext-decoration: none;\n\tcolor: unset;\n}"
  }
}
Highlight Custom Styles in Pandoc Exports (:::)
{
  "Pandoc-Syntax": {
    "class": "Pandoc-Syntax",
    "color": "#77787C4A",
    "regex": true,
    "query": "::: \\{.*?\\}[\\s\\S]*?:::",
    "mark": [
      "match",
      "group"
    ],
    "css": ""
  }
}

Settings

Delay

The delay before highlighting is applied after moving the cursor

Ignored Words

A comma delimited list of words that will not be highlighted

The default list can be found here: https://gist.github.com/sebleier/554280

Limitations

  • There is currently no support for dynamic highlights in reading/preview mode.
  • Only strings with 3 or more characters will be highlighted in selection highlight mode

Acknowledgments

Thanks to @chrisgrieser aka @pseudometa for the plugin idea and feedback. Thanks to @chetachiezikeuzor for the plugin settings UI code which was inspired by https://github.com/chetachiezikeuzor/highlightr-Plugin/

obsidian-dynamic-highlights's People

Contributors

chrisgrieser avatar nothingislost avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

obsidian-dynamic-highlights's Issues

Allow Multi line Matches (`s` flag for a regex)

I'd like to create a highlighter for Pandoc's Custom Style Syntax, which looks like this:

::: {custom-style="foobar"}
blabla
blabla
:::

This means I need a regex with the s flag, and would be able to even make good use of the capture group feature. however, it seems multi-line regex or custom flags are not supported? I tried entering this as a highlighter rule.

/::: {custom-style="(.*)"}\n(.*)\n:::/s

FR: Highlight entire line / bullet based on keyword

Love this plugin! It would be very cool to be able to dynamically highlight a line based on a keyword, tag, (or emoji).

Example. In my notes, I might add the tag #actionitem or (question). I would love if that entire line of text, rather than just the word, was highlighted to make it easier to find!

thanks!

FR: Add a toggle to re-enable highlighting in yaml/code block

So I had the idea to use the plugin to build me a (visual) linter. But for a lot of things (e.g. hashtags in yaml, codeblocks without language), the highlighter does not work, cause you implemented my earlier request #17 🙈

I think adding a toggle to include/search only in yaml/code blocks would easily solve this, probably on a per-highlighter-rule bases

Permanent highlights should ignore YAML + codeblocks

I noticed that the plugin also applies permanent highlights to the yaml header and code blocks, where it leads to tons of highlights that are actually distracting rather than helpful, since words and punctuation have a different meaning there as opposed to "normal" markdown text, resulting in mostly unwanted highlights.

I'd simply suggest to have the plugin's permanent highlights ignore yaml and code blocks. Either permanently, or with a toggle (which should probably be on by default).

Add an option to add custom styles to highlighters

Add a button that pops up a CSS editor modal where you can write CSS styles associated with a highlighter.

Once import/export is available, this will allow for fully self contained highlighters. This will allow for much easier highlighter sharing.

Do not highlight with multiple words selected

Screenshot 2021-12-29 21 09 43

The plugin currently simply selects the first word. However, when I am selecting text, this probably means that I am doing something where highlights do not matter that much anyway, and the extra highlight only adds noise.

Alternatively, the plugin could select both/all words there a selection is, and highlight all occurrences of those two words occurring together, which might also be useful.

Enable "Discussions" on the repo for sharing persisted highlights

Wanted a good place to share my inline dataview highlight:

{
  "inline-property-name": {
    "class": "inline-property-name",
    "color": "#00000000",
    "regex": true,
    "query": "^[\\w ]+::",
    "mark": [
      "match"
    ],
    "css": ".inline-property-name {\n  color: var(--text-faint);\n  font-size: 0.8em;\n}"
  }
}

Optionally render in transclusions and other areas

I'm loving using this, but it looks like it doesn't render in transcluded link embeds. Could this be added?

More generally, it would be nice to optionally render matched text in frontmatter YAML, backticked-text, quotes (lines starting with > ), transclusions, and maybe other areas too. I'd love to have a way to specify where to render or not render per highlight entry.

Also Implement Highlights in Preview Mode

The Permanent and the Selected String Highlights both also makes a lot of sense in preview mode, especially since dynamic highlights will often be used by people reading their notes and not necessarily editing them.

Minor Bug: disable in URLs

It's a minor thing, but I think the current-word- highlight does not make much sense in URL-strings, and also breaks some theme's css (e.g. for hovers). My suggestion would be to disable the current-word-highlight in the cm-string.cm-url scope (which covers the respective urls, I think?)

Screenshot 2022-03-18 12 36 35

Regexp doesn't match if special chars at beginning or end of word

I've noticed that it doesn't highlight the word if there is a special character (é, à, ü) at the beginning or end of a word.

highlight-current-word

I looked at the code and it does get the word correctly, but it doesn't apply the span with the CSS class to it.

const view = app.workspace.activeLeaf.view
const word = view.editor.cm.state.wordAt(view.editor.cm.state.selection.main.from)
let wordText = word && view.editor.cm.state.doc.sliceString(word.from, word.to)

image

The issue seems to be the \b in the regular expression:

image
image
image
image

FR: Setting to add delay to highlighting

Not only would it remove distractions when writing, but having a short delay would also help with performance. In case you are considering on expanding this highlight the current sentence as well, a short delay would be very fine and would probably give the time needed for performance?

split `cm-matched-string` into two css classes

with the latest update, matches to the current string + matches to the current selection both get cm-matched-string class. this makes it impossible to style them separately, even though it would be useful to use more subtle styling for current-string-matches and more emphasized styling for current-selection-matches.

Come up with a new plugin name

Highlight Current Word no longer describes the plugin. What should it be called now?

  • Contextual Highlights?
  • Selection Highlighter?

Open to suggestions.

FR: Toggle and/or frontmatter key for permanent highlights

Like, my use case for permanent highlights would be support for writing. However, when I am not writing, but just doing "normal" note-taking, I prefer to turn the writing-related permanent highlights off again.

  • One solution would be a command to toggle permanent highlights on/off.
  • a more fine-grained solution could be to have toggles for specific rules
  • a different approach could be to have a yaml-key to turn off/on permanent highlights. That way, you could simply add "permanent-highlight: true" to your writing notes.

FR: Permanent Highlight of certain words?

I was playing around with the data attribute and realized this should rather be a plugin feature to be more flexible.

So the idea is to stray a bit away form the "word where the cursor is" idea but instead permanently highlighting certain words. One use case could be to automatically apply strikethroughs to filler words. Another one I can think of is to highlight conjunctions ("and", "or", "but", etc.), which would help with recognizing sentence structures at glance. (In most languages, sentences are longer than in English.)

both features would be great features for writers to have. They would stray away from the original idea of this plugin a bit, but nevertheless probably be based on a very similar code base, so it would probably make sense to add this feature to this plugin?

[Enhancement] ability to pick highlighting variants for dark/light theme

Thanks for making this plugin! Thanks to it I am able to highlight citations in the thesis I am writing :)

Do you consider adding an option/toggle for dark/light mode? My theme is automatically switching together with the system mode,
The highlight I am using in light theme doesn't look great in dark theme, would be nice to have a toggle to set in which theme the highlight will be used

Editing a persistent highlighter's name creates a duplicate record

Steps to repro

  • Create a new persistent highlighter
  • Click "edit"
  • Update the highlighter's name
  • Press "save"

Expected

highlighter name is modified

Actual

The old highlighter exists. A second highlighter with the new name is created.

Screen.Recording.2022-02-19.at.2.11.31.PM.mov

Plugin failed to load after update

My debug info
Plugin version 0.2.3
I am on Obsidian 0.13.23, does this is related to what what fixed in the Insiders release?
Fixed plugins unable to load when using esbuild’s new esmodule format.

SYSTEM INFO:
	Obsidian version: v0.13.23
	Installer version: v0.12.19
	Operating system: Darwin Kernel Version 21.4.0: Mon Feb  7 21:32:19 PST 2022; root:xnu-8020.100.429.131.1~2/RELEASE_ARM64_T8101 21.4.0
	Login status: not logged in
	Insider build toggle: off
	Live preview: on
	Legacy editor: off
	Base theme: dark
	Community theme: Minimal
	Snippets enabled: 0
	Safe mode: off
	Plugins installed: 4
	Plugins enabled: 3
		1: Minimal Theme Settings
		2: Style Settings
		3: Templater

RECOMMENDATIONS:
	Custom theme: for cosmetic issues, please first try updating your theme to latest. If still not fixed, please try to make the issue happen in the help vault or disable community theme and snippets.
	Community plugins: for bugs, please first try updating all your plugins to latest. If still not fixed, please try to make the issue happen in the help vault or disable community plugins.

FR: Ability to highlight in Dataview

I'm uncertain if this is possible at the moment?

How would you apply a format just to .dataview.table-view-table td for the particular cell containing the match?

I'm not sure if this is:

  1. A bug
  2. I'm just being stupid and it's easy
  3. The feature does not exist

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.