discourse/app/assets/javascripts
Régis Hanol 47d1703b67
FIX: code "block" detection before showing autocomplete (#26023)
**TL;DR:** Refactor autocomplete to use async markdown parsing for code block detection.

Previously, the `inCodeBlock` function in `discourse/app/lib/utilities.js` used regular expressions to determine if a given position in the text was inside a code block. This approach had some limitations and could lead to incorrect behavior in certain edge cases.

This commit refactors `inCodeBlock` to use a more robust algorithm that leverages Discourse's markdown parsing library.

The new approach works as follows:

1. Check if the text contains any code block markers using a regular expression.
   If not, return `false` since the cursor can't be in a code block.
1. If potential code blocks exist, find a unique marker character that doesn't appear in the text.
1. Insert the unique marker character into the text at the cursor position.
1. Parse the modified text using Discourse's markdown parser, which converts the markdown into a tree of tokens.
1. Traverse the token tree to find the token that contains the unique marker character.
1. Check if the token's type is one of the types representing code blocks ("code_inline", "code_block", or "fence").
   If so, return `true`, indicating that the cursor is inside a code block.
   Otherwise, return `false`.

This algorithm provides a more accurate way to determine the cursor's position in relation to code blocks, accounting for the various ways code blocks can be represented in markdown.

To accommodate this change, the autocomplete `triggerRule` option is now an async function.

The autocomplete logic in `composer-editor.js`, `d-editor.js`, and `hashtag-autocomplete.js` has been updated to handle the async nature of `inCodeBlock`.

Additionally, many of the tests have been refactored to handle async behavior. The test helpers now simulate typing and autocomplete selection in a more realistic, step-by-step manner. This should make the tests more robust and reflective of real-world usage.

This is a significant refactor that touches multiple parts of the codebase, but it should lead to more accurate and reliable autocomplete behavior, especially when dealing with code blocks in the editor.

> Written by an 🤖 LLM. Edited by a 🧑‍💻 human.
2024-03-11 17:35:50 +01:00
..
admin DEV: Allow typed objects theme settings to be saved via settings editor (#26100) 2024-03-11 08:42:12 +08:00
bootstrap-json Build(deps-dev): Bump html-entities from 2.4.0 to 2.5.2 (#26021) 2024-03-05 22:56:43 +01:00
deprecation-silencer DEV: Update ember-cli to 5.6.0 (#25886) 2024-02-27 10:48:30 +01:00
dialog-holder DEV: Use the "new" service import (#26059) 2024-03-06 18:05:11 +01:00
discourse FIX: code "block" detection before showing autocomplete (#26023) 2024-03-11 17:35:50 +01:00
discourse-common Build(deps-dev): Bump ember-cli from 5.6.0 to 5.7.0 (#26039) 2024-03-06 21:05:09 +01:00
discourse-hbr Build(deps-dev): Bump ember-cli from 5.6.0 to 5.7.0 (#26039) 2024-03-06 21:05:09 +01:00
discourse-i18n DEV: Update ember-cli to 5.6.0 (#25886) 2024-02-27 10:48:30 +01:00
discourse-markdown-it FEATURE: Allow rowspan and colspan attributes on HTML tables (#20973) 2024-02-28 16:59:36 +11:00
discourse-plugins Build(deps-dev): Bump ember-cli from 5.6.0 to 5.7.0 (#26039) 2024-03-06 21:05:09 +01:00
discourse-widget-hbs DEV: Fix glimmer node deprecations (#26124) 2024-03-11 13:10:05 +01:00
docs
ember-addons
ember-cli-progress-ci
ember-production-deprecations DEV: Add production-mode shim for Em deprecation (#25109) 2024-01-03 10:39:59 +00:00
float-kit Build(deps-dev): Bump ember-cli from 5.6.0 to 5.7.0 (#26039) 2024-03-06 21:05:09 +01:00
locales
pretty-text Build(deps-dev): Bump ember-cli from 5.6.0 to 5.7.0 (#26039) 2024-03-06 21:05:09 +01:00
select-kit FEATURE: Improve "+ subcategories" option (#26086) 2024-03-07 20:46:45 +02:00
theme-transpiler Build(deps): Bump terser from 5.28.1 to 5.29.1 (#26067) 2024-03-07 01:05:32 +01:00
truth-helpers DEV: Convert truth-helpers to a v2 addon and simplify imports (#26029) 2024-03-05 15:24:47 +00:00
.npmrc
handlebars-shim.js
polyfills.js
run-patch-package
service-worker.js.erb FIX: Service worker push notifications when cache disabled (#25610) 2024-02-08 11:55:32 +00:00