mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 09:17:08 +08:00
47d1703b67
**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. |
||
---|---|---|
.. | ||
app | ||
assets | ||
config | ||
db | ||
lib | ||
public | ||
spec | ||
test/javascripts | ||
plugin.rb | ||
README.md |
This plugin is still in active development and may change frequently
Documentation
The Discourse Chat plugin adds chat functionality to your Discourse so it can natively support both long-form and short-form communication needs of your online community.
For user documentation, see Discourse Chat.
For developer documentation, see Discourse Documentation.