Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪲 Set highlighter language and level #6126

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

boryanagoncharenko
Copy link
Collaborator

PR #6085 makes uses of data-level and data-kwlang but these are never set. As a result, the language of the highlighter always defaults to English regardless of the current language. This PR makes some minimal changes to address the issue.

Fixes #6112

How to test
As an anonymous user go to /hedy and change the language to Turkish (or any other language). In the editor paste yazdır test and check that the first word is highlighted as a command. Do the same check for the /tryit page and a logged in user.

@boryanagoncharenko boryanagoncharenko changed the title 🪲 Set highliter language and level 🪲 Set highlighter language and level Jan 23, 2025
rix0rrr
rix0rrr previously approved these changes Jan 23, 2025
Copy link
Contributor

mergify bot commented Jan 23, 2025

Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork).

Copy link
Contributor

mergify bot commented Jan 23, 2025

This pull request has been removed from the queue for the following reason: pull request dequeued.

Pull request #6126 has been dequeued. The pull request rule doesn't match anymore. The following conditions don't match anymore:

  • all of:
    • #approved-reviews-by>=1
  • any of: [🔀 queue conditions]
    • all of: [📌 queue conditions of queue default_queue]
      • #approved-reviews-by >= 1 [🛡 GitHub branch protection]

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.

If you want to requeue this pull request, you need to post a comment with the text: @mergifyio requeue

@@ -210,8 +210,8 @@ export class HedyCodeMirrorEditor implements HedyEditor {
state: state
});

const levelStr = $(element).closest('[data-level]').attr('data-level');
const lang = $(element).closest('[data-kwlang]').attr('data-kwlang') ?? 'en';
const levelStr = $('[data-level]').attr('data-level');
Copy link
Collaborator

Choose a reason for hiding this comment

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

The old code tried to find the closest enclosing element that has a data-level attribute.

The new code finds any data-level attribute anywhere on the page, whether it has a relation to the editor or not.

Why did the old code not work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is no data-level or data-kwlang on any element. This is why it did not work. I added the attributes to the editor but then ofc closest does not work. We could either put the attributes on an enclosing element indeed which is a bit weird or keep them in the editor and fetch them directly from the editor $('#editor').attr('data-*'). What was your intention?

Copy link
Collaborator

@rix0rrr rix0rrr Jan 23, 2025

Choose a reason for hiding this comment

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

Putting the level and kwlang directly on the editor works for the main editor because we control the HTML for it, but doesn't work very well for example for all the syntax highlighted preview editors in running text, because those are <pre> tags that are generated from Markdown. So I wanted a place to put it where it's still in context, but not the editor itself, so it become "some enclosing <div>".

I'd like to keep the logic consistent between the read/write editor and the preview-only editors. That allows us to de-dupe this information wherever it makes sense (*).

I for sure though x.closest() would return x if it matched the selector, but perhaps it doesn't. The document seems to bear that out though, I recall looking this up:

image

https://api.jquery.com/closest/

(*) In general, I'd like to move to a future in which more data is stored in the HTML, via data-* attributes in strategic locations, than passed via JavaScript to initializeXxx() functions. The latter requires a very rigid initialization order that doesn't work very well with HTMX.

@@ -15,7 +15,8 @@
<!-- The actual editor -->
<div id="editor" data-cy="editor" style="background: #272822; font-size:0.95em; color: white; direction: {{ g.dir }};" lang="en" blurred='false'
{% if editor_readonly %}data-readonly="true"{% endif %}
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height"></div>
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height"
data-kwlang="{{ current_language().lang }}" data-level="{{ level }}"></div>
Copy link
Collaborator

Choose a reason for hiding this comment

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

An enclosing scope should be good enough, and I think it should be on there already, no?

@@ -25,7 +25,8 @@
<!-- The actual editor -->
<div id="editor" style="background: #272822; font-size:0.95em; color: white; direction: {{ g.dir }};" lang="en" blurred='false'
{% if editor_readonly %}data-readonly="true"{% endif %}
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height"></div>
class="w-full flex-1 text-lg rounded min-h-0 overflow-hidden fixed-editor-height"
data-kwlang="{{ current_language().lang }}" data-level="{{ level }}"></div>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, if we can put this attribute on a <div> that contains all editors, it should work already?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

🪲 code highlighting in slides does not work properly for translations on Alpha
2 participants