Skip to content

(sql) 'foreign key' and 'primary key' do not highlight 'key'. #4057

@htozaki

Description

@htozaki

Describe the issue
When I write foreign key or primary key is sql language, primary or foreign is highlighted, however key is not.

Which language seems to have the issue?
sql.

Are you using highlight or highlightAuto?

highlightAll().

Sample Code to Reproduce

<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/agate.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>
hljs.highlightAll();
</script>
</head>
<body>
<pre>
<code class="sql">
create table Book (
    bookId      int,
    publisherId int,
    primary key(bookId),
    foreign key(PublisherId) references Publisher(id)
);
</code>
</pre>
</body>
</html>

Expected behavior
'key' is also highlighted.

Additional context
Even though I add the following to my script to force the word 'key' as keyword,

const custom_keywords = ['key'];
hljs.getLanguage('sql').keywords.keyword.push(...custom_keywords);

key won't be highlighted. (However this addition will now highlights foreign key or foreign&nbsp;key primary&#8288; key etc when the separator is not a single space.)

I think the regexp using in COMBO does not recognize space as a part of keyword, that is the problem.
Supposing the intention of this code is to treat foreign key as one keyword.
In my script, changing the regexp so as to include space character

hljs.getLanguage('sql').contains[0].keywords.$pattern = /\b[\w\. ]+/;

solve the problem.

However I have not understand yet why create table is working as intended, so I am not sure this is correct fix.

Also it does not highlights when I write foreign key using 2 spaces etc, unless I added the key in keyword.

Though I have not understand the whole code yet, my suggestion would be the regexp at

$pattern: /[\w\.]+/,

should be changed to recognize space as a part of keyword. (Also it need to add key in reserved word list

const RESERVED_WORDS = [

in case there are 2 or more whitespaces between foreign and key.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions