Compare commits
No commits in common. "main" and "2.0.0" have entirely different histories.
|
|
@ -1,9 +0,0 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = false
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
8
.gitignore
vendored
|
|
@ -25,10 +25,4 @@ data.json
|
|||
build
|
||||
|
||||
# Deprecated code
|
||||
.deprecated
|
||||
*.deprecated
|
||||
|
||||
# ignored
|
||||
.ignored
|
||||
*.ignored
|
||||
*.svg
|
||||
.deprecated
|
||||
688
LICENSE
|
|
@ -1,22 +1,674 @@
|
|||
MIT License
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (c) 2025 Kotaindah55 (Sheva ihza)
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
“Software”), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
Preamble
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
|
|
|
|||
331
README.md
|
|
@ -1,207 +1,228 @@
|
|||
# Extended Markdown Syntax - Obsidian Plugin
|
||||
|
||||

|
||||
Provides some alternatives for inline formatting using non-standard syntaxes instead of using html tags, such as underline, superscript, and much more.
|
||||
|
||||
Provides some alternatives for inline and block formatting using non-standard syntaxes instead of using html tags, such as underline, superscript, and much more.
|
||||
You can easily create text that is <u>underlined</u>, <sup>superscripted</sup>, or <sub>subscripted</sub> without any pain of writing html tags. And don't forget, this plugin supports both modes: editor mode and preview mode.
|
||||
|
||||
## 🚀 Main Features
|
||||

|
||||
|
||||
- Extended inline syntax, including:
|
||||
- insertion (underline),
|
||||
- Discord-flavored spoiler,
|
||||
- Pandoc-style superscript and subscript,
|
||||
- custom color tag for highlight, and
|
||||
- custom span with ability to insert your own class(es).
|
||||
- _Modified_ Pandoc-style fenced div for block-level syntax.
|
||||
- Edit formatted text directly without omitting its style, in contrast to using HTML tags.
|
||||
- Context-aware, syntax won't be parsed while it encounters such a codeblock, codespan, or context boundary.
|
||||
- Quick format with commands and context menu.
|
||||
- Toggle specific syntax on and off.
|
||||
- Customize your own color tags for custom highlight.
|
||||
- Predefine your own tags for custom span and fenced div.
|
||||
- Supports rendering exported PDF.
|
||||
## Features
|
||||
|
||||
## ✍️ Usage
|
||||
This plugin has four main features:
|
||||
|
||||
### 1. Inline Formatting
|
||||
1. Inline formatting (insertion, spoiler, superscript, subscript).
|
||||
2. Paragraph aligning.
|
||||
3. Custom highlight color.
|
||||
4. Context-aware formatting.
|
||||
|
||||
There are six inline formattings that currently developed in this plugin:
|
||||
> [!NOTE]
|
||||
> As it should be, the syntax will not be parsed if it is inside inline-code, codeblock, inline-math, mathblock, and internal links. Instead, they will still be styled by those syntaxes if they are inside them.
|
||||
|
||||
| Type | Syntax | Result |
|
||||
| ----------- | ---------------------- | ------------------------------------------------------------- |
|
||||
| insertion | `++your text++` | <ins>your text</ins> |
|
||||
| spoiler | `\|\|your text\|\|` | <span style="background:#2e2e2e">your text</span> |
|
||||
| superscript | `^your-text^` | <sup>your-text</sup> |
|
||||
| subscript | `~your-text~` | <sub>your-text</sub> |
|
||||
| highlight | `=={color}your text==` | <mark>your text</mark> |
|
||||
| custom span | `!!{myCls}your text!!` | your text (should be rendered with the `myCls` class defined) |
|
||||
### A. Inline Formatting
|
||||
|
||||
By default:
|
||||
- **insertion** give the text underline style,
|
||||
- **spoiler** hide the text and can be revealed by clicking it (or hovering over it in editor mode),
|
||||
- **superscript** and **subscript** make the text being raised or lowered as `<sup>` and `<sub>` do.
|
||||
There is four formatting types that currently developed in this plugin: **insertion** (underline), **Discord-flavoured spoiler**, and **Pandoc-flavoured superscript and subscript**.
|
||||
|
||||
Additionally, for the **highlight** and **custom span**, you may insert a tag right after the opening delimiter, specifying the color for the highlight, and the classes for the custom span.
|
||||
| Type | Syntax | Result |
|
||||
| ----------- | ------------------- | -------------------------------------------------- |
|
||||
| insertion | `++your text++` | <u>your text</u> |
|
||||
| spoiler | `\|\|your text\|\|` | <span style="background: #2e2e2e">your text</span> |
|
||||
| superscript | `^your-text^` | <sup>your-text</sup> |
|
||||
| subscript | `~your-text~` | <sub>your-text</sub> |
|
||||
|
||||
### 2. Block Formatting
|
||||
By default, **insertion** give the text underline style, **spoiler** hide the text and can be revealed by clicking it (or hovering over it in editor mode), while **superscript** and **subscript** make the text being raised or lowered as `<sup>` and `<sub>` do.
|
||||
|
||||
Currenty, this plugin only support **Pandoc-style fenced div** with some modifications. You only need an opening delimiter (three consecutive colons at least) to start the syntax. Blank line or the end of the document will act as a syntax closing. No need of closing delimiter.
|
||||
The main advantage of using those syntaxes over html tags is that those syntaxes are rendered properly in the editor alongside other built-in syntaxes. So you can combine them without blocking other style being rendered in editor, in stark contrast to the html tags, for instance `this *is ++italic-underlined++*`.
|
||||
|
||||
```markdown
|
||||
::: my-class-1 my-class-2
|
||||
This is fenced div content.
|
||||
To make clear and avoid ambiguity, some rules are applied to the syntaxes:
|
||||
|
||||
::: another-class
|
||||
Another fenced div content.
|
||||
#### 1. General Rules for Inline Syntaxes (Insertion, Spoiler, Superscript, Subscript)
|
||||
|
||||
- Opening delimiter must not be followed by any whitespace character (regular space, tab, and new line), and the closing one must not be preceded by any whitespace character.
|
||||
- Delimiter must satisfy its requiered length as will be explained later, and must not be preceded or followed by the same character as the delimiter, or the same non-escaped if configured for that.
|
||||
- Delimiter must not be escaped, if it was configured to not be escaped, by a backslash. Otherwise, it will act as literal character.
|
||||
- Formatting only occurs when opening delimiter met its closing.
|
||||
- Content text, that is surrounded by delimiters, must at least one character.
|
||||
- Content text must not have two or more new line character.
|
||||
- Any built-in syntaxes from Obsidian has a higher precedence than that in this plugin.
|
||||
|
||||
For better understanding, the table below can give some example applying those rules:
|
||||
|
||||
| Valid | Invalid |
|
||||
| -------------------------------------------------- | ------------------------------------------ |
|
||||
| `++lorem++` | `++ lorem++` `++lorem` `lorem++` |
|
||||
| `++lor em++` | `++lorem ++` |
|
||||
| `++l++` | `++++` |
|
||||
| `++l+o+r+em++` | `++dfdf+++` |
|
||||
| `++lo++rem++` (third plus pair doesn't include) | `++lo\nre\nm++` (`\n` as a new line char) |
|
||||
| `++lo\nr e m++` | `+++lore++m+++` |
|
||||
| `++ lor++em++` (first one doesn't include) | `++ ++` |
|
||||
| `\+++lorem++ ++ipsum\+++` (if escaping is enabled) | `\++lorem++ +\++ipsum\++++` |
|
||||
|
||||
#### 2. Rules for Insertion and Spoiler
|
||||
|
||||
- Insertion is defined as text consist at least one character surrounded by exactly double plus signs (`++`) on each side.
|
||||
- Spoiler is the same as the insertion, it's just surrounded by exactly double bars (`||`) on each side.
|
||||
|
||||
```
|
||||
++insertion++ +not insertion+ ||spoiler|| |||not spoiler||||
|
||||
```
|
||||
|
||||
### 3. Customizable Highlight
|
||||
the expected result is:
|
||||
|
||||
Under "Custom highlight" section in the settings, you can:
|
||||
> <u>insertion</u> +not insertion+ <span style="background: #2e2e2e">spoiler</span> |||not spoiler||||
|
||||
|
||||
- **show the color button**, providing color menu when you click on it,
|
||||
- **adjust opacity** of the highlight,
|
||||
- **customize color palettes** based on its tag:
|
||||
- first field for its name displayed in the color menu,
|
||||
- second field for its tag inserted into the highlight syntax,
|
||||
- show or hide each palette from the color menu,
|
||||
- rearrange colors by dragging 6-dots icon on the left side.
|
||||
#### 3. Rules for Superscript and Subscript
|
||||
|
||||
<details>
|
||||
<img src="docs/assets/custom-highlight.gif" alt="custom-highlight.gif"/>
|
||||
</details>
|
||||
- Superscript is defined as text consist at least one character surrounded by only single caret (`^`) on each side, and must not contain any of whitespace character.
|
||||
- Subscript act like superscript, it's just use single tilde as delimiter.
|
||||
- Thus, insertion and spoiler allow its content to have any whitespace character. It's contasts with the case of superscript and subscript.
|
||||
|
||||
### 4. Predefined Tags
|
||||
```
|
||||
^sup^ ^^not-sup^ ^not sup^, ~sub~ ~~not-sub~~~ ~not
|
||||
sub~
|
||||
```
|
||||
|
||||
In the settings, you can predefine specific tags for **custom span** and **fenced div**, then can be displayed in the tag menu. Same as the color palattes, you can set their name and arrange them.
|
||||
will be rendered as:
|
||||
|
||||
### 5. Commands and Context Menu
|
||||
> <sup>sup</sup> ^^not-sup^ ^not sup^, <sub>sub</sub> ~~not-sub~~~ ~not
|
||||
> sub~
|
||||
|
||||
This plugin provides commands to toggle each formatting type, also commands to show the color and tag menu. You can set the keymap for each of them through "Hotkeys" in the settings.
|
||||
#### 4. Delimiter escaping
|
||||
|
||||
It also brings functionality of all those commands (except for fenced div) to the context menu, by right-clicking on the editor and choosing "More format".
|
||||
As already explained, using escaper backslash can change the semantic meaning to that being escaped. Escaped punctuation is treated as regular character that doesn't have functional use. This applies to those delimiters when "Delimiter escaping" option is switched on.
|
||||
|
||||
<details>
|
||||
<img src="docs/assets/commands.gif" alt="commands.gif"/>
|
||||
</details>
|
||||
```
|
||||
++insertion++ \++not insertion\++ in editor mode
|
||||
```
|
||||
|
||||
> [!Note]
|
||||
However, we can only apply this feature in editor mode, since escaped character being rendered as normal character without being wrapped by any tag (and it makes sense).
|
||||
|
||||
```
|
||||
++insertion++ \++still insertion\++ \+\+still insertion\+\+, \+++not insertion+\++ in preview mode
|
||||
```
|
||||
|
||||
Due to this condition, "Delimiter escaping" was turned of by default to maintain consistency between editor and preview mode.
|
||||
|
||||
### B. Paragraph Aligning
|
||||
|
||||
Paragraph can be aligned by inserting one of specific tags exactly at the beginning of the line. Those tags are `!!left!!`, `!!right!!`, `!!center!!`, and `!!justify!!`, where each tag representing a type of alignment that should be used.
|
||||
|
||||
Note that any character that was prepended before the tag, even if it's just one space, will terminate the tag, so it's treated as regular characters.
|
||||
|
||||
```
|
||||
!!center!!will be centered
|
||||
|
||||
!!center!!will not be centered
|
||||
```
|
||||
|
||||
It's will be rendered like this:
|
||||
|
||||
> <div>
|
||||
> <p style="text-align: center">will be centered</p>
|
||||
> <p> !!center!!will not be centered</p>
|
||||
> </div>
|
||||
|
||||
#### Caution
|
||||
|
||||
In editor mode, each line depends on itself, so you must type alignment tag to get it be aligned. However, in preview mode, paragraph can consist more than one line. Accordingly, only tag that located at the first line of the paragraph (if it consists more than a line) will be treated as it is, and the others will act as a regular text.
|
||||
|
||||
So, this markdown
|
||||
|
||||
```
|
||||
!!center!!aligned to the center in both modes
|
||||
!!right!!aligned to the right in editor, to the center in preview
|
||||
not being aligned in editor (or aligned to its initial), aligned to center in preview
|
||||
```
|
||||
|
||||
will be rendered in editor like this:
|
||||
> <p style="text-align: center">aligned to the center in both modes</p>
|
||||
> <p style="text-align: right">aligned to the right in editor, to the center in preview</p>
|
||||
> not being aligned in editor (or aligned to its initial), aligned to center in preview
|
||||
|
||||
and in preview like this:
|
||||
> <p style="text-align: center">
|
||||
> aligned to the center in both modes<br>
|
||||
> !!right!!aligned to the right in editor, to the center in preview<br>
|
||||
> not being aligned in editor (or aligned to its initial), aligned to center in preview<br>
|
||||
> </p>
|
||||
|
||||
### C. Custom Highlight Color
|
||||
|
||||
Color of each highlight can be customized by adding color tag exactly after its opening delimiter. The tag consists of opening curly bracket `{`, alphanumeric character(s) `a-zA-Z0-9` (at least one, can be added with minus sign `-`), and closing curly bracket, for example: `=={red}Red==, =={Green2}Green==, =={ocean-blue}Blue==`.
|
||||
|
||||
| Valid color tag | Invalid color tag |
|
||||
| ---------------------- | --------------------------------------------------------- |
|
||||
| `=={color}lorem==` | `== {color}lorem==`, `==a{color}lorem==` |
|
||||
| `=={abcAb--10}lorem==` | `=={ }lorem==,` `=={*_}lorem==`, `=={green_color}lorem==` |
|
||||
|
||||
Valid color tag will be added in the highlight classes, so `=={red}Red==` will be parsed in html tag as **`<span class="cm-custom-highlight cm-custom-highlight-red">`** in editor and **`<mark class="custom-highlight custom-highlight-red">`** in preview mode. This plugin bring some predefined CSS rules that automatically give customized color to the highlight. Those predifined colors are red, orange, yellow, green, cyan, blue, purple, pink, and accent (accent color of your app).
|
||||
|
||||
For ease access, in the editor, the color button will appear after opening delimiter when the cursor or selection touches the highlight. Clicking on it shows colors menu and let the user choose the desired color (you can disable the color botton in settings, and thus will be hidden in the editor).
|
||||
|
||||

|
||||
|
||||
> [!note]
|
||||
>
|
||||
> In mobile devices, those commands can be added into the toolbar.
|
||||
> Currently, other colors can only be customized through CSS. In the future, I would like to add a feature that can customize and add other colors through the settings.
|
||||
|
||||
### 6. Tidier Formatting
|
||||
### D. Context-aware Formatting
|
||||
|
||||
With "Tidier formatting" enabled in the settings, you can format a single word simply by placing the cursor on it, without the need to select the entire word. Otherwise, it acts like a normal wrapper.
|
||||
Any syntaxes that are written in the codespan, codeblock, math, comment, and wikilink don't apply the rules, so they are treated according to their context. Or, in the nutshell, they aren't formatted. This corresponds to the many of markdown implementation, as well as that implemented in Obsidian.
|
||||
|
||||
<details>
|
||||
<img src="docs/assets/tidier-formatting.gif" alt="tidier-formatting.gif"/>
|
||||
</details>
|
||||
In addition in the editor, any formatting above doesn't overlapping its block context. So if opening delimiter, i.e. double plus signs for insertion, was found in heading, and didn't meet any double plus signs but in the next line (which is paragraph for instance), then the second delimiter isn't treated as a closing for that opening.
|
||||
|
||||
### 7. Other Tweaks
|
||||
Example below can give some better understanding:
|
||||
|
||||
- Enable/disable each formatting type individually under "Syntax switch" section.
|
||||
- Prevent fenced div from being styled in source mode.
|
||||
- Specifies when tags should be displayed under "Tag display behavior" section.
|
||||
- etc...
|
||||
|
||||
> [!Note]
|
||||
Markdown:
|
||||
> ```
|
||||
> # Heading ++1
|
||||
> The paragraph.++
|
||||
>
|
||||
> You don't need to reload the app after changing the settings.
|
||||
> Another ++paragraph
|
||||
> > Blockquote++
|
||||
>
|
||||
> 1. ++List
|
||||
> 2. another list++
|
||||
> 3. another ++list
|
||||
> lazy continuation++
|
||||
> ```
|
||||
|
||||
## 🎨 Styling Custom Spans and Fenced Divs
|
||||
Expected result:
|
||||
> <h1>Heading ++1</h1>
|
||||
> The paragraph.++
|
||||
>
|
||||
> Another ++paragraph
|
||||
> <blockquote>Blockquote++</blockquote>
|
||||
>
|
||||
> <ol><li>++List</li>
|
||||
> <li>another list++</li>
|
||||
> <li>another <u>list
|
||||
> <br>lazy continuation</u></li></ol>
|
||||
|
||||
With tag attached to them, you can insert your own CSS style rules based on their tag. For instance, you can apply this CSS rules to the markdown below:
|
||||
---
|
||||
|
||||
```css
|
||||
.text-large { font-size: 48px }
|
||||
.text-green { color: green }
|
||||
|
||||
.align-center { text-align: center }
|
||||
.bordered { border: 4px solid red }
|
||||
```
|
||||
|
||||
```markdown
|
||||
!!{text-large text-green}Large text with green color.!!
|
||||
|
||||
:::align-center bordered
|
||||
Should be aligned to the center
|
||||
and bordered.
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Expected result:</summary>
|
||||
<img src="docs/assets/tag-styling.png" alt="tag-styling.png"/>
|
||||
</details>
|
||||
|
||||
> [!Note]
|
||||
>
|
||||
> For which characters are allowed in the tag, see "Syntax Rules" section below.
|
||||
|
||||
## 📜 Syntax Rules
|
||||
|
||||
We move the explanation to [this page](./docs/rule.md).
|
||||
|
||||
## 📦 Installation
|
||||
|
||||
- In-app
|
||||
- Open settings.
|
||||
- Choose "Community plugins" setting tab.
|
||||
- Turn off "Restricted mode" if it was enabled before.
|
||||
- Click "Browse" at "Community plugins" item.
|
||||
- Type "Extended Markdown Syntax" in the search box.
|
||||
- Install and enable it.
|
||||
- Manual
|
||||
- Create a folder named `extended-markdown-parser` under `YOUR_VAULT_NAME/.obsidian/plugins`.
|
||||
- Place `manifest.json`, `main.js`, and `style.css` from the latest release into the folder.
|
||||
- Enable it through the "Community plugin" setting tab.
|
||||
- Using [BRAT](https://github.com/TfTHacker/obsidian42-brat)
|
||||
- Install and enable "BRAT" plugin in "Community plugins" list.
|
||||
- Choose "BRAT" setting tab.
|
||||
- Click "Add beta plugin" under "Beta plugin list" section.
|
||||
- Enter this repo link into "Repository" input field.
|
||||
- Check "Enable after installing the plugin" and click "Add Plugin".
|
||||
|
||||
## ❓ FAQ's
|
||||
|
||||
### Is it working on the table and callout?
|
||||
Of course it's working on both, except for the fenced div.
|
||||
|
||||
### Does the plugin work on large files?
|
||||
I tested it on a 250kB file and it's still working fine. This plugin also uses parser that implemented simple incremental and partial parsing, so you don't need to worry about facing with large files. But if you still have some issues with it, feel free to inform me what the issues you are struggling with.
|
||||
|
||||
### Will it cause conflict with other plugins that concerns extending syntax?
|
||||
It depends on what character the others use in their syntax.
|
||||
|
||||
## 📋 Roadmap
|
||||
## Features to be Implemented in The Future
|
||||
|
||||
- [x] Enable/disable formatting in settings
|
||||
- [x] Applicable on mobile
|
||||
- [x] ~~Fixing paragraph alignment bug~~ For now, use fenced div to customize block level format
|
||||
- [x] Customize highlighting colors
|
||||
- [ ] Customize formatting styles
|
||||
- [x] Applying syntax quickly using shortcuts and context menu
|
||||
- [x] ~~Class suggester~~ Predefined tags for custom span and fenced div
|
||||
- [x] ~~More syntax, if necessary~~ *I'll be over here for a while*
|
||||
- [ ] Fixing paragraph alignment bug
|
||||
- [ ] Customize formatting styles and highlighting colors
|
||||
- [ ] Applying syntax quickly using shortcuts and context menu
|
||||
- [ ] More syntaxes, if necessary
|
||||
|
||||
## ⚙️ Compatibility Note
|
||||
## Other Known Issues
|
||||
|
||||
This plugin have been tested in the latest version of Obsidian (about 1.7.x - 1.8.x), and haven't been tested yet in the version 1.6.x and below.
|
||||
|
||||
## 🐞 Known Issues
|
||||
|
||||
- Delimiter escaping doesn't work in the preview mode.
|
||||
- Cannot escape spoilers that are inside table cells (in source mode). (**cannot be fixed**)
|
||||
- Cannot escape spoilers that are inside table cells (in source mode)
|
||||
|
||||
Feel free to let me know if you find any bugs...
|
||||
|
||||
## 🙏 Acknowledgment
|
||||
## Credit
|
||||
|
||||
Thanks to:
|
||||
- [Pandoc](https://pandoc.org/MANUAL.html) for the idea of some syntax,
|
||||
- [Pandoc](https://pandoc.org/MANUAL.html) for the idea of superscript and subscript,
|
||||
- [CommonMark](https://spec.commonmark.org/) and [Github Flavored Markdown](https://github.github.com/gfm/) for the markdown specification.
|
||||
- [Discord](https://discord.com/) for the spoiler idea.
|
||||
- [Superschnizel](https://github.com/Superschnizel/obisdian-fast-text-color) for interactive menu idea,
|
||||
- [Exaroth](https://github.com/exaroth/mdx_custom_span_class) for custom span idea.
|
||||
- [Mara-Li](https://github.com/Mara-Li/obsidian-regex-mark) for some code snippets,
|
||||
- [marcel-goldammer](https://github.com/marcel-goldammer/obsidian-keyword-highlighter),
|
||||
- [attcoleanderson](https://github.com/mattcoleanderson/obsidian-dynamic-text-concealer)
|
||||
- [attcoleanderson](https://github.com/mattcoleanderson/obsidian-dynamic-text-concealer)
|
||||
|
Before Width: | Height: | Size: 4.6 MiB |
|
Before Width: | Height: | Size: 8.8 MiB |
|
Before Width: | Height: | Size: 8.5 MiB |
|
Before Width: | Height: | Size: 94 KiB |
|
Before Width: | Height: | Size: 5.7 MiB |
BIN
docs/media/custom-highlight.gif
Executable file
|
After Width: | Height: | Size: 1.6 MiB |
BIN
docs/media/formattings.gif
Executable file
|
After Width: | Height: | Size: 2.1 MiB |
145
docs/rule.md
|
|
@ -1,145 +0,0 @@
|
|||
## Syntax Rules for Extended Markdown Syntax
|
||||
|
||||
To make clear and avoid ambiguity, some rules are applied to the syntax.
|
||||
|
||||
#### 1. General Rules for All Syntax
|
||||
|
||||
- Any built-in syntax from Obsidian has a higher precedence than that in this plugin.
|
||||
|
||||
#### 2. General Rules for Inline Syntax (Insertion, Spoiler, Superscript, Subscript, Highlight Color Tag, Custom Span)
|
||||
|
||||
- Opening delimiter must not be followed by any whitespace character (regular space, tab, and new line), and the closing one must not be preceded by any whitespace character.
|
||||
- Delimiter must satisfy its requiered length as will be explained later, and must not be preceded or followed by the same character as the delimiter, or the same non-escaped if configured for that.
|
||||
- (Editor only, can be altered through the settings) Delimiter must not be escaped, if it was configured to not be escaped, by a backslash. Otherwise, it will act as literal character.
|
||||
- Formatting only occurs when opening delimiter met its closing (doesn't include highlight).
|
||||
- Content text, that is surrounded by delimiters, must at least one character.
|
||||
- Content text must not have two or more new line character.
|
||||
|
||||
For better understanding, the table below can give some example applying those rules:
|
||||
|
||||
| Valid | Invalid |
|
||||
| -------------------------------------------------- | ------------------------------------------ |
|
||||
| `++lorem++` | `++ lorem++` `++lorem` `lorem++` |
|
||||
| `++lor em++` | `++lorem ++` |
|
||||
| `++l++` | `++++` |
|
||||
| `++l+o+r+em++` | `++dfdf+++` |
|
||||
| `++lo++rem++` (third plus pair doesn't include) | `++lo\nre\nm++` (`\n` as a new line char) |
|
||||
| `++lo\nr e m++` | `+++lore++m+++` |
|
||||
| `++ lor++em++` (first one doesn't include) | `++ ++` |
|
||||
| `\+++lorem++ ++ipsum\+++` (if escaping is enabled) | `\++lorem++ +\++ipsum\++++` |
|
||||
|
||||
#### 3. Rules for Insertion and Spoiler
|
||||
|
||||
- Insertion is defined as text consist at least one character surrounded by exactly double plus signs (`++`) on each side.
|
||||
- Spoiler is the same as the insertion, it's just surrounded by exactly double bars (`||`) on each side.
|
||||
|
||||
| Valid format | Invalid format |
|
||||
| ---------------------------------------- | ------------------------------------------------------------------ |
|
||||
| `++ins++` `++ins also++` | `++ not ins++`, `++not ins` `++not ins ++` `+++not ins++` |
|
||||
| `\|\|spoiler\|\|` `\|\|spoiler also\|\|` | `\|\| not spoiler\|\|`, `\|\|not spoiler \|\|` `\|\|not spoiler\|` |
|
||||
|
||||
#### 4. Rules for Superscript and Subscript
|
||||
|
||||
- Superscript is defined as text consist at least one character surrounded by only single caret (`^`) on each side, and must not contain any of whitespace character.
|
||||
- Subscript act like superscript, it's just use single tilde as delimiter.
|
||||
- Thus, insertion and spoiler allow its content to have any whitespace character. It's contasts with the case of superscript and subscript.
|
||||
|
||||
| Valid format | Invalid format |
|
||||
| -------------------- | --------------------------------------------------------------- |
|
||||
| `^sup^` `^sup-also^` | `^ not-sup^`, `^not sup^`, `^not-sup` `^not-sup ^` `^^not-sup^` |
|
||||
| `~sub~` `~sub-also~` | `~ not-sub~`, `~not sub~`, `~not-sub` `~not-sub ~` `~not-sub~~` |
|
||||
|
||||
#### 5. Rules for Custom Span
|
||||
|
||||
- Custom span use the same rules as insertion and spoiler, except it uses double exclamation marks on each side to wrap its content.
|
||||
- It also comes with a tag you can insert a class or classes into. The tag consists alphanumeric character(s), hyphen(s), and space(s) (at least one character), wrapped by curly brackets on each side. A space is treated as a devider between classes. Other character inserted before or within the tag will disable it, so it will be stated as invalid tag.
|
||||
|
||||
| Valid tag | Invalid tag |
|
||||
| ------------------------- | ----------------------------------------------------------- |
|
||||
| `!!{my-class}span!!` | `!! {class}not span!!`, `!!a{class}span!!` |
|
||||
| `!!{class-1 cls-2}span!!` | `!!{}span!!,` `!!{*_}not span !!`, `!!{green_color}span!!` |
|
||||
|
||||
Later on, you can add corresponding class in your CSS snippet. For instance, if you type `!!{my-class strong-text}My text!!`, it will be rendered in HTML as `<span class="my-class strong-text">My text</span>` (with additional `cm-custom-span` class in the editor, and `custom-span` in the preview).
|
||||
|
||||
#### 6. Rules for Highlight Color Tag
|
||||
|
||||
The color of each highlight can be customized by adding color tag exactly after its opening delimiter. The tag behaves like the custom span tag, except it doesn't allow any space within it.
|
||||
|
||||
| Valid color tag | Invalid color tag |
|
||||
| -------------------------- | --------------------------------------------------------------------- |
|
||||
| `=={color}highlight==` | `== {color}highlight==`, `==a{color}highlight==` |
|
||||
| `=={abcAb--10}highlight==` | `=={ }highlight==,` `=={*_}highlight==`, `=={green_color}highlight==` |
|
||||
|
||||
Valid color tag will be added in the highlight classes, so `=={red}Red==` will be parsed in html tag as **`<span class="cm-custom-highlight cm-custom-highlight-red">`** in editor and **`<mark class="custom-highlight custom-highlight-red">`** in preview mode.
|
||||
|
||||
#### 7. NOTE: Delimiter Escaping
|
||||
|
||||
As already explained, using escaper backslash can change the semantic meaning to that being escaped. Escaped punctuation is treated as regular character that doesn't have functional use. This applies to those delimiters when "Delimiter escaping" option is switched on.
|
||||
|
||||
```
|
||||
++insertion++ \++not insertion\++ in editor mode
|
||||
```
|
||||
|
||||
However, we can only apply this feature in editor mode, since escaped character being rendered as normal character without being wrapped by any tag (and it makes sense).
|
||||
|
||||
```
|
||||
++insertion++ \++still insertion\++ \+\+still insertion\+\+, \+++not insertion+\++ in preview mode
|
||||
```
|
||||
|
||||
Due to this condition, "Delimiter escaping" was turned of by default to maintain consistency between editor and preview mode.
|
||||
|
||||
#### 8. Modified Pandoc-style Fenced Div
|
||||
|
||||
Since the align tag has been removed, as it is not as flexible as though, I decided to bring the plugin with Pandoc-style fenced div, which more flexible and intuitive for block customizing. Even though it is Pandoc-style, the implemented rule in this plugin is more restricted to avoid unnecessary broken layout and conflicts (or even worse).
|
||||
|
||||
- Fenced div must be opened by a line consists of three or more colons (`:::`), at must be located exactly in the line start.
|
||||
- The colons may, and only, be followed by tag that consists of alphanumeric, hyphen, and space (can be empty).
|
||||
- Other characters aren't allowed in the opening line. Even a colon that was inserted in the tag range will make the format invalid.
|
||||
- It is only be closed by different context (e.g. list, blockquote), blank line, or the end of the document. Therefore, closing the div with colons doesn't have any effect.
|
||||
- You don't need to close the format with any delimiter.
|
||||
|
||||
```
|
||||
:::
|
||||
valid fenced div
|
||||
|
||||
::: my-class
|
||||
also valid
|
||||
|
||||
:::my-class other-class
|
||||
also
|
||||
valid
|
||||
```
|
||||
|
||||
#### 9. Context-aware Formatting
|
||||
|
||||
Any syntax that are written in the codespan, codeblock, math, comment, and wikilink don't apply the rules, so they are treated according to their context. Or, in the nutshell, they aren't formatted. This corresponds to the many of markdown implementation, as well as that implemented in Obsidian.
|
||||
|
||||
In addition in the editor, any formatting above doesn't overlapping its block context. So if a opening delimiter, i.e. double plus signs for insertion, was found in heading, and didn't meet any double plus signs but in the next line (which is paragraph for instance), then the second delimiter isn't treated as a closing for that opening.
|
||||
|
||||
Example below can give some better understanding:
|
||||
|
||||
Markdown:
|
||||
> ```
|
||||
> # Heading ++1
|
||||
> The paragraph.++
|
||||
>
|
||||
> Another ++paragraph
|
||||
> > Blockquote++
|
||||
>
|
||||
> 1. ++List
|
||||
> 2. another list++
|
||||
> 3. another ++list
|
||||
> lazy continuation++
|
||||
> ```
|
||||
|
||||
Expected result:
|
||||
> <h1>Heading ++1</h1>
|
||||
> The paragraph.++
|
||||
>
|
||||
> Another ++paragraph
|
||||
> <blockquote>Blockquote++</blockquote>
|
||||
>
|
||||
> <ol><li>++List</li>
|
||||
> <li>another list++</li>
|
||||
> <li>another <ins>list
|
||||
> <br>lazy continuation</ins></li></ol>
|
||||
|
|
@ -9,47 +9,47 @@ import { FlatCompat } from "@eslint/eslintrc";
|
|||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const compat = new FlatCompat({
|
||||
baseDirectory: __dirname,
|
||||
recommendedConfig: js.configs.recommended,
|
||||
allConfig: js.configs.all
|
||||
baseDirectory: __dirname,
|
||||
recommendedConfig: js.configs.recommended,
|
||||
allConfig: js.configs.all
|
||||
});
|
||||
|
||||
export default [{
|
||||
ignores: [
|
||||
"**/node_modules/",
|
||||
"**/main.js",
|
||||
"**/.deprecated/",
|
||||
"**/esbuild.config.mjs",
|
||||
"**/eslint.config.mjs"
|
||||
],
|
||||
ignores: [
|
||||
"**/node_modules/",
|
||||
"**/main.js",
|
||||
"**/.deprecated/",
|
||||
"**/esbuild.config.mjs",
|
||||
"**/eslint.config.mjs"
|
||||
],
|
||||
}, ...compat.extends(
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/eslint-recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/eslint-recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
), {
|
||||
plugins: {
|
||||
"@typescript-eslint": typescriptEslint,
|
||||
},
|
||||
plugins: {
|
||||
"@typescript-eslint": typescriptEslint,
|
||||
},
|
||||
|
||||
languageOptions: {
|
||||
globals: {
|
||||
...globals.node,
|
||||
},
|
||||
languageOptions: {
|
||||
globals: {
|
||||
...globals.node,
|
||||
},
|
||||
|
||||
parser: tsParser,
|
||||
ecmaVersion: 6,
|
||||
sourceType: "module",
|
||||
},
|
||||
parser: tsParser,
|
||||
ecmaVersion: 6,
|
||||
sourceType: "module",
|
||||
},
|
||||
|
||||
rules: {
|
||||
"no-unused-vars": "off",
|
||||
"@typescript-eslint/no-unused-vars": ["error", {
|
||||
args: "none",
|
||||
}],
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"no-prototype-builtins": "off",
|
||||
"@typescript-eslint/no-empty-function": "off",
|
||||
"prefer-const": "off",
|
||||
"no-cond-assign": "off",
|
||||
},
|
||||
rules: {
|
||||
"no-unused-vars": "off",
|
||||
"@typescript-eslint/no-unused-vars": ["error", {
|
||||
args: "none",
|
||||
}],
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"no-prototype-builtins": "off",
|
||||
"@typescript-eslint/no-empty-function": "off",
|
||||
"prefer-const": "off",
|
||||
"no-cond-assign": "off",
|
||||
},
|
||||
}];
|
||||
122
main.ts
|
|
@ -1,88 +1,42 @@
|
|||
import { MarkdownView, Plugin, Command } from "obsidian";
|
||||
import { Plugin } from "obsidian";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
// import { drawSelection } from "@codemirror/view";
|
||||
import { parserField } from "src/editor-mode/state-fields";
|
||||
import { editorExtendedSyntax } from "src/editor-mode/extensions";
|
||||
import { PreviewExtendedSyntax } from "src/preview-mode/post-processor";
|
||||
import { PluginSettings } from "src/types";
|
||||
import { DEFAULT_SETTINGS } from "src/settings/configs";
|
||||
import { ExtendedSettingTab } from "src/settings/ui/setting-tab";
|
||||
import { configureDelimLookup } from "src/format-configs/format-utils"
|
||||
import { StyleSheetHandler } from "src/stylesheet";
|
||||
import { Theme } from "src/enums";
|
||||
import { editorCommands } from "src/editor-mode/formatting/commands";
|
||||
import { extendEditorCtxMenu } from "src/editor-mode/ui-components";
|
||||
import { TagManager } from "src/tag-manager";
|
||||
import { ReadingModeSyntaxExtender } from "src/preview-mode/post-processor/core";
|
||||
import { editorSyntaxExtender, refreshCall } from "src/editor-mode/cm-extensions";
|
||||
import { DEFAULT_SETTINGS } from "src/settings";
|
||||
import { SettingTab } from "src/settings/interface";
|
||||
import { settingsFacet } from "src/editor-mode/facets";
|
||||
import { configureDelimLookup } from "src/utils";
|
||||
|
||||
export default class ExtendedMarkdownSyntax extends Plugin {
|
||||
settings: PluginSettings;
|
||||
opacityHandler: StyleSheetHandler;
|
||||
tagManager: TagManager;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
this.addSettingTab(new ExtendedSettingTab(this.app, this));
|
||||
|
||||
configureDelimLookup(this.settings);
|
||||
this.registerEditorExtension(editorSyntaxExtender(this));
|
||||
this.registerMarkdownPostProcessor(new ReadingModeSyntaxExtender(this.settings).postProcess);
|
||||
|
||||
this.tagManager = new TagManager(this);
|
||||
this.opacityHandler = new StyleSheetHandler(this);
|
||||
this.opacityHandler.insert(`body.theme-light{--hl-opacity:${this.settings.lightModeHlOpacity}}`);
|
||||
this.opacityHandler.insert(`body.theme-dark{--hl-opacity:${this.settings.darkModeHlOpacity}}`);
|
||||
|
||||
this.registerCommands(editorCommands);
|
||||
this.extendEditorCtxMenu();
|
||||
|
||||
console.log("Load Extended Markdown Syntax");
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, structuredClone(DEFAULT_SETTINGS), await this.loadData());
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
|
||||
onunload(): void {
|
||||
this.tagManager.colorsHandler.destroy();
|
||||
this.opacityHandler.destroy();
|
||||
console.log("Unload Extended Markdown Syntax");
|
||||
}
|
||||
|
||||
reconfigureDelimLookup() {
|
||||
configureDelimLookup(this.settings);
|
||||
}
|
||||
|
||||
/** Get the settings change effect without reload the whole app. */
|
||||
refreshMarkdownView(deep = true) {
|
||||
this.app.workspace.getLeavesOfType("markdown").forEach(leaf => {
|
||||
if (leaf.view instanceof MarkdownView) {
|
||||
let cmView = leaf.view.editor.cm;
|
||||
cmView.dispatch({
|
||||
annotations: refreshCall.of({ deep }),
|
||||
});
|
||||
leaf.view.previewMode.rerender(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setHlOpacity(value: number, theme: Theme) {
|
||||
let selector = theme == Theme.LIGHT
|
||||
? "body.theme-light"
|
||||
: "body.theme-dark",
|
||||
property = "--hl-opacity",
|
||||
// The index of light mode opacity is 0, and dark mode opacity is 1.
|
||||
ruleIndex = theme;
|
||||
this.opacityHandler.replace(`${selector}{${property}:${value}}`, ruleIndex);
|
||||
}
|
||||
|
||||
registerCommands(commands: Command[]) {
|
||||
commands.forEach(cmd => this.addCommand(cmd));
|
||||
}
|
||||
|
||||
extendEditorCtxMenu() {
|
||||
this.registerEvent(this.app.workspace.on("editor-menu", (menu, editor, ctx) => {
|
||||
extendEditorCtxMenu(menu, editor, ctx);
|
||||
}));
|
||||
}
|
||||
settings: PluginSettings;
|
||||
areSettingsChanged: boolean = false;
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
this.addSettingTab(new SettingTab(this.app, this));
|
||||
configureDelimLookup(this.settings);
|
||||
this.registerEditorExtension([
|
||||
settingsFacet.of(this.settings),
|
||||
parserField,
|
||||
editorExtendedSyntax,
|
||||
EditorView.outerDecorations.of(view => view.plugin(editorExtendedSyntax)!.outerDecoSet)
|
||||
]);
|
||||
this.registerMarkdownPostProcessor(new PreviewExtendedSyntax(this.settings).postProcess);
|
||||
console.log("Load Extended Markdown Syntax");
|
||||
}
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
async saveSettings() {
|
||||
if (!this.areSettingsChanged) {
|
||||
this.areSettingsChanged = true;
|
||||
new Notice("You must restart the app to take the effect")
|
||||
}
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
onunload(): void {
|
||||
console.log("Unload Extended Markdown Syntax");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"id": "extended-markdown-syntax",
|
||||
"name": "Extended Markdown Syntax",
|
||||
"version": "2.0.11",
|
||||
"minAppVersion": "1.7.4",
|
||||
"description": "Extend your Markdown syntax using delimiters instead of HTML tags, such as underlining, superscript, subscript, highlighting, and spoiler.",
|
||||
"version": "2.0.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "Extend your markdown syntax using delimiters instead of HTML tags, such as underlining, superscript, subscript, highlighting, aligning.",
|
||||
"author": "Kotaindah55 (Sheva Ihza)",
|
||||
"isDesktopOnly": false
|
||||
}
|
||||
54
package.json
|
|
@ -1,37 +1,51 @@
|
|||
{
|
||||
"name": "extended-markdown-syntax",
|
||||
"version": "2.0.11",
|
||||
"description": "Extend your Markdown syntax using delimiters instead of HTML tags, such as underlining, superscript, subscript, highlighting, and spoiler.",
|
||||
"version": "2.0.0",
|
||||
"description": "Extend your markdown syntax using delimiters instead of HTML tags, such as underlining, superscript, subscript, highlighting, spoiler, and aligning.",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json package.json"
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
},
|
||||
"keywords": ["obsidian-plugin"],
|
||||
"keywords": [],
|
||||
"author": "kotaindah55",
|
||||
"license": "MIT",
|
||||
"license": "GPL-3.0-only",
|
||||
"dependencies": {
|
||||
"sortablejs": "^1.15.6"
|
||||
"@codemirror/collab": "^6.1.1",
|
||||
"@codemirror/language": "^6.10.2",
|
||||
"@codemirror/merge": "^6.6.7",
|
||||
"@codemirror/search": "^6.5.6",
|
||||
"@codemirror/state": "^6.4.1",
|
||||
"@codemirror/view": "^6.34.0",
|
||||
"@lezer/common": "^1.2.1",
|
||||
"@types/node": "^22.13.1",
|
||||
"@typescript-eslint/eslint-plugin": "^8.23.0",
|
||||
"@typescript-eslint/parser": "^8.23.0",
|
||||
"builtin-modules": "^4.0.0",
|
||||
"bun": "^1.2.2",
|
||||
"codemirror": "^6.0.1",
|
||||
"esbuild": "^0.24.0",
|
||||
"eslint": "^9.19.0",
|
||||
"jsdom": "^26.0.0",
|
||||
"lucide": "^0.303.0",
|
||||
"lucide-static": "^0.309.0",
|
||||
"obsidian": "latest",
|
||||
"punycode": "^2.3.1",
|
||||
"redirecter": "^0.0.1",
|
||||
"rollup": "^4.34.1",
|
||||
"tslib": "^2.8.1",
|
||||
"typescript": "^5.7.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@codemirror/language": "^6.11.0",
|
||||
"@codemirror/search": "^6.5.10",
|
||||
"@codemirror/state": "^6.5.2",
|
||||
"@codemirror/view": "^6.36.4",
|
||||
"@lezer/common": "^1.2.3",
|
||||
"@types/node": "^22.13.1",
|
||||
"@types/sortablejs": "^1.15.8",
|
||||
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
||||
"@typescript-eslint/parser": "^8.26.1",
|
||||
"builtin-modules": "^5.0.0",
|
||||
"esbuild": "^0.25.0",
|
||||
"lucide": "^0.483.0",
|
||||
"lucide-static": "^0.483.0",
|
||||
"@typescript-eslint/eslint-plugin": "^8.23.0",
|
||||
"@typescript-eslint/parser": "^8.23.0",
|
||||
"builtin-modules": "^4.0.0",
|
||||
"esbuild": "^0.24.0",
|
||||
"obsidian": "latest",
|
||||
"obsidian-typings": "^2.2.0",
|
||||
"tslib": "2.8.1",
|
||||
"typescript": "5.8.2",
|
||||
"sortablejs": "^1.15.6"
|
||||
"typescript": "5.7.3"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,234 +0,0 @@
|
|||
import ExtendedMarkdownSyntax from "main";
|
||||
import { editorLivePreviewField } from "obsidian";
|
||||
import { Annotation, EditorState, Extension, Facet, Prec, StateField, Transaction } from "@codemirror/state";
|
||||
import { EditorView, PluginValue, ViewPlugin, ViewUpdate } from "@codemirror/view";
|
||||
import { syntaxTree, syntaxTreeAvailable } from "@codemirror/language";
|
||||
import { IndexCache } from "src/types";
|
||||
import { EditorParser } from "src/editor-mode/preprocessor/parser";
|
||||
import { SelectionObserver } from "src/editor-mode/preprocessor/observer";
|
||||
import { DecorationBuilder } from "src/editor-mode/decorator/builder";
|
||||
import { Formatter } from "src/editor-mode/formatting/formatter";
|
||||
import { hookLangState } from "src/editor-mode/utils/hook";
|
||||
import { Tree } from "@lezer/common";
|
||||
|
||||
type TagMenuOptionCaches = Record<"colorMenuItem" | "spanTagMenuItem" | "divTagMenuItem", IndexCache>;
|
||||
|
||||
interface RefreshDesc {
|
||||
deep?: boolean;
|
||||
}
|
||||
|
||||
interface InternalInstances {
|
||||
mainPlugin: ExtendedMarkdownSyntax;
|
||||
parser: EditorParser;
|
||||
observer: SelectionObserver;
|
||||
builder: DecorationBuilder;
|
||||
formatter: Formatter;
|
||||
activities: ActivityRecord;
|
||||
}
|
||||
|
||||
export interface ActivityRecord {
|
||||
isParsing?: boolean;
|
||||
isObserving?: boolean;
|
||||
isSelectionMoved?: boolean;
|
||||
isRefreshed?: boolean;
|
||||
isDeepRefreshed?: boolean;
|
||||
isModeChanged?: boolean;
|
||||
}
|
||||
|
||||
function _isSelectionMoved(transaction: Transaction): boolean {
|
||||
return !(
|
||||
!transaction.selection ||
|
||||
transaction.startState.selection.eq(transaction.selection, false)
|
||||
);
|
||||
}
|
||||
|
||||
function _isEditorModeChanged(transaction: Transaction): boolean {
|
||||
let isLivePreviewCurrently = transaction.state.field(editorLivePreviewField),
|
||||
isLivePreviewPreviously = transaction.startState.field(editorLivePreviewField);
|
||||
return isLivePreviewCurrently != isLivePreviewPreviously;
|
||||
}
|
||||
|
||||
function _ensureMostUpdatedTree(transaction: Transaction): { tree: Tree, targetLen: number } {
|
||||
let {
|
||||
tree,
|
||||
treeLen: targetLen
|
||||
} = transaction.state.field(langStateField).context;
|
||||
|
||||
transaction.effects.forEach((effect) => {
|
||||
if (effect.is(langStateFxType))
|
||||
({ tree, treeLen: targetLen } = effect.value.context)
|
||||
});
|
||||
|
||||
return { tree, targetLen };
|
||||
}
|
||||
|
||||
class _EditorPlugin implements PluginValue {
|
||||
public readonly builder: DecorationBuilder;
|
||||
public readonly mainPlugin: ExtendedMarkdownSyntax;
|
||||
|
||||
public root: DocumentOrShadowRoot;
|
||||
public activities: ActivityRecord;
|
||||
|
||||
constructor(
|
||||
plugin: ExtendedMarkdownSyntax,
|
||||
view: EditorView,
|
||||
builder: DecorationBuilder,
|
||||
activities: ActivityRecord
|
||||
) {
|
||||
this.mainPlugin = plugin;
|
||||
this.root = view.root;
|
||||
this.builder = builder;
|
||||
this.activities = activities;
|
||||
this.builder.onViewInit(view);
|
||||
}
|
||||
|
||||
public update(update: ViewUpdate) {
|
||||
if (this.root !== update.view.root) {
|
||||
this._onRootChanged(update.view.root);
|
||||
}
|
||||
this.builder.onViewUpdate(update, this.activities);
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
this.mainPlugin.tagManager.colorsHandler.eject(this.root);
|
||||
this.mainPlugin.opacityHandler.eject(this.root);
|
||||
}
|
||||
|
||||
private _onRootChanged(newRoot: DocumentOrShadowRoot): void {
|
||||
this.mainPlugin.tagManager.colorsHandler.eject(this.root);
|
||||
this.mainPlugin.opacityHandler.eject(this.root);
|
||||
this.mainPlugin.tagManager.colorsHandler.inject(newRoot);
|
||||
this.mainPlugin.opacityHandler.inject(newRoot);
|
||||
this.root = newRoot;
|
||||
}
|
||||
}
|
||||
|
||||
export const refreshCall = Annotation.define<RefreshDesc>();
|
||||
|
||||
export const instancesStore = Facet.define<ExtendedMarkdownSyntax, InternalInstances>({
|
||||
combine(value) {
|
||||
if (!value.length) return undefined as unknown as InternalInstances;
|
||||
let mainPlugin = value[0],
|
||||
parser = new EditorParser(mainPlugin.settings),
|
||||
observer = new SelectionObserver(parser),
|
||||
builder = new DecorationBuilder(parser, observer),
|
||||
formatter = new Formatter(parser, observer);
|
||||
return { mainPlugin, parser, observer, builder, formatter, activities: {} }
|
||||
},
|
||||
get static() {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
export const tagMenuOptionCaches = Facet.define<TagMenuOptionCaches, TagMenuOptionCaches>({
|
||||
combine(value) {
|
||||
return value[0];
|
||||
},
|
||||
get static() {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
export const { langStateField, langStateFxType } = hookLangState();
|
||||
|
||||
export const editorSyntaxExtender = (plugin: ExtendedMarkdownSyntax): Extension => {
|
||||
let preprocessField = StateField.define({
|
||||
create(state: EditorState) {
|
||||
let { parser, observer, builder, activities } = state.facet(instancesStore);
|
||||
parser.initParse(state.doc, syntaxTree(state));
|
||||
observer.observe(state.selection, true);
|
||||
builder.onStateInit(state);
|
||||
return {
|
||||
parser, observer, builder, activities,
|
||||
lineBreaksSet: builder.holder.lineBreaksSet,
|
||||
blockOmittedSet: builder.holder.blockOmittedSet
|
||||
}
|
||||
},
|
||||
|
||||
update(prevField, transaction: Transaction) {
|
||||
let { tree: newTree, targetLen: stopAt } = _ensureMostUpdatedTree(transaction),
|
||||
{ parser, observer, builder, activities } = prevField,
|
||||
completedTree = syntaxTreeAvailable(transaction.state, stopAt),
|
||||
refreshDesc = transaction.annotation(refreshCall);
|
||||
|
||||
let isParsing = false,
|
||||
isObserving = false,
|
||||
isRefreshed = !!refreshDesc,
|
||||
isDeepRefreshed = !!refreshDesc?.deep,
|
||||
isModeChanged = _isEditorModeChanged(transaction);
|
||||
|
||||
if (transaction.docChanged) parser.storeChanges(transaction.changes);
|
||||
|
||||
if (isDeepRefreshed) {
|
||||
parser.initParse(transaction.newDoc, newTree);
|
||||
isParsing = true;
|
||||
}
|
||||
|
||||
else if (
|
||||
(parser.hasStoredChanges() || parser.lastStop != stopAt) &&
|
||||
completedTree
|
||||
) {
|
||||
parser.applyChange(transaction.newDoc, newTree, stopAt);
|
||||
isParsing = true;
|
||||
}
|
||||
|
||||
if (isDeepRefreshed || isModeChanged) {
|
||||
observer.restartObserver(transaction.newSelection, transaction.docChanged);
|
||||
isObserving = true;
|
||||
} else if (isParsing || _isSelectionMoved(transaction)) {
|
||||
observer.observe(transaction.newSelection, activities.isParsing ?? false);
|
||||
isObserving = true;
|
||||
}
|
||||
|
||||
builder.onStateUpdate(transaction, { isParsing, isObserving, isDeepRefreshed, isModeChanged });
|
||||
|
||||
activities.isParsing ||= isParsing;
|
||||
activities.isObserving ||= isObserving;
|
||||
activities.isRefreshed ||= isRefreshed;
|
||||
activities.isModeChanged ||= isModeChanged;
|
||||
|
||||
if (
|
||||
prevField.lineBreaksSet === builder.holder.lineBreaksSet &&
|
||||
prevField.blockOmittedSet === builder.holder.blockOmittedSet
|
||||
) return prevField;
|
||||
|
||||
return {
|
||||
parser, observer, builder, activities,
|
||||
lineBreaksSet: builder.holder.lineBreaksSet,
|
||||
blockOmittedSet: builder.holder.blockOmittedSet
|
||||
};
|
||||
},
|
||||
|
||||
provide(field): Extension {
|
||||
return [
|
||||
EditorView.decorations.from(field, sets => sets.blockOmittedSet),
|
||||
Prec.lowest(EditorView.decorations.from(field, sets => sets.lineBreaksSet))
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
let viewPlugin = ViewPlugin.define(
|
||||
view => {
|
||||
let { builder, activities } = view.state.facet(instancesStore);
|
||||
return new _EditorPlugin(plugin, view, builder, activities);
|
||||
},
|
||||
{
|
||||
provide: () => {
|
||||
return [
|
||||
tagMenuOptionCaches.of({
|
||||
colorMenuItem: { number: 0 },
|
||||
spanTagMenuItem: { number: 0 },
|
||||
divTagMenuItem: { number: 0 },
|
||||
}),
|
||||
EditorView.decorations.of(view => view.state.facet(instancesStore).builder.holder.blockSet),
|
||||
EditorView.decorations.of(view => view.state.facet(instancesStore).builder.holder.inlineOmittedSet),
|
||||
EditorView.decorations.of(view => view.state.facet(instancesStore).builder.holder.colorBtnSet),
|
||||
EditorView.decorations.of(view => view.state.facet(instancesStore).builder.holder.revealedSpoilerSet),
|
||||
EditorView.outerDecorations.of(view => view.state.facet(instancesStore).builder.holder.inlineSet),
|
||||
];
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return [instancesStore.of(plugin), preprocessField, viewPlugin];
|
||||
}
|
||||
|
|
@ -1,427 +0,0 @@
|
|||
import { ChangeDesc, ChangeSet, EditorState, Range, RangeSet, RangeValue, Text, Transaction } from "@codemirror/state";
|
||||
import { Decoration, DecorationSet, EditorView, ViewUpdate } from "@codemirror/view";
|
||||
import { editorLivePreviewField } from "obsidian";
|
||||
import { DisplayBehaviour, Format, MarkdownViewMode, TokenLevel, TokenStatus } from "src/enums";
|
||||
import { BlockFormat, PlainRange, InlineFormat, TokenGroup, TokenDecoration, PluginSettings, Token } from "src/types";
|
||||
import { BlockRules, InlineRules } from "src/format-configs/rules";
|
||||
import { trimTag } from "src/format-configs/format-utils";
|
||||
import { EditorParser } from "src/editor-mode/preprocessor/parser";
|
||||
import { SelectionObserver } from "src/editor-mode/preprocessor/observer";
|
||||
import { ActivityRecord } from "src/editor-mode/cm-extensions";
|
||||
import { LineBreak, HiddenWidget, ColorButton } from "src/editor-mode/decorator/widgets";
|
||||
import { REVEALED_SPOILER_DECO } from "src/editor-mode/decorator/decorations";
|
||||
import { getTagRange, iterTokenGroup, provideTokenPartsRanges } from "src/editor-mode/utils/token-utils"
|
||||
import { TextCursor } from "src/editor-mode/utils/doc-utils";
|
||||
|
||||
interface RangeSetUpdate<T extends RangeValue> {
|
||||
add?: readonly Range<T>[];
|
||||
sort?: boolean;
|
||||
filter?: (from: number, to: number, value: T) => boolean;
|
||||
filterFrom?: number;
|
||||
filterTo?: number;
|
||||
}
|
||||
|
||||
function _createInlineDecoRange(token: Token, cls: string) {
|
||||
return (Decoration
|
||||
.mark({ class: cls, token }) as TokenDecoration)
|
||||
.range(token.from, token.to);
|
||||
}
|
||||
|
||||
class _DecorationHolder {
|
||||
public inlineSet: DecorationSet = RangeSet.empty;
|
||||
public blockSet: DecorationSet = RangeSet.empty;
|
||||
public inlineOmittedSet: DecorationSet = RangeSet.empty;
|
||||
public blockOmittedSet: DecorationSet = RangeSet.empty;
|
||||
public colorBtnSet: DecorationSet = RangeSet.empty;
|
||||
public revealedSpoilerSet: DecorationSet = RangeSet.empty;
|
||||
public lineBreaksSet: DecorationSet = RangeSet.empty;
|
||||
}
|
||||
|
||||
class _DelimOmitter {
|
||||
private readonly _selectionObserver: SelectionObserver;
|
||||
private readonly _settings: PluginSettings;
|
||||
|
||||
constructor(settings: PluginSettings, selectionObserver: SelectionObserver) {
|
||||
this._settings = settings;
|
||||
this._selectionObserver = selectionObserver;
|
||||
}
|
||||
|
||||
public omitBlock(omittedSet?: DecorationSet, changes?: ChangeDesc): DecorationSet {
|
||||
let omittedRanges: Range<Decoration>[] = [],
|
||||
filterRegion = this._selectionObserver.filterRegions[TokenLevel.BLOCK];
|
||||
|
||||
this._selectionObserver.iterateChangedRegion(TokenLevel.BLOCK, (token, _, __, inSelection) => {
|
||||
if (inSelection || token.status != TokenStatus.ACTIVE || !token.validTag) return;
|
||||
let openFrom = token.from,
|
||||
openTo = openFrom + token.openLen + token.tagLen;
|
||||
if (token.to > openTo) openTo++;
|
||||
omittedRanges.push(HiddenWidget.of(openFrom, openTo, token, true));
|
||||
});
|
||||
|
||||
if (!omittedSet?.size) {
|
||||
omittedSet = Decoration.set(omittedRanges);
|
||||
} else {
|
||||
if (changes)
|
||||
omittedSet = omittedSet.map(changes);
|
||||
|
||||
for (let i = 0; i < filterRegion.length; i++) {
|
||||
let filterRange = filterRegion[i];
|
||||
omittedSet = omittedSet.update({
|
||||
filterFrom: filterRange.from,
|
||||
filterTo: filterRange.to,
|
||||
filter: () => false,
|
||||
});
|
||||
}
|
||||
omittedSet = omittedSet.update({ add: omittedRanges });
|
||||
}
|
||||
|
||||
return omittedSet;
|
||||
}
|
||||
|
||||
public omitInline(activeTokens: TokenGroup): DecorationSet {
|
||||
let alwaysShowHlTag = this._settings.hlTagDisplayBehaviour & DisplayBehaviour.ALWAYS,
|
||||
alwaysShowSpanTag = this._settings.spanTagDisplayBehaviour & DisplayBehaviour.ALWAYS,
|
||||
showHlTagIfTouched = this._settings.hlTagDisplayBehaviour & DisplayBehaviour.TAG_TOUCHED,
|
||||
showSpanTagIfTouched = this._settings.spanTagDisplayBehaviour & DisplayBehaviour.TAG_TOUCHED;
|
||||
|
||||
let omittedRanges: Range<Decoration>[] = [];
|
||||
|
||||
for (let i = 0; i < activeTokens.length; i++) {
|
||||
let token = activeTokens[i],
|
||||
openFrom = token.from,
|
||||
openTo = openFrom + token.openLen,
|
||||
tagTo = openTo + token.tagLen;
|
||||
if (this._selectionObserver.touchSelection(token.from, token.to)) {
|
||||
if (
|
||||
token.validTag && !this._selectionObserver.touchSelection(openTo, tagTo) &&
|
||||
(token.type == Format.HIGHLIGHT && showHlTagIfTouched || token.type == Format.CUSTOM_SPAN && showSpanTagIfTouched)
|
||||
) {
|
||||
omittedRanges.push(HiddenWidget.of(openTo, tagTo, token));
|
||||
}
|
||||
} else {
|
||||
if (token.type == Format.HIGHLIGHT && !alwaysShowHlTag || token.type == Format.CUSTOM_SPAN && !alwaysShowSpanTag) {
|
||||
openTo = tagTo;
|
||||
}
|
||||
omittedRanges.push(HiddenWidget.of(openFrom, openTo, token));
|
||||
if (token.closeLen) {
|
||||
omittedRanges.push(HiddenWidget.of(token.to - token.closeLen, token.to, token));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Decoration.set(omittedRanges, true);
|
||||
}
|
||||
}
|
||||
|
||||
class _LineBreakReplacer {
|
||||
public readonly parser: EditorParser;
|
||||
public lineBreakSet: RangeSet<Decoration> = RangeSet.empty;
|
||||
|
||||
constructor(parser: EditorParser) {
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
public replace(doc: Text, changes?: ChangeSet): DecorationSet {
|
||||
let reparsedRange = this.parser.reparsedRanges[TokenLevel.BLOCK],
|
||||
blockTokens = this.parser.blockTokens,
|
||||
updateSpec: RangeSetUpdate<Decoration> = {
|
||||
add: this._produceWidgetRanges(doc)
|
||||
};
|
||||
|
||||
if (!blockTokens.length)
|
||||
return this.lineBreakSet = RangeSet.empty;
|
||||
|
||||
if (reparsedRange.from != reparsedRange.initTo || reparsedRange.from != reparsedRange.changedTo) {
|
||||
updateSpec.filterFrom = Math.min(blockTokens[reparsedRange.from]?.from ?? this.parser.lastStreamPoint.from, this.parser.lastStreamPoint.from);
|
||||
updateSpec.filterTo = this.parser.lastStreamPoint.to;
|
||||
updateSpec.filter = () => false;
|
||||
}
|
||||
|
||||
if (changes) this.lineBreakSet = this.lineBreakSet.map(changes);
|
||||
return this.lineBreakSet = this.lineBreakSet.update(updateSpec);
|
||||
}
|
||||
|
||||
private _getReparsedBlockTokens(): TokenGroup {
|
||||
let range = this.parser.reparsedRanges[TokenLevel.BLOCK];
|
||||
return this.parser.blockTokens.slice(range.from, range.changedTo);
|
||||
}
|
||||
|
||||
private _produceWidgetRanges(doc: Text): Range<Decoration>[] {
|
||||
let tokens = this._getReparsedBlockTokens(),
|
||||
ranges: Range<Decoration>[] = [];
|
||||
if (!tokens.length) { return ranges }
|
||||
|
||||
let tokenIndex = 0,
|
||||
textCursor = TextCursor.atOffset(doc, tokens[0].from);
|
||||
|
||||
do {
|
||||
let curToken = tokens[tokenIndex],
|
||||
{ curLine } = textCursor;
|
||||
if (!curToken) break;
|
||||
if (curToken.status != TokenStatus.ACTIVE) { tokenIndex++; continue }
|
||||
if (
|
||||
curLine.from == curToken.from ||
|
||||
curLine.from - 1 == curToken.from + curToken.openLen + curToken.tagLen
|
||||
) continue;
|
||||
if (
|
||||
curLine.from >= curToken.to ||
|
||||
curLine.to < curToken.from ||
|
||||
curLine.to == curToken.to && curToken.closedByBlankLine
|
||||
) { tokenIndex++; continue }
|
||||
ranges.push(LineBreak.of(curLine.from));
|
||||
} while (textCursor.next());
|
||||
|
||||
return ranges;
|
||||
}
|
||||
}
|
||||
|
||||
class _TokensBuffer {
|
||||
public activeTokens: TokenGroup = [];
|
||||
public hlTokens: TokenGroup = [];
|
||||
public spoilerTokens: TokenGroup = [];
|
||||
|
||||
public catch(activeTokens: TokenGroup, hlTokens: TokenGroup, spoilerTokens: TokenGroup): void {
|
||||
this.activeTokens = activeTokens;
|
||||
this.hlTokens = hlTokens;
|
||||
this.spoilerTokens = spoilerTokens;
|
||||
}
|
||||
|
||||
public empty(): void {
|
||||
this.activeTokens = [];
|
||||
this.hlTokens = [];
|
||||
this.spoilerTokens = [];
|
||||
}
|
||||
}
|
||||
|
||||
export class DecorationBuilder {
|
||||
private readonly _parser: EditorParser;
|
||||
private readonly _omitter: _DelimOmitter;
|
||||
private readonly _catcher: _TokensBuffer;
|
||||
private readonly _lineBreakReplacer: _LineBreakReplacer;
|
||||
private readonly _selectionObserver: SelectionObserver;
|
||||
private readonly _settings: PluginSettings;
|
||||
|
||||
public readonly holder: _DecorationHolder;
|
||||
|
||||
constructor(parser: EditorParser, selectionObserver: SelectionObserver) {
|
||||
this._parser = parser;
|
||||
this._selectionObserver = selectionObserver;
|
||||
this._settings = parser.settings;
|
||||
this._omitter = new _DelimOmitter(this._settings, selectionObserver);
|
||||
this._catcher = new _TokensBuffer();
|
||||
this._lineBreakReplacer = new _LineBreakReplacer(parser);
|
||||
this.holder = new _DecorationHolder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Main decorations hold basic formatting style of the tokens.
|
||||
*
|
||||
* Intended to build non-height-altering decorations. So, it doesn't
|
||||
* include line breaks and fenced div opening omitter. (It runs only in
|
||||
* the view update)
|
||||
*/
|
||||
public buildMain(view: EditorView, state: EditorState, noStyledFencedDiv: boolean): void {
|
||||
let { visibleRanges } = view,
|
||||
visibleText = state.sliceDoc(
|
||||
visibleRanges[0]?.from ?? 0,
|
||||
visibleRanges[visibleRanges.length - 1]?.to ?? 0
|
||||
);
|
||||
if (!visibleRanges.length) visibleRanges = [{ from: 0, to: 0 }];
|
||||
this._buildInline(visibleRanges, visibleText);
|
||||
this._buildBlock(visibleRanges, visibleText, noStyledFencedDiv);
|
||||
}
|
||||
|
||||
/**
|
||||
* Supplementary decorations consist omitted delimiter of inline tokens,
|
||||
* color buttons for the highlight, and revealed spoiler when touched the
|
||||
* cursor or selection.
|
||||
*
|
||||
* Intended to build non-height-altering decorations. So, it doesn't
|
||||
* include line breaks and fenced div opening omitter. (It runs only in
|
||||
* the view update)
|
||||
*/
|
||||
public buildSupplementary(isLivePreview: boolean): void {
|
||||
if (isLivePreview) {
|
||||
this._omitInlineDelim(this._catcher.activeTokens);
|
||||
this._createColorBtnWidgets(this._catcher.hlTokens);
|
||||
this._revealSpoiler(this._catcher.spoilerTokens);
|
||||
} else {
|
||||
this.holder.colorBtnSet = this.holder.revealedSpoilerSet = RangeSet.empty;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs once on editor intialization, should be inside the view update
|
||||
* (i.e. the ViewPlugin update).
|
||||
*/
|
||||
public onViewInit(view: EditorView): void {
|
||||
let state = view.state,
|
||||
isLivePreview = state.field(editorLivePreviewField),
|
||||
noStyledFencedDiv = !isLivePreview && this._settings.noStyledDivInSourceMode;
|
||||
this.buildMain(view, state, noStyledFencedDiv);
|
||||
this.buildSupplementary(isLivePreview);
|
||||
}
|
||||
|
||||
public onViewUpdate(update: ViewUpdate, activities: ActivityRecord): void {
|
||||
let state = update.state,
|
||||
view = update.view,
|
||||
isLivePreview = state.field(editorLivePreviewField),
|
||||
noStyledFencedDiv = !isLivePreview && this._settings.noStyledDivInSourceMode;
|
||||
|
||||
if (activities.isParsing || activities.isRefreshed || (activities.isModeChanged && this._settings.noStyledDivInSourceMode) || update.viewportMoved) {
|
||||
this.buildMain(view, state, noStyledFencedDiv);
|
||||
}
|
||||
|
||||
if (activities.isObserving || activities.isRefreshed || update.viewportMoved) {
|
||||
this.buildSupplementary(isLivePreview);
|
||||
}
|
||||
|
||||
activities.isParsing =
|
||||
activities.isObserving =
|
||||
activities.isRefreshed =
|
||||
activities.isModeChanged = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs once on editor intialization, should be inside the state update
|
||||
* (i.e. the StateField update).
|
||||
*/
|
||||
public onStateInit(state: EditorState): void {
|
||||
let isLivePreview = state.field(editorLivePreviewField);
|
||||
if (isLivePreview) {
|
||||
this._omitFencedDivOpening();
|
||||
}
|
||||
this._replaceLineBreaks(state.doc);
|
||||
}
|
||||
|
||||
public onStateUpdate(transaction: Transaction, activities: ActivityRecord): void {
|
||||
if (activities.isParsing)
|
||||
this._replaceLineBreaks(transaction.newDoc, transaction.changes);
|
||||
|
||||
if (activities.isModeChanged || activities.isDeepRefreshed)
|
||||
this.holder.blockOmittedSet = RangeSet.empty;
|
||||
|
||||
if (!transaction.state.field(editorLivePreviewField)) {
|
||||
this._removeOmitter();
|
||||
} else if (activities.isObserving || activities.isModeChanged) {
|
||||
this._omitFencedDivOpening(transaction.changes);
|
||||
}
|
||||
}
|
||||
|
||||
private _buildInline(visibleRanges: readonly PlainRange[], visibleText: string): DecorationSet {
|
||||
let inlineDecoRanges: Range<TokenDecoration>[] = [],
|
||||
activeTokens: TokenGroup = [],
|
||||
hlTokens: TokenGroup = [],
|
||||
spoilerTokens: TokenGroup = [],
|
||||
viewportStart = visibleRanges[0].from;
|
||||
|
||||
iterTokenGroup({
|
||||
tokens: this._parser.inlineTokens,
|
||||
ranges: visibleRanges,
|
||||
callback: token => {
|
||||
if (token.status != TokenStatus.ACTIVE) return;
|
||||
if (token.type == Format.HIGHLIGHT) hlTokens.push(token);
|
||||
if (token.type == Format.SPOILER) spoilerTokens.push(token);
|
||||
activeTokens.push(token);
|
||||
let cls = "cm-" + InlineRules[token.type as InlineFormat].class;
|
||||
if (token.tagLen) {
|
||||
let tagRange = getTagRange(token),
|
||||
tagStr = visibleText.slice(tagRange.from - viewportStart + 1, tagRange.to - viewportStart - 1);
|
||||
if (token.type == Format.CUSTOM_SPAN) {
|
||||
tagStr = trimTag(tagStr);
|
||||
cls += " " + tagStr;
|
||||
} else {
|
||||
cls += " " + cls + "-" + tagStr;
|
||||
}
|
||||
}
|
||||
inlineDecoRanges.push(_createInlineDecoRange(token, cls));
|
||||
},
|
||||
});
|
||||
|
||||
this._catcher.catch(activeTokens, hlTokens, spoilerTokens);
|
||||
return this.holder.inlineSet = Decoration.set(inlineDecoRanges);
|
||||
}
|
||||
|
||||
private _buildBlock(visibleRanges: readonly PlainRange[], visibleText: string, noStyle: boolean): DecorationSet {
|
||||
let lineDecoRanges: Range<TokenDecoration>[] = [],
|
||||
viewportStart = visibleRanges[0].from;
|
||||
|
||||
iterTokenGroup({
|
||||
tokens: this._parser.blockTokens,
|
||||
ranges: visibleRanges,
|
||||
callback: token => {
|
||||
if (token.status != TokenStatus.ACTIVE) return;
|
||||
|
||||
let baseCls = "cm-" + BlockRules[token.type as BlockFormat].class,
|
||||
{ contentRange, tagRange } = provideTokenPartsRanges(token),
|
||||
tagStr = trimTag(visibleText.slice(tagRange.from - viewportStart, tagRange.to - viewportStart)),
|
||||
openDelimCls = baseCls + " cm-fenced-div-start",
|
||||
contentCls = baseCls + (noStyle ? "" : " " + tagStr),
|
||||
hasContent = !!visibleText
|
||||
.slice(contentRange.from - viewportStart, contentRange.to - viewportStart)
|
||||
.trimEnd();
|
||||
|
||||
lineDecoRanges.push(
|
||||
(Decoration.line({ class: openDelimCls, token }) as TokenDecoration)
|
||||
.range(token.from)
|
||||
);
|
||||
|
||||
if (hasContent) {
|
||||
lineDecoRanges.push(
|
||||
(Decoration.line({ class: contentCls, token }) as TokenDecoration)
|
||||
.range(contentRange.from + 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return this.holder.blockSet = Decoration.set(lineDecoRanges);
|
||||
}
|
||||
|
||||
private _createColorBtnWidgets(hlTokens: TokenGroup): DecorationSet {
|
||||
if (!this._settings.colorButton)
|
||||
return this.holder.colorBtnSet = RangeSet.empty;
|
||||
|
||||
let btnWidgets: Range<Decoration>[] = [];
|
||||
for (let i = 0; i < hlTokens.length; i++) {
|
||||
let token = hlTokens[i];
|
||||
if (this._selectionObserver.touchSelection(token.from, token.to)) {
|
||||
btnWidgets.push(ColorButton.of(token));
|
||||
}
|
||||
}
|
||||
return this.holder.colorBtnSet = Decoration.set(btnWidgets);
|
||||
}
|
||||
|
||||
private _revealSpoiler(spoilerTokens: TokenGroup): DecorationSet {
|
||||
let revealedRanges: Range<Decoration>[] = [];
|
||||
for (let i = 0; i < spoilerTokens.length; i++) {
|
||||
let token = spoilerTokens[i];
|
||||
if (this._selectionObserver.touchSelection(token.from, token.to)) {
|
||||
revealedRanges.push(REVEALED_SPOILER_DECO.range(token.from, token.to));
|
||||
}
|
||||
}
|
||||
return this.holder.revealedSpoilerSet = Decoration.set(revealedRanges);
|
||||
}
|
||||
|
||||
private _omitInlineDelim(activeTokens: TokenGroup): DecorationSet {
|
||||
return this.holder.inlineOmittedSet = this._omitter.omitInline(activeTokens);
|
||||
}
|
||||
|
||||
/** Executed only in the state update. */
|
||||
private _replaceLineBreaks(doc: Text, changes?: ChangeSet): DecorationSet {
|
||||
return this.holder.lineBreaksSet = this._lineBreakReplacer.replace(doc, changes);
|
||||
}
|
||||
|
||||
/** Executed only in the state update. */
|
||||
private _omitFencedDivOpening(changes?: ChangeDesc): DecorationSet {
|
||||
let isOmitted = !(this._settings.alwaysShowFencedDivTag & MarkdownViewMode.EDITOR_MODE);
|
||||
if (!isOmitted) {
|
||||
return this.holder.blockOmittedSet = RangeSet.empty;
|
||||
}
|
||||
return this.holder.blockOmittedSet = this._omitter.omitBlock(this.holder.blockOmittedSet, changes);
|
||||
}
|
||||
|
||||
private _removeOmitter(): void {
|
||||
this.holder.inlineOmittedSet = this.holder.blockOmittedSet = RangeSet.empty;
|
||||
}
|
||||
}
|
||||
108
src/editor-mode/decorator/builder/DecorationBuilder.ts
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
import { EditorState, Range } from "@codemirror/state";
|
||||
import { Decoration, DecorationSet, ViewUpdate } from "@codemirror/view";
|
||||
import { Parser } from "src/editor-mode/parser";
|
||||
import { Format, TokenRole, TokenStatus } from "src/enums";
|
||||
import { createHlDeco } from "src/editor-mode/decorator/utils";
|
||||
import { AlignDeco, AlignMarkDeco, ColorTagDeco, ContentDeco, DelimDeco, RevealedSpoiler } from "src/editor-mode/decorator/decorations";
|
||||
import { AlignFormat, MainFormat } from "src/types";
|
||||
import { ColorButton } from "src/editor-mode/decorator/widgets";
|
||||
|
||||
/** Decoration builder (excludes omitted delimiter), should be attached to `ExtendedSyntax` */
|
||||
export class DecorationBuilder {
|
||||
/** Attached `Parser` for obtaining tokens quickly */
|
||||
parser: Parser;
|
||||
constructor() {
|
||||
}
|
||||
/**
|
||||
* Produces _necessary_ decoration ranges from tokens provided by
|
||||
* `Builder.parser`. Therefore, it doesn't reproduce new ranges
|
||||
* from reused tokens. Doesn't include supplementary and omitted
|
||||
* delimiter.
|
||||
*/
|
||||
build(state: EditorState) {
|
||||
let doc = state.doc,
|
||||
tokens = this.parser.tokens,
|
||||
mainDecoRanges: Range<Decoration>[] = [],
|
||||
outerDecoRanges: Range<Decoration>[] = [],
|
||||
delimiterRanges: Range<Decoration>[] = [],
|
||||
{ startToken, endToken } = this.parser.lastParsed;
|
||||
for (
|
||||
let i = startToken, token = tokens[i];
|
||||
i < endToken;
|
||||
token = tokens[++i]
|
||||
) {
|
||||
if (token.status != TokenStatus.ACTIVE) { continue }
|
||||
if (token.role == TokenRole.OPEN) {
|
||||
if (token.type == Format.HIGHLIGHT) {
|
||||
let color = "",
|
||||
mayBeColorTag = tokens[i + 2],
|
||||
close = tokens[i + token.size - 1],
|
||||
hasColorTag = mayBeColorTag?.type == Format.HIGHLIGHT_COLOR_TAG;
|
||||
// gets color tag
|
||||
if (hasColorTag) {
|
||||
color = doc.sliceString(mayBeColorTag.from + 1, mayBeColorTag.to - 1);
|
||||
i++;
|
||||
}
|
||||
let hlDeco = createHlDeco(color, { openLen: token.to - token.from });
|
||||
// highlight should be pushed in outer decorations
|
||||
outerDecoRanges.push(hlDeco.range(token.from, close.to));
|
||||
if (hasColorTag) {
|
||||
delimiterRanges.push(ColorTagDeco.range(mayBeColorTag.from, mayBeColorTag.to));
|
||||
}
|
||||
} else {
|
||||
let content = tokens[++i];
|
||||
delimiterRanges.push(DelimDeco[token.type as MainFormat].range(token.from, token.to));
|
||||
if (token.type == Format.SPOILER) { // spoiler should be pushed in outer decorations
|
||||
outerDecoRanges.push(ContentDeco[token.type as MainFormat].range(content.from, content.to));
|
||||
} else {
|
||||
mainDecoRanges.push(ContentDeco[token.type as MainFormat].range(content.from, content.to));
|
||||
}
|
||||
}
|
||||
} else if (token.role == TokenRole.CLOSE && token.type != Format.HIGHLIGHT) {
|
||||
delimiterRanges.push(DelimDeco[token.type as MainFormat].range(token.from, token.to));
|
||||
} else if (token.role == TokenRole.SINGLE) {
|
||||
delimiterRanges.push(AlignMarkDeco[token.type as AlignFormat].range(token.from, token.to));
|
||||
mainDecoRanges.push(AlignDeco[token.type as AlignFormat].range(token.from, token.from));
|
||||
}
|
||||
}
|
||||
return { mainDecoRanges, outerDecoRanges, delimiterRanges };
|
||||
}
|
||||
/**
|
||||
* Filters out some ranges from old set that needs to be replaced by the new one,
|
||||
* resulting new `DecorationSet`. Doesn't intended to filtering supplementary
|
||||
* and omitted delimiter set.
|
||||
*/
|
||||
updateSet(update: ViewUpdate, decoSet: DecorationSet, ranges: Range<Decoration>[]) {
|
||||
let { startToken: start, endToken: end } = this.parser.lastParsed,
|
||||
docLen = update.state.doc.length,
|
||||
filterFrom = (this.parser.tokens[start - 1]?.to ?? 0),
|
||||
filterTo = (this.parser.tokens[end]?.from ?? docLen);
|
||||
return decoSet.map(update.changes).update({
|
||||
add: ranges, filterFrom, filterTo,
|
||||
filter(from, to, val) {
|
||||
return (to == filterFrom || from == filterTo) && ((val.spec.type as Format) <= Format.ALIGN_JUSTIFY && from != 0 || to != from);
|
||||
}
|
||||
});
|
||||
}
|
||||
/** Produces supplementary decorations (color button and reveled text for spoiler). */
|
||||
// TODO: Make it incrementally
|
||||
getSupplemental(state: EditorState, outerDecoSet: DecorationSet) {
|
||||
let decoRanges: Range<Decoration>[] = [],
|
||||
selectRanges = state.selection.ranges,
|
||||
i = 0;
|
||||
outerDecoSet.between(0, state.doc.length, (from, to, val) => {
|
||||
while (selectRanges[i] && selectRanges[i].to < from) { i++ }
|
||||
if (!selectRanges[i]) { return false }
|
||||
if (selectRanges[i].from > to) { return }
|
||||
let type = val.spec.type as Format.HIGHLIGHT | Format.SPOILER;
|
||||
if (type == Format.HIGHLIGHT && this.parser.settings.colorButton) {
|
||||
let color = val.spec.color as string,
|
||||
offset = from + (val.spec.openLen as number);
|
||||
decoRanges.push(ColorButton.of(offset, color));
|
||||
} else if (type == Format.SPOILER) {
|
||||
decoRanges.push(RevealedSpoiler.range(from, to));
|
||||
}
|
||||
});
|
||||
return Decoration.set(decoRanges);
|
||||
}
|
||||
}
|
||||
84
src/editor-mode/decorator/builder/ExtendedSyntax.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { Decoration, DecorationSet, EditorView, PluginValue, ViewUpdate } from "@codemirror/view";
|
||||
import { Parser } from "src/editor-mode/parser";
|
||||
import { DecorationBuilder, RangeOmitter } from "src/editor-mode/decorator/builder";
|
||||
import { parserField } from "src/editor-mode/state-fields";
|
||||
import { EditorState, RangeSet } from "@codemirror/state";
|
||||
import { editorLivePreviewField } from "obsidian";
|
||||
|
||||
export class ExtendedSyntax implements PluginValue {
|
||||
/** Insertion, superscript, and subscript. */
|
||||
mainDecoSet: DecorationSet;
|
||||
/**
|
||||
* Spoiler and highlight, positioned at upper DOM hierarchy and
|
||||
* not split by other decorations. Useful for making them appear
|
||||
* continous in the editor, especially when overlap with others.
|
||||
*/
|
||||
outerDecoSet: DecorationSet;
|
||||
/** Any type of tag and delimiter */
|
||||
delimiterSet: DecorationSet;
|
||||
/**
|
||||
* Any type of tag and delimiter omitted due to touching cursor or selection.
|
||||
* Used only when in live preview mode.
|
||||
*/
|
||||
omittedSet: DecorationSet;
|
||||
/**
|
||||
* Color button for highlight and revealed text for spoiler,
|
||||
* only when selection or cursor are touching them.
|
||||
*/
|
||||
supplementalSet: DecorationSet;
|
||||
combinedSet: DecorationSet;
|
||||
parser: Parser;
|
||||
builder: DecorationBuilder;
|
||||
omitter: RangeOmitter;
|
||||
noOmit = true;
|
||||
constructor(view: EditorView) {
|
||||
this.builder = new DecorationBuilder();
|
||||
this.omitter = new RangeOmitter();
|
||||
this.parser = view.state.field(parserField);
|
||||
this.builder.parser = this.parser;
|
||||
this.init(view.state);
|
||||
}
|
||||
init(state: EditorState) {
|
||||
let { mainDecoRanges, outerDecoRanges, delimiterRanges } = this.builder.build(state);
|
||||
this.mainDecoSet = Decoration.set(mainDecoRanges);
|
||||
this.outerDecoSet = Decoration.set(outerDecoRanges);
|
||||
this.delimiterSet = Decoration.set(delimiterRanges);
|
||||
this.supplementalSet = this.builder.getSupplemental(state, this.outerDecoSet);
|
||||
if (state.field(editorLivePreviewField)) {
|
||||
this.noOmit = false;
|
||||
this.omittedSet = this.omitter.omit(state, this.delimiterSet);
|
||||
} else {
|
||||
this.omittedSet = RangeSet.empty;
|
||||
}
|
||||
this.combine();
|
||||
}
|
||||
update(update: ViewUpdate) {
|
||||
if (update.docChanged) {
|
||||
let { mainDecoRanges, outerDecoRanges, delimiterRanges } = this.builder.build(update.state);
|
||||
this.mainDecoSet = this.builder.updateSet(update, this.mainDecoSet, mainDecoRanges);
|
||||
this.outerDecoSet = this.builder.updateSet(update, this.outerDecoSet, outerDecoRanges);
|
||||
this.delimiterSet = this.builder.updateSet(update, this.delimiterSet, delimiterRanges);
|
||||
}
|
||||
if (update.docChanged || update.selectionSet) {
|
||||
this.supplementalSet = this.builder.getSupplemental(update.state, this.outerDecoSet);
|
||||
}
|
||||
if (update.state.field(editorLivePreviewField)) {
|
||||
if (update.docChanged || update.selectionSet || this.noOmit) {
|
||||
this.omittedSet = this.omitter.omit(update.state, this.delimiterSet);
|
||||
}
|
||||
this.noOmit = false;
|
||||
} else {
|
||||
this.omittedSet = RangeSet.empty;
|
||||
this.noOmit = true;
|
||||
}
|
||||
this.combine();
|
||||
}
|
||||
combine() {
|
||||
this.combinedSet = RangeSet.join([
|
||||
this.mainDecoSet,
|
||||
this.delimiterSet,
|
||||
this.omittedSet,
|
||||
this.supplementalSet
|
||||
]);
|
||||
}
|
||||
}
|
||||
52
src/editor-mode/decorator/builder/RangeOmitter.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import { EditorState, Range } from "@codemirror/state";
|
||||
import { Decoration, DecorationSet } from "@codemirror/view";
|
||||
import { HiddenWidget } from "src/editor-mode/decorator/widgets";
|
||||
import { Format } from "src/enums";
|
||||
import { MainFormat } from "src/types";
|
||||
|
||||
/**
|
||||
* Omits delimiters that touch the cursor or selection,
|
||||
* so those don't appear in the editor.
|
||||
*/
|
||||
export class RangeOmitter {
|
||||
constructor() {};
|
||||
/** Should be run in preview mode only */
|
||||
omit(state: EditorState, delimiterSet: DecorationSet) {
|
||||
let omittedRanges: Range<Decoration>[] = [],
|
||||
selectRanges = state.selection.ranges, i = 0,
|
||||
openOffset: { [T in MainFormat]: null | number } = {
|
||||
[Format.INSERTION]: null,
|
||||
[Format.SPOILER]: null,
|
||||
[Format.SUPERSCRIPT]: null,
|
||||
[Format.SUBSCRIPT]: null,
|
||||
[Format.HIGHLIGHT]: null
|
||||
};
|
||||
delimiterSet.between(0, state.doc.length, (from, to, val) => {
|
||||
let type = val.spec.type as Format;
|
||||
if (type == Format.HIGHLIGHT_COLOR_TAG || type <= Format.ALIGN_JUSTIFY) {
|
||||
while (i + 1 < selectRanges.length && selectRanges[i].to < from) { i++ }
|
||||
if (selectRanges[i].from > to || selectRanges[i].to < from) {
|
||||
omittedRanges.push(HiddenWidget.of(from, to, val));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (openOffset[type as MainFormat] === null) {
|
||||
openOffset[type as MainFormat] = from;
|
||||
} else {
|
||||
let length = to - from,
|
||||
openFrom = openOffset[type as MainFormat]!;
|
||||
while (i + 1 < selectRanges.length && selectRanges[i].to < openFrom) { i++ }
|
||||
if (selectRanges[i].from > to || selectRanges[i].to < openFrom) {
|
||||
omittedRanges.push(
|
||||
HiddenWidget.of(openFrom, openFrom + length, val),
|
||||
HiddenWidget.of(from, to, val)
|
||||
);
|
||||
}
|
||||
openOffset[type as MainFormat] = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
return Decoration.set(omittedRanges, true);
|
||||
}
|
||||
}
|
||||
3
src/editor-mode/decorator/builder/index.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export * from "./DecorationBuilder";
|
||||
export * from "./RangeOmitter";
|
||||
export * from "./ExtendedSyntax";
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
import { Line, Range } from "@codemirror/state";
|
||||
import { Decoration } from "@codemirror/view";
|
||||
import { Token, TokenDecoration } from "src/types";
|
||||
import { Format } from "src/enums";
|
||||
|
||||
export const REVEALED_SPOILER_DECO: Decoration = Decoration.mark({
|
||||
class: "cm-spoiler-revealed",
|
||||
})
|
||||
|
||||
export function createHlDeco(color: string, data?: Record<string, unknown>): Decoration {
|
||||
let spec: Parameters<typeof Decoration.mark>[0] = {
|
||||
class: "cm-custom-highlight cm-custom-highlight-" + (color || "default"),
|
||||
type: Format.HIGHLIGHT,
|
||||
color,
|
||||
inclusive: false
|
||||
};
|
||||
if (data)
|
||||
for (let prop in data) spec[prop] = data[prop];
|
||||
return Decoration.mark(spec);
|
||||
}
|
||||
|
||||
export function createInlineDecoRange(token: Token, cls: string): Range<TokenDecoration> {
|
||||
return (Decoration
|
||||
.mark({ class: cls, token }) as TokenDecoration)
|
||||
.range(token.from, token.to);
|
||||
}
|
||||
|
||||
export function createLineDecoRange(token: Token, cls: string, line: Line): Range<TokenDecoration> {
|
||||
return (Decoration
|
||||
.line({ class: cls, token }) as TokenDecoration)
|
||||
.range(line.from);
|
||||
}
|
||||
111
src/editor-mode/decorator/decorations/index.ts
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import { Decoration } from "@codemirror/view";
|
||||
import { Format } from "src/enums";
|
||||
|
||||
export const AlignDeco = {
|
||||
[Format.ALIGN_LEFT]: Decoration.line({
|
||||
class: "cm-align-left",
|
||||
type: Format.ALIGN_LEFT
|
||||
}),
|
||||
[Format.ALIGN_RIGHT]: Decoration.line({
|
||||
class: "cm-align-right",
|
||||
type: Format.ALIGN_RIGHT
|
||||
}),
|
||||
[Format.ALIGN_CENTER]: Decoration.line({
|
||||
class: "cm-align-center",
|
||||
type: Format.ALIGN_CENTER
|
||||
}),
|
||||
[Format.ALIGN_JUSTIFY]: Decoration.line({
|
||||
class: "cm-align-justify",
|
||||
type: Format.ALIGN_JUSTIFY
|
||||
})
|
||||
};
|
||||
|
||||
export const AlignMarkDeco = {
|
||||
[Format.ALIGN_LEFT]: Decoration.mark({
|
||||
class: "cm-align-mark-left",
|
||||
type: Format.ALIGN_LEFT,
|
||||
omitted: true
|
||||
}),
|
||||
[Format.ALIGN_RIGHT]: Decoration.mark({
|
||||
class: "cm-align-mark-right",
|
||||
type: Format.ALIGN_RIGHT,
|
||||
omitted: true
|
||||
}),
|
||||
[Format.ALIGN_CENTER]: Decoration.mark({
|
||||
class: "cm-align-mark-center",
|
||||
type: Format.ALIGN_CENTER,
|
||||
omitted: true
|
||||
}),
|
||||
[Format.ALIGN_JUSTIFY]: Decoration.mark({
|
||||
class: "cm-align-mark-justify",
|
||||
type: Format.ALIGN_JUSTIFY,
|
||||
omitted: true
|
||||
}),
|
||||
};
|
||||
|
||||
export const DelimDeco = {
|
||||
[Format.INSERTION]: Decoration.mark({
|
||||
class: "cm-delim cm-ins",
|
||||
type: Format.INSERTION,
|
||||
omitted: true
|
||||
}),
|
||||
[Format.SPOILER]: Decoration.mark({
|
||||
class: "cm-delim cm-spoiler",
|
||||
type: Format.SPOILER,
|
||||
omitted: true
|
||||
}),
|
||||
[Format.SUPERSCRIPT]: Decoration.mark({
|
||||
class: "cm-delim cm-sup",
|
||||
type: Format.SUPERSCRIPT,
|
||||
omitted: true
|
||||
}),
|
||||
[Format.SUBSCRIPT]: Decoration.mark({
|
||||
class: "cm-delim cm-sub",
|
||||
type: Format.SUBSCRIPT,
|
||||
omitted: true
|
||||
}),
|
||||
[Format.HIGHLIGHT]: Decoration.mark({
|
||||
class: "cm-highlight",
|
||||
type: Format.HIGHLIGHT,
|
||||
omitted: true
|
||||
})
|
||||
};
|
||||
|
||||
export const ContentDeco = {
|
||||
[Format.INSERTION]: Decoration.mark({
|
||||
class: "cm-ins",
|
||||
type: Format.INSERTION,
|
||||
inclusive: true
|
||||
}),
|
||||
[Format.SPOILER]: Decoration.mark({
|
||||
class: "cm-spoiler",
|
||||
type: Format.SPOILER,
|
||||
inclusive: true
|
||||
}),
|
||||
[Format.SUPERSCRIPT]: Decoration.mark({
|
||||
class: "cm-sup",
|
||||
type: Format.SUPERSCRIPT,
|
||||
inclusive: true
|
||||
}),
|
||||
[Format.SUBSCRIPT]: Decoration.mark({
|
||||
class: "cm-sub",
|
||||
type: Format.SUBSCRIPT,
|
||||
inclusive: true
|
||||
}),
|
||||
[Format.HIGHLIGHT]: Decoration.mark({
|
||||
class: "cm-highlight",
|
||||
type: Format.HIGHLIGHT,
|
||||
inclusive: true
|
||||
})
|
||||
};
|
||||
|
||||
export const ColorTagDeco = Decoration.mark({
|
||||
class: "cm-color-tag",
|
||||
type: Format.HIGHLIGHT_COLOR_TAG,
|
||||
omitted: true
|
||||
});
|
||||
|
||||
export const RevealedSpoiler = Decoration.mark({
|
||||
class: "cm-spoiler-revealed",
|
||||
type: Format.SPOILER
|
||||
});
|
||||
15
src/editor-mode/decorator/utils/createHlDeco.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { Decoration } from "@codemirror/view";
|
||||
import { Format } from "src/enums";
|
||||
|
||||
export function createHlDeco(color: string, data?: { [P in string]: unknown }) {
|
||||
let spec: Parameters<typeof Decoration.mark>[0] = {
|
||||
class: "cm-custom-highlight cm-custom-highlight-" + (color || "default"),
|
||||
type: Format.HIGHLIGHT,
|
||||
color,
|
||||
inclusive: true
|
||||
};
|
||||
if (data) {
|
||||
for (let prop in data) { spec[prop] = data[prop] }
|
||||
}
|
||||
return Decoration.mark(spec);
|
||||
}
|
||||
1
src/editor-mode/decorator/utils/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./createHlDeco";
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
import { Range } from "@codemirror/state";
|
||||
import { WidgetType, EditorView, Decoration } from "@codemirror/view"
|
||||
import { Format } from "src/enums";
|
||||
import { Token } from "src/types";
|
||||
import { TagMenu } from "src/editor-mode/ui-components";
|
||||
|
||||
/**
|
||||
* These code snippets are taken from
|
||||
* https://github.com/Superschnizel/obisdian-fast-text-color/blob/master/src/widgets/ColorWidget.ts
|
||||
* with some modifications.
|
||||
*/
|
||||
export class ColorButton extends WidgetType {
|
||||
private _btnPos: number;
|
||||
private _colorMenu: TagMenu;
|
||||
|
||||
private constructor(token: Token) {
|
||||
super();
|
||||
this._btnPos = token.from + token.openLen;
|
||||
}
|
||||
|
||||
public eq(other: ColorButton): boolean {
|
||||
return this._btnPos == other._btnPos;
|
||||
}
|
||||
|
||||
public toDOM(view: EditorView): HTMLElement {
|
||||
let btn = document.createElement("span");
|
||||
btn.setAttribute("aria-hidden", "true");
|
||||
btn.className = "cm-highlight-color-btn";
|
||||
btn.onclick = () => {
|
||||
view.dispatch({
|
||||
selection: { anchor: this._btnPos }
|
||||
});
|
||||
this._colorMenu ||= TagMenu.create(view, Format.HIGHLIGHT);
|
||||
this._colorMenu.showMenu();
|
||||
}
|
||||
return btn;
|
||||
}
|
||||
|
||||
public ignoreEvent() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static of(hlToken: Token): Range<Decoration> {
|
||||
let btnOffset = hlToken.from + hlToken.openLen;
|
||||
return Decoration
|
||||
.widget({ widget: new ColorButton(hlToken), side: 1 })
|
||||
.range(btnOffset);
|
||||
}
|
||||
}
|
||||
|
||||
export class HiddenWidget extends WidgetType {
|
||||
private _token: Token;
|
||||
private constructor(replaced: Token) {
|
||||
super();
|
||||
this._token = replaced;
|
||||
}
|
||||
|
||||
public eq(other: HiddenWidget): boolean {
|
||||
return other._token == this._token;
|
||||
}
|
||||
|
||||
public toDOM(): HTMLElement {
|
||||
return document.createElement("span");
|
||||
}
|
||||
|
||||
public static of(from: number, to: number, token: Token, isBlock = false): Range<Decoration> {
|
||||
return Decoration.replace({
|
||||
widget: new HiddenWidget(token),
|
||||
block: isBlock,
|
||||
inclusiveEnd: false
|
||||
}).range(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
export class LineBreak extends WidgetType {
|
||||
private _offset: number;
|
||||
private constructor(offset: number) {
|
||||
super();
|
||||
this._offset = offset;
|
||||
}
|
||||
|
||||
public eq(other: LineBreak): boolean {
|
||||
return other._offset === this._offset;
|
||||
}
|
||||
|
||||
public toDOM(): HTMLElement {
|
||||
return document.createElement("br");
|
||||
}
|
||||
|
||||
public static of(offset: number): Range<Decoration> {
|
||||
return Decoration.replace({
|
||||
widget: new LineBreak(offset),
|
||||
}).range(offset - 1, offset);
|
||||
}
|
||||
}
|
||||
110
src/editor-mode/decorator/widgets/ColorButton.ts
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import { WidgetType, EditorView, Decoration } from "@codemirror/view"
|
||||
import { Menu } from "obsidian";
|
||||
|
||||
const PredefinedColors = ["red", "orange", "yellow", "green", "cyan", "blue", "purple", "pink", "accent", "default"];
|
||||
|
||||
/**
|
||||
* These code snippets are taken from
|
||||
* https://github.com/Superschnizel/obisdian-fast-text-color/blob/master/src/widgets/ColorWidget.ts
|
||||
* with some modifications.
|
||||
*/
|
||||
export class ColorButton extends WidgetType {
|
||||
color: string;
|
||||
menu: Menu;
|
||||
menuPos: { x: number, y: number };
|
||||
colorTag: { from: number, to: number };
|
||||
constructor(offset: number, color: string) {
|
||||
super();
|
||||
this.color = color;
|
||||
this.colorTag = color ?
|
||||
{ from: offset, to: offset + color.length + 2 } :
|
||||
{ from: offset, to: offset };
|
||||
}
|
||||
eq(other: ColorButton) {
|
||||
return (
|
||||
other.colorTag.from == this.colorTag.from &&
|
||||
other.colorTag.to == this.colorTag.to
|
||||
);
|
||||
}
|
||||
toDOM(view: EditorView): HTMLElement {
|
||||
let btn = document.createElement("span");
|
||||
btn.setAttribute("aria-hidden", "true");
|
||||
btn.className = "cm-highlight-color-btn";
|
||||
btn.onclick = evt => {
|
||||
view.dispatch({
|
||||
selection: {
|
||||
anchor: this.colorTag.from,
|
||||
head: this.colorTag.to
|
||||
}
|
||||
});
|
||||
this.menu = new Menu();
|
||||
this.menu.dom.addClass("cm-highlight-colors-modal");
|
||||
PredefinedColors.forEach((color) => {
|
||||
this.menu.addItem((item) => {
|
||||
item
|
||||
.setTitle(color)
|
||||
.onClick(evt => {
|
||||
view.dispatch({
|
||||
changes: {
|
||||
from: this.colorTag.from,
|
||||
to: this.colorTag.to,
|
||||
insert: color != "default" ? `{${color}}` : ""
|
||||
}
|
||||
});
|
||||
if (color == "default") {
|
||||
this.color = "";
|
||||
this.colorTag.to = this.colorTag.from;
|
||||
} else {
|
||||
this.color = color;
|
||||
this.colorTag.to = this.colorTag.from + color.length + 2;
|
||||
}
|
||||
})
|
||||
.setIcon("palette")
|
||||
.dom.addClass(`cm-item-${color || "default"}`);
|
||||
})
|
||||
});
|
||||
/* this.menu.addItem(item => {
|
||||
item
|
||||
.setTitle("Remove")
|
||||
.setIcon("ban")
|
||||
.onClick((evt) => {
|
||||
view.dispatch({
|
||||
changes: [{
|
||||
from: this.colorTag.from,
|
||||
to: this.colorTag.to,
|
||||
insert: ''
|
||||
}]
|
||||
});
|
||||
this.colorTag.from = this.colorTag.to = 0;
|
||||
});
|
||||
}); */
|
||||
this.showMenu(view, btn);
|
||||
}
|
||||
return btn;
|
||||
}
|
||||
showMenu(view: EditorView, btn: HTMLElement) {
|
||||
let menu = this.menu;
|
||||
view.requestMeasure({
|
||||
read(view) {
|
||||
let rect = btn.getBoundingClientRect();
|
||||
return { x: rect.left, y: rect.bottom }
|
||||
},
|
||||
write(measure, view) {
|
||||
menu.showAtPosition(measure);
|
||||
},
|
||||
});
|
||||
}
|
||||
ignoreEvent() {
|
||||
return false;
|
||||
}
|
||||
static of(offset: number, color: string) {
|
||||
return Decoration.widget({ widget: new ColorButton(offset, color) }).range(offset);
|
||||
}
|
||||
}
|
||||
|
||||
/* class ColorMenu extends Menu {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
} */
|
||||
20
src/editor-mode/decorator/widgets/HiddenWidget.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { Decoration, WidgetType } from "@codemirror/view";
|
||||
|
||||
export class HiddenWidget extends WidgetType {
|
||||
replaced: Decoration;
|
||||
constructor(replaced: Decoration) {
|
||||
super();
|
||||
this.replaced = replaced;
|
||||
}
|
||||
eq(other: HiddenWidget) {
|
||||
return other.replaced == this.replaced;
|
||||
}
|
||||
toDOM(): HTMLElement {
|
||||
return document.createElement("span");
|
||||
}
|
||||
static of(from: number, to: number, replaced: Decoration) {
|
||||
return Decoration.replace({
|
||||
widget: new HiddenWidget(replaced)
|
||||
}).range(from, to);
|
||||
}
|
||||
}
|
||||
2
src/editor-mode/decorator/widgets/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from "./HiddenWidget";
|
||||
export * from "./ColorButton";
|
||||
6
src/editor-mode/extensions/index.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { ViewPlugin } from "@codemirror/view";
|
||||
import { ExtendedSyntax } from "src/editor-mode/decorator/builder";
|
||||
|
||||
export const editorExtendedSyntax = ViewPlugin.fromClass(ExtendedSyntax, {
|
||||
decorations: value => value.combinedSet,
|
||||
});
|
||||
1
src/editor-mode/facets/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./settingsFacet";
|
||||
9
src/editor-mode/facets/settingsFacet.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { Facet } from "@codemirror/state";
|
||||
import { DEFAULT_SETTINGS } from "src/settings";
|
||||
import { PluginSettings } from "src/types";
|
||||
|
||||
export const settingsFacet = Facet.define<PluginSettings, PluginSettings>({
|
||||
combine(value) {
|
||||
return Object.assign({}, value.at(-1) || DEFAULT_SETTINGS);
|
||||
}
|
||||
});
|
||||
|
|
@ -1,166 +0,0 @@
|
|||
import { Command } from "obsidian";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { Format } from "src/enums";
|
||||
import { CtxMenuCommand } from "src/types";
|
||||
import { instancesStore } from "src/editor-mode/cm-extensions";
|
||||
import { TagMenu } from "src/editor-mode/ui-components";
|
||||
import { Formatter } from "src/editor-mode/formatting/formatter";
|
||||
|
||||
function _getFormatter(view: EditorView): Formatter {
|
||||
return view.state.facet(instancesStore).formatter;
|
||||
}
|
||||
|
||||
export const insertionCmd: CtxMenuCommand = {
|
||||
id: "toggle-insertion",
|
||||
name: "Toggle insertion",
|
||||
icon: "underline",
|
||||
ctxMenuTitle: "Insertion",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
let formatter = _getFormatter(editorView);
|
||||
formatter.startFormat(editorView, Format.INSERTION);
|
||||
}
|
||||
},
|
||||
type: Format.INSERTION
|
||||
}
|
||||
|
||||
export const spoilerCmd: CtxMenuCommand = {
|
||||
id: "toggle-spoiler",
|
||||
name: "Toggle spoiler",
|
||||
icon: "rectangle-horizontal",
|
||||
ctxMenuTitle: "Spoiler",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
let formatter = _getFormatter(editorView);
|
||||
formatter.startFormat(editorView, Format.SPOILER);
|
||||
}
|
||||
},
|
||||
type: Format.SPOILER
|
||||
}
|
||||
|
||||
export const superscriptCmd: CtxMenuCommand = {
|
||||
id: "toggle-superscript",
|
||||
name: "Toggle superscript",
|
||||
icon: "superscript",
|
||||
ctxMenuTitle: "Superscript",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
let formatter = _getFormatter(editorView);
|
||||
formatter.startFormat(editorView, Format.SUPERSCRIPT);
|
||||
}
|
||||
},
|
||||
type: Format.SUPERSCRIPT
|
||||
}
|
||||
|
||||
export const subscriptCmd: CtxMenuCommand = {
|
||||
id: "toggle-subscript",
|
||||
name: "Toggle subscript",
|
||||
icon: "subscript",
|
||||
ctxMenuTitle: "Subscript",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
let formatter = _getFormatter(editorView);
|
||||
formatter.startFormat(editorView, Format.SUBSCRIPT);
|
||||
}
|
||||
},
|
||||
type: Format.SUBSCRIPT
|
||||
}
|
||||
|
||||
export const customHighlightCmd: CtxMenuCommand = {
|
||||
id: "toggle-custom-highlight",
|
||||
name: "Toggle custom highlight",
|
||||
icon: "highlighter",
|
||||
ctxMenuTitle: "Custom highlight",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
let formatter = _getFormatter(editorView);
|
||||
formatter.startFormat(editorView, Format.HIGHLIGHT, undefined, false, true);
|
||||
}
|
||||
},
|
||||
type: Format.HIGHLIGHT
|
||||
}
|
||||
|
||||
export const customSpanCmd: CtxMenuCommand = {
|
||||
id: "toggle-custom-span",
|
||||
name: "Toggle custom span",
|
||||
icon: "brush",
|
||||
ctxMenuTitle: "Custom span",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
let formatter = _getFormatter(editorView);
|
||||
formatter.startFormat(editorView, Format.CUSTOM_SPAN, undefined, false, true);
|
||||
}
|
||||
},
|
||||
type: Format.CUSTOM_SPAN
|
||||
}
|
||||
|
||||
export const fencedDivCmd: Command = {
|
||||
id: "toggle-fenced-div",
|
||||
name: "Toggle fenced div",
|
||||
icon: "list-plus",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
let formatter = _getFormatter(editorView);
|
||||
formatter.startFormat(editorView, Format.FENCED_DIV, undefined, false, true);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export const colorMenuCmd: Command = {
|
||||
id: "show-color-menu",
|
||||
name: "Show highlight color menu",
|
||||
icon: "palette",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
TagMenu.create(editorView, Format.HIGHLIGHT).showMenu();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const spanTagMenuCmd: Command = {
|
||||
id: "show-custom-span-tag-menu",
|
||||
name: "Show custom span tag menu",
|
||||
icon: "shapes",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
TagMenu.create(editorView, Format.CUSTOM_SPAN).showMenu();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const divTagMenuCmd: Command = {
|
||||
id: "show-fenced-div-tag-menu",
|
||||
name: "Show fenced div tag menu",
|
||||
icon: "shapes",
|
||||
editorCallback: (editor, ctx) => {
|
||||
let editorView = (editor.activeCm || editor.cm);
|
||||
if (editorView instanceof EditorView) {
|
||||
TagMenu.create(editorView, Format.FENCED_DIV).showMenu();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const ctxMenuCommands = [
|
||||
insertionCmd,
|
||||
spoilerCmd,
|
||||
superscriptCmd,
|
||||
subscriptCmd,
|
||||
customHighlightCmd,
|
||||
customSpanCmd
|
||||
];
|
||||
|
||||
export const editorCommands = [
|
||||
fencedDivCmd,
|
||||
colorMenuCmd,
|
||||
spanTagMenuCmd,
|
||||
divTagMenuCmd
|
||||
].concat(ctxMenuCommands);
|
||||
|
|
@ -1,435 +0,0 @@
|
|||
import { ChangeSet, ChangeSpec, EditorSelection, EditorState, SelectionRange, Text } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { Format, TokenLevel, TokenStatus } from "src/enums";
|
||||
import { BlockFormat, InlineFormat, PluginSettings, TokenGroup, PlainRange, Token } from "src/types";
|
||||
import { SelectionObserver } from "src/editor-mode/preprocessor/observer";
|
||||
import { EditorParser } from "src/editor-mode/preprocessor/parser";
|
||||
import { getTagRange, provideTokenPartsRanges } from "src/editor-mode/utils/token-utils";
|
||||
import { getBlocks, isBlockStart, isBlockEnd } from "src/editor-mode/utils/doc-utils";
|
||||
import { trimSelection } from "src/editor-mode/utils/selection-utils";
|
||||
import { isTouched } from "src/editor-mode/utils/range-utils";
|
||||
import { TagMenu } from "src/editor-mode/ui-components";
|
||||
import { BlockRules, InlineRules } from "src/format-configs/rules";
|
||||
import { isInlineFormat, supportTag } from "src/format-configs/format-utils";
|
||||
|
||||
function _isSurroundedByDelimiter(str: string, delimStr: string): boolean {
|
||||
return str.startsWith(delimStr) && str.endsWith(delimStr);
|
||||
}
|
||||
|
||||
class _FormatterState {
|
||||
public editorState: EditorState;
|
||||
public doc: Text;
|
||||
public tokens: TokenGroup;
|
||||
public selectionRanges: SelectionRange[];
|
||||
public curSelectionIndex: number = 0;
|
||||
public tokenMaps: (number[] | undefined)[];
|
||||
public curTokenMap: number[] | undefined;
|
||||
public level: TokenLevel;
|
||||
public type: Format;
|
||||
public delimStr: string;
|
||||
public tagStr: string | undefined;
|
||||
public precise: boolean;
|
||||
public changes: ChangeSpec[] = [];
|
||||
public selectionShift: Partial<Record<number, { shift: number }>> = {};
|
||||
public changeSet: ChangeSet;
|
||||
public remappedSelection: EditorSelection;
|
||||
|
||||
constructor(type: Format, editorState: EditorState, selectionObserver: SelectionObserver, settings: PluginSettings, tagStr?: string) {
|
||||
this.type = type;
|
||||
this.editorState = editorState;
|
||||
this.doc = editorState.doc;
|
||||
this.level = isInlineFormat(type) ? TokenLevel.INLINE : TokenLevel.BLOCK;
|
||||
this.tagStr = tagStr;
|
||||
this.precise = settings.tidyFormatting;
|
||||
this.tokens = selectionObserver.parser.getTokens(this.level);
|
||||
this.tokenMaps = selectionObserver.pickMaps(type);
|
||||
this.curTokenMap = this.tokenMaps[this.curSelectionIndex];
|
||||
if (this.precise) {
|
||||
let trimmedSelection = trimSelection(selectionObserver.selection, this.doc);
|
||||
this.selectionRanges = trimmedSelection.ranges.map(range => range);
|
||||
} else {
|
||||
this.selectionRanges = selectionObserver.selection.ranges.map(range => range);
|
||||
}
|
||||
if (tagStr && this.level == TokenLevel.INLINE) {
|
||||
this.tagStr = "{" + tagStr + "}";
|
||||
} else if (this.level == TokenLevel.BLOCK) {
|
||||
this.tagStr += "\n";
|
||||
}
|
||||
let { char, length: delimLen } = this.level == TokenLevel.INLINE
|
||||
? InlineRules[type as InlineFormat]
|
||||
: BlockRules[type as BlockFormat];
|
||||
this.delimStr = char.padEnd(delimLen, char);
|
||||
}
|
||||
|
||||
public get curRange(): SelectionRange {
|
||||
return this.selectionRanges[this.curSelectionIndex];
|
||||
}
|
||||
|
||||
public get mappedTokens(): TokenGroup {
|
||||
if (!this.curTokenMap) { return [] }
|
||||
return this.curTokenMap.map(index => this.tokens[index]);
|
||||
}
|
||||
|
||||
public advance(): boolean {
|
||||
this.curSelectionIndex++
|
||||
if (this.curSelectionIndex >= this.selectionRanges.length) {
|
||||
return false;
|
||||
}
|
||||
this.curTokenMap = this.tokenMaps[this.curSelectionIndex];
|
||||
return true;
|
||||
}
|
||||
|
||||
public pushChange(spec: ChangeSpec): void {
|
||||
this.changes.push(spec);
|
||||
}
|
||||
|
||||
public pushSelectionShift(index: number, shift: number): void {
|
||||
this.selectionShift[index] = { shift };
|
||||
}
|
||||
}
|
||||
|
||||
export class Formatter {
|
||||
private readonly _parser: EditorParser;
|
||||
private readonly _observer: SelectionObserver;
|
||||
private readonly _settings: PluginSettings;
|
||||
public state: _FormatterState;
|
||||
|
||||
constructor(parser: EditorParser, observer: SelectionObserver) {
|
||||
this._parser = parser;
|
||||
this._observer = observer;
|
||||
this._settings = this._parser.settings;
|
||||
}
|
||||
|
||||
public startFormat(view: EditorView, type: Format, tagStr?: string, forceRemove?: boolean, showMenu?: boolean): void {
|
||||
if (forceRemove) {
|
||||
tagStr = undefined;
|
||||
}
|
||||
if (!this._settings.openTagMenuAfterFormat) {
|
||||
showMenu = false;
|
||||
}
|
||||
|
||||
let tokenMaps = this._observer.pickMaps(type),
|
||||
editorState = view.state;
|
||||
if (tokenMaps.length == 1 && !tokenMaps[0]?.length && showMenu) {
|
||||
TagMenu.create(view, type).showMenu();
|
||||
return;
|
||||
}
|
||||
this._defineState(editorState, type, tagStr);
|
||||
|
||||
if (forceRemove) {
|
||||
this._removeAll()
|
||||
} else if (this.state.level == TokenLevel.INLINE) {
|
||||
this._formatInline();
|
||||
} else {
|
||||
this._formatBlock();
|
||||
}
|
||||
|
||||
this._composeChanges();
|
||||
this._remapSelection();
|
||||
this._dispatchToView(view);
|
||||
}
|
||||
|
||||
private _defineState(editorState: EditorState, type: Format, tagStr?: string): void {
|
||||
this.state = new _FormatterState(type, editorState, this._observer, this._settings, tagStr);
|
||||
}
|
||||
|
||||
private _clearState(): void {
|
||||
(this.state as unknown as undefined) = undefined;
|
||||
}
|
||||
|
||||
private _formatInline(): void {
|
||||
let state = this.state,
|
||||
tokens = this.state.tokens;
|
||||
|
||||
do {
|
||||
let { curTokenMap, curRange, tagStr, precise } = state,
|
||||
firstToken = curTokenMap ? tokens[curTokenMap[0]] : null;
|
||||
if (!precise) {
|
||||
this._toggleInlineDelim();
|
||||
} else if (!firstToken) {
|
||||
this._wrap();
|
||||
} else if (firstToken.from > curRange.from || firstToken.to < curRange.to) {
|
||||
this._extend();
|
||||
} else if (firstToken.status != TokenStatus.ACTIVE) {
|
||||
this._close(firstToken);
|
||||
} else if (tagStr !== undefined) {
|
||||
this._changeInlineTag(firstToken);
|
||||
} else if (curRange.empty) {
|
||||
this._remove(firstToken);
|
||||
} else {
|
||||
this._breakApart(firstToken);
|
||||
}
|
||||
} while (state.advance());
|
||||
}
|
||||
|
||||
private _formatBlock(): void {
|
||||
do {
|
||||
this._toggleBlockTag();
|
||||
} while (this.state.advance());
|
||||
}
|
||||
|
||||
/**
|
||||
* Use wrap only when the current range didn't meet any token.
|
||||
*
|
||||
* **Exclusive to inline formatting use.**
|
||||
*/
|
||||
private _wrap(detectWord = true): void {
|
||||
let { curRange, delimStr, tagStr } = this.state;
|
||||
// If the current selection is actually an empty cursor, attempt to use
|
||||
// word range if any.
|
||||
if (curRange.empty) {
|
||||
let cursorOffset = curRange.from;
|
||||
if (detectWord) {
|
||||
curRange = this.state.editorState.wordAt(curRange.from) ?? curRange;
|
||||
}
|
||||
if (cursorOffset == curRange.to) {
|
||||
let shiftAmount = delimStr.length;
|
||||
if (cursorOffset == curRange.from) {
|
||||
shiftAmount += tagStr?.length ?? 0;
|
||||
}
|
||||
this.state.pushSelectionShift(this.state.curSelectionIndex, shiftAmount);
|
||||
}
|
||||
}
|
||||
this.state.pushChange([
|
||||
{ from: curRange.from, insert: delimStr + (tagStr ?? "") },
|
||||
{ from: curRange.to, insert: delimStr }
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add corresponding closing delimiter to the token that's currently
|
||||
* inactive. Should be run when the cursor is within or the same range as
|
||||
* the token.
|
||||
*
|
||||
* **Exclusive to inline formatting use.**
|
||||
*
|
||||
* @param token should be in `INACTIVE` status.
|
||||
*/
|
||||
private _close(token: Token): void {
|
||||
let { delimStr, tagStr } = this.state;
|
||||
if (supportTag(token.type) && tagStr) {
|
||||
this.state.pushChange({ from: token.from + token.openLen, insert: tagStr });
|
||||
}
|
||||
this.state.pushChange({ from: token.to, insert: delimStr });
|
||||
}
|
||||
|
||||
/**
|
||||
* Break the current token into two new tokens if the current range
|
||||
* was within the content range of the token and didn't touch both
|
||||
* delimiters, or narrow it if one of both delimiters was touched, or
|
||||
* behave like `remove()`. Should be run when the current selection
|
||||
* range within the token.
|
||||
*
|
||||
* **Exclusive to inline formatting use.**
|
||||
*/
|
||||
private _breakApart(token: Token): void {
|
||||
let { openRange, tagRange, closeRange } = provideTokenPartsRanges(token),
|
||||
{ curRange, delimStr } = this.state,
|
||||
tagStr = this.state.doc.sliceString(tagRange.from, tagRange.to);
|
||||
// Remove opening delimiter when the current range touched it. Otherwise,
|
||||
// insert corresponding delimiter at the start offset.
|
||||
if (isTouched(curRange.from, openRange)) {
|
||||
this.state.pushChange(openRange);
|
||||
} else {
|
||||
this.state.pushChange({ from: curRange.from, insert: delimStr });
|
||||
}
|
||||
// Remove closing delimiter when the current range touched it. Otherwise,
|
||||
// insert corresponding delimiter at the end offset.
|
||||
if (isTouched(curRange.to, closeRange)) {
|
||||
this.state.pushChange(closeRange);
|
||||
} else {
|
||||
// This delimiter should be opening. To have the same tag as the original
|
||||
// we need to copy the original tag to the newly created one.
|
||||
this.state.pushChange({ from: curRange.to, insert: delimStr + tagStr });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend formatting range to cover across the tokens that are in the
|
||||
* current token map, and across the current selection. Should be run
|
||||
* with condition the selection touched at least two tokens, or a token
|
||||
* with the selection range exceeds the token range, at least one of its
|
||||
* side.
|
||||
*
|
||||
* **Exclusive to inline formatting use.**
|
||||
*/
|
||||
private _extend(): void {
|
||||
let { curRange, delimStr, tagStr, curTokenMap, mappedTokens } = this.state,
|
||||
tokens = this._parser.getTokens(this.state.level),
|
||||
firstTokenIndex = curTokenMap?.[0],
|
||||
lastTokenIndex = curTokenMap?.at(-1),
|
||||
firstToken = mappedTokens[0],
|
||||
lastToken = mappedTokens[mappedTokens.length - 1],
|
||||
isLastTokenAtEdge = false,
|
||||
fusedRange: PlainRange = { from: firstTokenIndex ?? 0, to: (lastTokenIndex ?? 0) + 1 };
|
||||
// If the start offset of the current range touches a token, then
|
||||
// eliminate only its closing delimiter.
|
||||
if (firstToken && firstToken.from <= curRange.from) {
|
||||
let { tagRange, closeRange } = provideTokenPartsRanges(firstToken);
|
||||
fusedRange.from++;
|
||||
if (tagStr !== undefined) {
|
||||
this.state.pushChange(
|
||||
firstToken.validTag
|
||||
? { from: tagRange.from, to: tagRange.to, insert: tagStr }
|
||||
: { from: tagRange.from, insert: tagStr }
|
||||
);
|
||||
}
|
||||
this.state.pushChange(closeRange);
|
||||
} else {
|
||||
this.state.pushChange({ from: curRange.from, insert: delimStr });
|
||||
}
|
||||
if (lastToken && lastToken.to >= curRange.to) {
|
||||
isLastTokenAtEdge = true;
|
||||
fusedRange.to--;
|
||||
}
|
||||
// Tokens that don't touch one of the current range side have to be fused
|
||||
// (i.e. eliminate all of their delimiter).
|
||||
for (let i = fusedRange.from; i < fusedRange.to; i++) {
|
||||
let tokenIndex = curTokenMap?.[i];
|
||||
if (tokenIndex !== undefined) {
|
||||
this._remove(tokens[tokenIndex]);
|
||||
}
|
||||
}
|
||||
// If the end offset of the current range touches a token, then eliminate
|
||||
// only its opening delimiter.
|
||||
if (isLastTokenAtEdge) {
|
||||
let { openRange, tagRange } = provideTokenPartsRanges(lastToken!);
|
||||
this.state.pushChange(openRange);
|
||||
if (lastToken!.validTag) {
|
||||
this.state.pushChange(tagRange);
|
||||
}
|
||||
if (lastToken!.status != TokenStatus.ACTIVE) {
|
||||
this.state.pushChange({ from: curRange.to, insert: delimStr });
|
||||
}
|
||||
} else {
|
||||
this.state.pushChange({ from: curRange.to, insert: delimStr });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the tag of targetted token, or insert as a new if the current
|
||||
* tag was invalid or didn't exist.
|
||||
*
|
||||
* **Exclusive to inline formatting use.**
|
||||
*/
|
||||
private _changeInlineTag(token: Token): void {
|
||||
let { tagRange } = provideTokenPartsRanges(token),
|
||||
{ tagStr, curRange, curSelectionIndex } = this.state;
|
||||
if (tagStr === undefined) { return }
|
||||
if (!token.validTag) { tagRange.to = tagRange.from }
|
||||
this.state.pushChange({ from: tagRange.from, to: tagRange.to, insert: tagStr });
|
||||
if (curRange.empty && curRange.from == tagRange.from) {
|
||||
this.state.pushSelectionShift(curSelectionIndex, tagStr.length);
|
||||
}
|
||||
}
|
||||
|
||||
/** Run only when tidier formatting is switched off. */
|
||||
private _toggleInlineDelim(): void {
|
||||
let { curRange, delimStr } = this.state,
|
||||
delimLen = delimStr.length,
|
||||
selectedStrWithOverlappedEdge = this.state.doc.sliceString(curRange.from - delimLen, curRange.to + delimLen),
|
||||
selectedStr = selectedStrWithOverlappedEdge.slice(delimLen, -delimLen);
|
||||
if (_isSurroundedByDelimiter(selectedStr, delimStr)) {
|
||||
this.state.pushChange([
|
||||
{ from: curRange.from, to: curRange.from + delimLen },
|
||||
{ from: curRange.to - delimLen, to: curRange.to }
|
||||
]);
|
||||
} else if (_isSurroundedByDelimiter(selectedStrWithOverlappedEdge, delimStr)) {
|
||||
this.state.pushChange([
|
||||
{ from: curRange.from - delimLen, to: curRange.from },
|
||||
{ from: curRange.to, to: curRange.to + delimLen }
|
||||
]);
|
||||
} else {
|
||||
let detectWord = false;
|
||||
this._wrap(detectWord);
|
||||
}
|
||||
}
|
||||
|
||||
private _addBlockTag(block: { start: number, end: number }): void {
|
||||
let { doc } = this.state,
|
||||
{ delimStr } = this.state,
|
||||
blockStart = doc.line(block.start),
|
||||
blockEnd = doc.line(block.end - 1),
|
||||
tagStr = this.state.tagStr ?? "";
|
||||
if (!isBlockStart(doc, blockStart)) { delimStr = "\n" + delimStr }
|
||||
this.state.pushChange({ from: blockStart.from, insert: delimStr + tagStr });
|
||||
if (!isBlockEnd(doc, blockEnd)) {
|
||||
this.state.pushChange({ from: blockEnd.to, insert: "\n" });
|
||||
}
|
||||
}
|
||||
|
||||
private _changeBlockTag(token: Token): void {
|
||||
let { tagRange } = provideTokenPartsRanges(token),
|
||||
{ tagStr } = this.state;
|
||||
this.state.pushChange({ from: tagRange.from, to: tagRange.to, insert: tagStr });
|
||||
}
|
||||
|
||||
private _toggleBlockTag(): void {
|
||||
let { doc } = this.state,
|
||||
blocks = getBlocks(doc, this.state.curRange),
|
||||
{ mappedTokens, tagStr } = this.state;
|
||||
for (let i = 0, j = 0; i < blocks.length; i++) {
|
||||
let block = blocks[i],
|
||||
token: Token | undefined = mappedTokens[j],
|
||||
blockStart = doc.line(block.start),
|
||||
tagRange = token ? getTagRange(token) : undefined;
|
||||
if (!token || blockStart.from < token.from) {
|
||||
this._addBlockTag(block);
|
||||
} else if (token.status != TokenStatus.ACTIVE || blockStart.from > tagRange!.to + 1) {
|
||||
this._addBlockTag(block); j++;
|
||||
} else if (tagStr === undefined) {
|
||||
this._remove(token); j++;
|
||||
} else {
|
||||
this._changeBlockTag(token); j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove formatting based on the token by erasing its delimiter. Should
|
||||
* be run on fused token in `extend()`, or when the cursor is empty and
|
||||
* within the token.
|
||||
*/
|
||||
private _remove(token: Token): void {
|
||||
let { openRange, tagRange, closeRange } = provideTokenPartsRanges(token),
|
||||
removedRanges = [openRange, tagRange, closeRange];
|
||||
if (!token.validTag) { removedRanges.remove(tagRange) }
|
||||
if (token.level == TokenLevel.BLOCK && token.to > tagRange.to) { tagRange.to++ }
|
||||
this.state.pushChange(removedRanges);
|
||||
}
|
||||
|
||||
private _removeAll(): void {
|
||||
do {
|
||||
let { mappedTokens } = this.state;
|
||||
mappedTokens.forEach(token => {
|
||||
this._remove(token);
|
||||
});
|
||||
} while (this.state.advance());
|
||||
}
|
||||
|
||||
private _composeChanges(): ChangeSet {
|
||||
return this.state.changeSet = ChangeSet.of(this.state.changes, this.state.doc.length);
|
||||
}
|
||||
|
||||
private _remapSelection(): EditorSelection {
|
||||
let { selectionRanges, changeSet, selectionShift: selectionChanges } = this.state;
|
||||
for (let i = 0; i < selectionRanges.length; i++) {
|
||||
let range = selectionRanges[i].map(changeSet),
|
||||
shift = selectionChanges[i]?.shift;
|
||||
if (shift) {
|
||||
range = EditorSelection.range(range.to + shift, range.from + shift);
|
||||
}
|
||||
selectionRanges[i] = range;
|
||||
}
|
||||
return this.state.remappedSelection = EditorSelection.create(selectionRanges);
|
||||
}
|
||||
|
||||
private _dispatchToView(view: EditorView): void {
|
||||
let { changeSet, remappedSelection } = this.state;
|
||||
view.dispatch(
|
||||
{ changes: changeSet },
|
||||
{ selection: remappedSelection, sequential: true }
|
||||
);
|
||||
this._clearState();
|
||||
}
|
||||
}
|
||||
191
src/editor-mode/parser/Parser.ts
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
import { ChangedRange, MainFormat, PluginSettings, StateConfig, TokenGroup } from "src/types";
|
||||
import { ParserState } from "src/editor-mode/parser";
|
||||
import { TokenQueue } from "src/editor-mode/parser";
|
||||
import { ChangeSet, Line, Text } from "@codemirror/state";
|
||||
import { Format, SettingOpt1, TokenRole, TokenStatus } from "src/enums";
|
||||
import { Tokenizer } from "src/editor-mode/parser";
|
||||
import { SpaceRestrictedFormats } from "src/shared-configs";
|
||||
import { Tree } from "@lezer/common";
|
||||
import { composeChanges, disableEscape, findShifterAt, getShifterStart, hasInterferer } from "src/editor-mode/parser/utils";
|
||||
import { EditorDelimLookup } from "./configs";
|
||||
|
||||
export class Parser {
|
||||
private state: ParserState;
|
||||
private queue: TokenQueue = new TokenQueue();
|
||||
private reusedTokens: TokenGroup | undefined;
|
||||
tokens: TokenGroup = [];
|
||||
lastParsed = { startToken: 0, endToken: 0 };
|
||||
settings: PluginSettings;
|
||||
constructor(settings: PluginSettings) {
|
||||
this.settings = settings;
|
||||
if (!settings.editorEscape) {
|
||||
disableEscape();
|
||||
}
|
||||
}
|
||||
private defineState(config: StateConfig) {
|
||||
this.state = new ParserState(config, this.tokens);
|
||||
this.queue.attachState(this.state);
|
||||
this.shiftOffset();
|
||||
}
|
||||
private removeState() {
|
||||
this.queue.deattachState();
|
||||
(this.state as ParserState | undefined) = undefined;
|
||||
}
|
||||
private streamParse() {
|
||||
let prevLine: Line | null,
|
||||
state = this.state,
|
||||
hlOpen = this.queue.getOpen(Format.HIGHLIGHT);
|
||||
if (prevLine = this.state.prevLine) {
|
||||
state.setContext(state.getContext(prevLine));
|
||||
}
|
||||
if (hlOpen && state.gOffset == hlOpen.to) {
|
||||
Tokenizer.colorTag(state);
|
||||
}
|
||||
state.resolveContext();
|
||||
do { this.parseLine() } while (state.nextLine())
|
||||
this.queue.clear();
|
||||
this.removeState();
|
||||
}
|
||||
private parseLine() {
|
||||
let type: MainFormat | undefined,
|
||||
state = this.state;
|
||||
if (this.settings.customAlign & SettingOpt1.EDITOR_MODE) {
|
||||
Tokenizer.align(state);
|
||||
}
|
||||
while (true) {
|
||||
if (state.isSpace()) {
|
||||
state.queue.resolve(SpaceRestrictedFormats);
|
||||
}
|
||||
let nodeType = state.processCursor();
|
||||
if (nodeType == "skipped") {
|
||||
state.skipCursorRange();
|
||||
} else if (nodeType == "hl_delim") {
|
||||
Tokenizer.highlightDelim(state);
|
||||
} else if (nodeType == "table_sep") {
|
||||
state.queue.resolveAll();
|
||||
state.advance();
|
||||
} else if (type = EditorDelimLookup[state.char]) {
|
||||
Tokenizer.delim(state, type);
|
||||
} else if (!state.advance()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
initParse(doc: Text, tree: Tree) {
|
||||
this.defineState({ doc, tree, offset: 0, settings: this.settings });
|
||||
this.streamParse();
|
||||
this.lastParsed.endToken = this.tokens.length;
|
||||
}
|
||||
applyChange(doc: Text, tree: Tree, oldTree: Tree, changes: ChangeSet) {
|
||||
let changedRange = composeChanges(changes),
|
||||
offset = Math.min(oldTree.length, tree.length) + 1;
|
||||
if (changedRange) {
|
||||
offset = Math.min(offset, changedRange.from);
|
||||
}
|
||||
let config: StateConfig = { doc, tree, offset, settings: this.settings };
|
||||
this.shiftOffsetByNode(config, oldTree);
|
||||
this.filterTokens(config, oldTree, changedRange);
|
||||
this.defineState(config);
|
||||
this.streamParse();
|
||||
this.recalculateReusedTokens(changedRange);
|
||||
}
|
||||
private filterTokens(config: StateConfig, oldTree: Tree, changedRange: ChangedRange | null) {
|
||||
let start = 0,
|
||||
lastChange: number | undefined,
|
||||
filteredOut: TokenGroup;
|
||||
for (let curToken = this.tokens[start]; start < this.tokens.length; curToken = this.tokens[++start]) {
|
||||
if (curToken.to < config.offset) { continue }
|
||||
if (curToken.from >= config.offset) { break }
|
||||
if (curToken.role == TokenRole.CONTENT || curToken.role == TokenRole.CLOSE) {
|
||||
let openDelim = this.tokens[curToken.pointer],
|
||||
content = this.tokens[curToken.pointer + 1],
|
||||
size = 2;
|
||||
if (curToken.type != Format.HIGHLIGHT) {
|
||||
openDelim.status = curToken.status = TokenStatus.PENDING;
|
||||
}
|
||||
openDelim.size = content.size = size;
|
||||
content.to = curToken.from;
|
||||
this.queue.push(openDelim);
|
||||
this.queue.push(content);
|
||||
lastChange ??= curToken.pointer;
|
||||
}
|
||||
if (curToken.role != TokenRole.CONTENT) { break }
|
||||
}
|
||||
this.lastParsed.startToken = lastChange ?? start;
|
||||
filteredOut = this.tokens.splice(start);
|
||||
if (changedRange && filteredOut.length) {
|
||||
if (
|
||||
hasInterferer(config.tree, changedRange.from, changedRange.changedTo) ||
|
||||
hasInterferer(oldTree, changedRange.from, changedRange.initTo)
|
||||
) { return }
|
||||
let i = 0,
|
||||
line = config.doc.lineAt(changedRange.changedTo),
|
||||
changeLen = changedRange.length;
|
||||
while (line.number < config.doc.lines) {
|
||||
line = config.doc.line(line.number + 1);
|
||||
if (!line.text.trimEnd()) { break }
|
||||
}
|
||||
while (i < filteredOut.length && filteredOut[i].to <= line.to - changeLen) { i++ }
|
||||
if (i < filteredOut.length) { this.reusedTokens = filteredOut.slice(i) }
|
||||
config.maxLine = line.number;
|
||||
}
|
||||
}
|
||||
private shiftOffsetByNode(config: StateConfig, oldTree: Tree) {
|
||||
let newNode = findShifterAt(config.tree, config.offset),
|
||||
oldNode = findShifterAt(oldTree, config.offset),
|
||||
newOffset: number | null = null;
|
||||
if (newNode) {
|
||||
newOffset = getShifterStart(newNode);
|
||||
}
|
||||
if (oldNode) {
|
||||
let oldStart = getShifterStart(oldNode);
|
||||
if (oldStart !== null && (newOffset === null || oldStart < newOffset)) {
|
||||
newOffset = oldStart;
|
||||
}
|
||||
}
|
||||
if (newOffset !== null) {
|
||||
config.offset = newOffset;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private shiftOffset() {
|
||||
if (this.state.offset == 0) { return false }
|
||||
let prevOffset = this.state.offset - 1,
|
||||
str = this.state.lineStr,
|
||||
char = str[prevOffset],
|
||||
lastToken = this.state.lastToken;
|
||||
if (char == "+" || char == "|" || char == "^" || char == "~" || char == "=") {
|
||||
while (str[prevOffset - 1] == char) { prevOffset-- }
|
||||
this.state.offset = prevOffset;
|
||||
}
|
||||
else if (
|
||||
lastToken?.type == Format.HIGHLIGHT &&
|
||||
lastToken.role == TokenRole.CONTENT &&
|
||||
lastToken.from >= this.state.line.from &&
|
||||
this.queue.isQueued(Format.HIGHLIGHT)
|
||||
) {
|
||||
this.state.setGOffset(lastToken.from);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private recalculateReusedTokens(changedRange: ChangedRange | null) {
|
||||
this.lastParsed.endToken = this.tokens.length;
|
||||
if (!this.reusedTokens || !changedRange) { return }
|
||||
let indexDiffer = this.tokens.length - this.reusedTokens[0].pointer,
|
||||
offsetDiffer = changedRange.changedTo - changedRange.initTo;
|
||||
for (
|
||||
let i = 0, token = this.reusedTokens[i];
|
||||
i < this.reusedTokens.length;
|
||||
token = this.reusedTokens[++i]
|
||||
) {
|
||||
token.from += offsetDiffer;
|
||||
token.to += offsetDiffer;
|
||||
token.pointer += indexDiffer;
|
||||
}
|
||||
this.tokens = this.tokens.concat(this.reusedTokens);
|
||||
this.reusedTokens = undefined;
|
||||
}
|
||||
}
|
||||
223
src/editor-mode/parser/ParserState.ts
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
import { Line, Text } from "@codemirror/state";
|
||||
import { Tree, TreeCursor } from "@lezer/common";
|
||||
import { PluginSettings, StateConfig, TokenGroup } from "src/types";
|
||||
import { TokenQueue } from "src/editor-mode/parser";
|
||||
import { Format, LineCtx, SettingOpt1 } from "src/enums";
|
||||
import { NonHighlightFormats, SpaceRestrictedFormats } from "src/shared-configs";
|
||||
import { SKIPPED_NODE_RE } from "src/editor-mode/parser/regexps";
|
||||
import { findNode, getContextFromNode } from "src/editor-mode/parser/utils";
|
||||
|
||||
export class ParserState {
|
||||
doc: Text;
|
||||
tree: Tree;
|
||||
cursor: TreeCursor | null;
|
||||
line: Line;
|
||||
/** block start */
|
||||
maxLine: number;
|
||||
offset: number;
|
||||
tokens: TokenGroup;
|
||||
queue: TokenQueue;
|
||||
curCtx: LineCtx = LineCtx.NONE;
|
||||
prevCtx: LineCtx = LineCtx.NONE;
|
||||
settings: PluginSettings;
|
||||
constructor(config: StateConfig, tokens: TokenGroup) {
|
||||
this.doc = config.doc;
|
||||
this.tree = config.tree;
|
||||
this.line = this.doc.lineAt(config.offset);
|
||||
this.maxLine = config.maxLine || this.doc.lineAt(this.tree.length).number;
|
||||
this.offset = config.offset - this.line.from;
|
||||
this.tokens = tokens;
|
||||
this.cursor = this.tree.cursor();
|
||||
this.settings = config.settings;
|
||||
this.nextCursor();
|
||||
if (
|
||||
(!this.lastToken || this.lastToken.to < this.line.from) &&
|
||||
this.lineStr[0] == "!" && this.offset < 11
|
||||
) { this.offset = 0 }
|
||||
}
|
||||
/** global offset */
|
||||
get gOffset() {
|
||||
return this.offset + this.line.from;
|
||||
}
|
||||
get linePos() {
|
||||
return this.line.number;
|
||||
}
|
||||
get lineStr() {
|
||||
return this.line.text;
|
||||
}
|
||||
get char() {
|
||||
let char = this.line.text[this.offset];
|
||||
if (!char && !this.isLastLine()) {
|
||||
return "\n";
|
||||
}
|
||||
return char ?? "";
|
||||
}
|
||||
get lastToken() {
|
||||
return this.tokens.at(-1);
|
||||
}
|
||||
get prevLine() {
|
||||
let linePos = this.linePos;
|
||||
if (linePos == 1) { return null }
|
||||
return this.doc.line(linePos - 1);
|
||||
}
|
||||
advance(n = 1) {
|
||||
let restLen = this.lineStr.length - this.offset;
|
||||
if (!restLen) {
|
||||
this.queue.resolve(SpaceRestrictedFormats);
|
||||
return false;
|
||||
}
|
||||
if (n > restLen) {
|
||||
this.offset += restLen;
|
||||
} else {
|
||||
this.offset += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
setGOffset(gOffset: number) {
|
||||
if (gOffset > this.doc.length) {
|
||||
this.line = this.doc.line(this.doc.lines);
|
||||
this.offset = this.line.to;
|
||||
} else {
|
||||
this.line = this.doc.lineAt(gOffset);
|
||||
this.offset = gOffset - this.line.from;
|
||||
}
|
||||
}
|
||||
isSpace(side: -1 | 0 | 1 = 0) {
|
||||
let char = this.lineStr[this.offset + side];
|
||||
return char == " " || char == "\t" || !char;
|
||||
}
|
||||
seekWhitespace(maxOffset = this.line.length) {
|
||||
let offset = this.offset;
|
||||
for (let char = this.line.text[offset]; offset < maxOffset; char = this.line.text[++offset]) {
|
||||
if (char == " " || char == "\t") {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
nextLine(skipBlankLine = true) {
|
||||
if (this.linePos == this.maxLine) {
|
||||
this.queue.resolveAll();
|
||||
return null;
|
||||
}
|
||||
this.line = this.doc.line(this.linePos + 1);
|
||||
this.offset = 0;
|
||||
this.resolveContext();
|
||||
if (skipBlankLine) {
|
||||
if (this.isBlankLine()) {
|
||||
this.queue.resolveAll(this.line.to);
|
||||
while (this.linePos != this.maxLine) {
|
||||
this.line = this.doc.line(this.linePos + 1);
|
||||
if (!this.isBlankLine()) { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.line;
|
||||
}
|
||||
isLastLine() {
|
||||
return this.line.number >= this.doc.lines;
|
||||
}
|
||||
isBlankLine() {
|
||||
return !this.lineStr.trimEnd();
|
||||
}
|
||||
nextCursor(enter = true) {
|
||||
if (this.cursor) {
|
||||
if (this.cursor.next(enter) && this.cursor.name != "Document") { return true }
|
||||
this.cursor = null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
cursorPos(endSide: 0 | -1 = 0): "after" | "before" | "touch" | null {
|
||||
let offset = this.gOffset;
|
||||
if (!this.cursor) { return null }
|
||||
if (offset < this.cursor.from) { return "after" }
|
||||
if (offset > this.cursor.to + endSide) { return "before" }
|
||||
return "touch";
|
||||
}
|
||||
processCursor() {
|
||||
let cursorPos = this.cursorPos(-1);
|
||||
while (cursorPos == "before") {
|
||||
this.nextCursor();
|
||||
cursorPos = this.cursorPos(-1);
|
||||
}
|
||||
if (cursorPos != "touch") { return null }
|
||||
let nodeName = this.cursor!.name;
|
||||
if (
|
||||
this.settings.customHighlight & SettingOpt1.EDITOR_MODE &&
|
||||
nodeName.includes("formatting-highlight")
|
||||
) {
|
||||
return "hl_delim";
|
||||
}
|
||||
if (nodeName.includes("table-sep")) { return "table_sep" }
|
||||
if (SKIPPED_NODE_RE.test(nodeName)) { return "skipped" }
|
||||
return null;
|
||||
}
|
||||
skipCursorRange() {
|
||||
if (!this.cursor) { return false }
|
||||
let cursorTo = this.cursor.to - this.line.from,
|
||||
whitespaceOffset = this.seekWhitespace(cursorTo);
|
||||
if (whitespaceOffset !== null) {
|
||||
this.offset = whitespaceOffset;
|
||||
this.queue.resolve(SpaceRestrictedFormats);
|
||||
}
|
||||
this.offset = cursorTo;
|
||||
return true;
|
||||
}
|
||||
getContext(line = this.line) {
|
||||
if (line.number != this.linePos) {
|
||||
let node = findNode(
|
||||
this.tree, line.from, line.from,
|
||||
(node) => node.parent?.name == "Document"
|
||||
);
|
||||
if (node) { return getContextFromNode(node) }
|
||||
} else if (this.cursorPos() == "touch") {
|
||||
let node = this.cursor!.node;
|
||||
return getContextFromNode(node);
|
||||
}
|
||||
return LineCtx.NONE;
|
||||
}
|
||||
setContext(ctx: LineCtx) {
|
||||
this.prevCtx = this.curCtx;
|
||||
this.curCtx = ctx;
|
||||
}
|
||||
resolveContext() {
|
||||
while (this.cursorPos() == "before") { this.nextCursor() }
|
||||
this.setContext(this.getContext());
|
||||
let isSkip = false,
|
||||
toBeResolved = false,
|
||||
includesHl = false,
|
||||
offset = this.line.from;
|
||||
switch (this.curCtx) {
|
||||
case LineCtx.HR_LINE:
|
||||
case LineCtx.CODEBLOCK:
|
||||
case LineCtx.TABLE_DELIM:
|
||||
isSkip = true;
|
||||
toBeResolved = true;
|
||||
break;
|
||||
case LineCtx.BLOCKQUOTE:
|
||||
if (this.prevCtx != LineCtx.BLOCKQUOTE) {
|
||||
toBeResolved = true;
|
||||
}
|
||||
break;
|
||||
case LineCtx.LIST_HEAD:
|
||||
includesHl = true;
|
||||
// eslint-disable-next-line no-fallthrough
|
||||
case LineCtx.HEADING:
|
||||
case LineCtx.FOOTNOTE_HEAD:
|
||||
toBeResolved = true;
|
||||
break;
|
||||
}
|
||||
switch (this.prevCtx) {
|
||||
case LineCtx.HEADING:
|
||||
case LineCtx.TABLE:
|
||||
toBeResolved = true;
|
||||
offset -= 1;
|
||||
}
|
||||
if (!this.offset) {
|
||||
if (toBeResolved) { this.queue.resolve(NonHighlightFormats, offset) }
|
||||
if (includesHl) { this.queue.resolve([Format.HIGHLIGHT], offset) }
|
||||
}
|
||||
if (isSkip) { this.skipCursorRange() }
|
||||
if (this.curCtx) { this.nextCursor() }
|
||||
}
|
||||
}
|
||||
106
src/editor-mode/parser/TokenQueue.ts
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
import { Format, TokenRole, TokenStatus } from "src/enums";
|
||||
import { MainFormat, Token } from "src/types";
|
||||
import { ParserState } from "src/editor-mode/parser";
|
||||
|
||||
export class TokenQueue {
|
||||
open: Record<MainFormat, Token | null> = {
|
||||
[Format.INSERTION]: null,
|
||||
[Format.SPOILER]: null,
|
||||
[Format.SUPERSCRIPT]: null,
|
||||
[Format.SUBSCRIPT]: null,
|
||||
[Format.HIGHLIGHT]: null,
|
||||
};
|
||||
/**
|
||||
* Content token is generated and pushed automatically
|
||||
* as its opening delimiter enter the queue.
|
||||
*/
|
||||
content: Record<MainFormat, Token | null> = {
|
||||
[Format.INSERTION]: null,
|
||||
[Format.SPOILER]: null,
|
||||
[Format.SUPERSCRIPT]: null,
|
||||
[Format.SUBSCRIPT]: null,
|
||||
[Format.HIGHLIGHT]: null,
|
||||
};
|
||||
close: Record<MainFormat, Token | null> = {
|
||||
[Format.INSERTION]: null,
|
||||
[Format.SPOILER]: null,
|
||||
[Format.SUPERSCRIPT]: null,
|
||||
[Format.SUBSCRIPT]: null,
|
||||
[Format.HIGHLIGHT]: null,
|
||||
};
|
||||
state: ParserState;
|
||||
constructor() {
|
||||
}
|
||||
get tokens() {
|
||||
return this.state.tokens;
|
||||
}
|
||||
attachState(state: ParserState) {
|
||||
this.state = state;
|
||||
state.queue = this;
|
||||
}
|
||||
deattachState() {
|
||||
(this.state as ParserState | undefined) = undefined;
|
||||
}
|
||||
isQueued(type: MainFormat) {
|
||||
return !!this.open[type];
|
||||
}
|
||||
push(token: Token) {
|
||||
if (token.type < Format.INSERTION || token.type == Format.HIGHLIGHT_COLOR_TAG) {
|
||||
throw TypeError("Type of token must be one of MainFormat");
|
||||
}
|
||||
// Used as key to access certain queued token
|
||||
let type = token.type as MainFormat,
|
||||
role = token.role;
|
||||
if (role == TokenRole.OPEN) {
|
||||
this.open[type] = token;
|
||||
} else if (role == TokenRole.CONTENT) {
|
||||
this.content[type] = token;
|
||||
} else if (role == TokenRole.CLOSE) {
|
||||
this.close[type] = token;
|
||||
this.resolve([type]);
|
||||
}
|
||||
}
|
||||
getOpen(type: MainFormat) {
|
||||
return this.open[type];
|
||||
}
|
||||
/**
|
||||
* Resolve type-specific token(s) in the queue. Resolving it means
|
||||
* that the token will no longer be in `PENDING` status. Instead, it
|
||||
* will be stated as `ACTIVE` or `INACTIVE` depending on presence of
|
||||
* closing delimiter in the queue.
|
||||
*/
|
||||
resolve(types: MainFormat[], to = this.state.gOffset) {
|
||||
for (let type of types) {
|
||||
let status: TokenStatus,
|
||||
open = this.open[type],
|
||||
content = this.content[type],
|
||||
close = this.close[type];
|
||||
if (!open) { continue }
|
||||
if (close) {
|
||||
let size = this.tokens.length - open.pointer;
|
||||
status = TokenStatus.ACTIVE;
|
||||
content!.to = close.from;
|
||||
close.status = status;
|
||||
close.size = content!.size = open.size = size;
|
||||
this.close[type] = null;
|
||||
} else {
|
||||
status = TokenStatus.INACTIVE;
|
||||
content!.to = to;
|
||||
}
|
||||
if (type != Format.HIGHLIGHT) {
|
||||
open.status = content!.status = status;
|
||||
}
|
||||
this.open[type] = this.content[type] = null;
|
||||
}
|
||||
}
|
||||
/** Resolve all existing token in the queue. Often used when facing boundary or a blank line. */
|
||||
resolveAll(to = this.state.gOffset) {
|
||||
this.resolve([Format.INSERTION, Format.SPOILER, Format.SUPERSCRIPT, Format.SUBSCRIPT, Format.HIGHLIGHT], to);
|
||||
}
|
||||
clear() {
|
||||
for (let type = Format.INSERTION as MainFormat; type <= Format.HIGHLIGHT; type++) {
|
||||
this.open[type] = this.content[type] = this.close[type] = null;
|
||||
}
|
||||
(this.state as ParserState | undefined) = undefined;
|
||||
}
|
||||
}
|
||||
140
src/editor-mode/parser/Tokenizer.ts
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
import { Format, TokenRole, TokenStatus } from "src/enums";
|
||||
import { ParserState } from "src/editor-mode/parser";
|
||||
import { MainFormat, Token } from "src/types";
|
||||
import { retrieveDelimSpec, validateDelim } from "src/editor-mode/parser/utils";
|
||||
import { COLOR_TAG_RE } from "src/editor-mode/parser/regexps";
|
||||
|
||||
export const Tokenizer = {
|
||||
align(state: ParserState) {
|
||||
let str = state.lineStr, type: Format, length = 0;
|
||||
// If offset isn't zero or the line isn't started with "!!", then it fails
|
||||
if (state.offset || !str.startsWith("!!")) { return false }
|
||||
// Because the line was preceded by "!!", the offset incrased by 2
|
||||
length += 2;
|
||||
switch (true) {
|
||||
case str.startsWith("left", length):
|
||||
type = Format.ALIGN_LEFT;
|
||||
break;
|
||||
case str.startsWith("right", length):
|
||||
type = Format.ALIGN_RIGHT;
|
||||
break;
|
||||
case str.startsWith("center", length):
|
||||
type = Format.ALIGN_CENTER;
|
||||
break;
|
||||
case str.startsWith("justify", length):
|
||||
type = Format.ALIGN_JUSTIFY;
|
||||
break;
|
||||
default:
|
||||
state.advance(length);
|
||||
return false;
|
||||
}
|
||||
length += type + 3;
|
||||
if (!str.startsWith("!!", length)) {
|
||||
state.advance(length);
|
||||
return false;
|
||||
}
|
||||
let lineFrom = state.line.from;
|
||||
length += 2;
|
||||
state.advance(length);
|
||||
state.tokens.push({
|
||||
type,
|
||||
status: TokenStatus.ACTIVE,
|
||||
role: TokenRole.SINGLE,
|
||||
from: lineFrom,
|
||||
to: lineFrom + length,
|
||||
pointer: state.tokens.length,
|
||||
size: 1
|
||||
});
|
||||
return true;
|
||||
},
|
||||
delim(state: ParserState, type: MainFormat) {
|
||||
if (type == Format.HIGHLIGHT) {
|
||||
throw TypeError("");
|
||||
}
|
||||
let role = state.queue.isQueued(type) ? TokenRole.CLOSE : TokenRole.OPEN,
|
||||
spec = retrieveDelimSpec(type, role),
|
||||
{ valid, length } = validateDelim(state.lineStr, state.offset, spec);
|
||||
if (valid) {
|
||||
let token: Token = {
|
||||
type,
|
||||
status: TokenStatus.PENDING,
|
||||
role,
|
||||
from: state.gOffset,
|
||||
to: state.gOffset + length,
|
||||
pointer: state.queue.getOpen(type)?.pointer ?? state.tokens.length,
|
||||
size: 2
|
||||
};
|
||||
state.tokens.push(token);
|
||||
state.queue.push(token);
|
||||
state.advance(length);
|
||||
if (role == TokenRole.OPEN) { Tokenizer.content(state, type) }
|
||||
return true;
|
||||
} else {
|
||||
state.advance(length);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
highlightDelim(state: ParserState) {
|
||||
let { from, to } = state.cursor!,
|
||||
length = to - from,
|
||||
type = Format.HIGHLIGHT,
|
||||
role = state.queue.isQueued(type) ? TokenRole.CLOSE : TokenRole.OPEN,
|
||||
token: Token = {
|
||||
type,
|
||||
status: TokenStatus.ACTIVE,
|
||||
role,
|
||||
from,
|
||||
to,
|
||||
pointer: state.queue.getOpen(type)?.pointer ?? state.tokens.length,
|
||||
size: 2
|
||||
};
|
||||
state.tokens.push(token);
|
||||
state.queue.push(token);
|
||||
state.advance(length);
|
||||
if (role == TokenRole.OPEN) { Tokenizer.content(state, type) }
|
||||
return true;
|
||||
},
|
||||
content(state: ParserState, type: MainFormat) {
|
||||
// content should be indexed right after its opening delimiter
|
||||
let from = state.gOffset;
|
||||
let token: Token = {
|
||||
type,
|
||||
status: TokenStatus.PENDING,
|
||||
role: TokenRole.CONTENT,
|
||||
from,
|
||||
to: from,
|
||||
pointer: state.tokens.length - 1,
|
||||
size: 2
|
||||
};
|
||||
state.tokens.push(token);
|
||||
state.queue.push(token);
|
||||
if (type == Format.HIGHLIGHT) {
|
||||
token.status = TokenStatus.ACTIVE;
|
||||
Tokenizer.colorTag(state);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
colorTag(state: ParserState) {
|
||||
// color tag should be indexed right after mark content,
|
||||
// therefore its index should be plus two of mark opening
|
||||
// index
|
||||
COLOR_TAG_RE.lastIndex = state.offset;
|
||||
let match = state.lineStr.match(COLOR_TAG_RE);
|
||||
if (match) {
|
||||
let tagLen = match[0].length,
|
||||
token: Token = {
|
||||
type: Format.HIGHLIGHT_COLOR_TAG,
|
||||
status: TokenStatus.ACTIVE,
|
||||
role: TokenRole.TAG,
|
||||
from: state.gOffset,
|
||||
to: state.gOffset + tagLen,
|
||||
pointer: state.tokens.length,
|
||||
size: 1
|
||||
};
|
||||
state.tokens.push(token);
|
||||
state.advance(tagLen);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
3
src/editor-mode/parser/configs/EditorDelimLookup.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import { MainFormat } from "src/types";
|
||||
|
||||
export const EditorDelimLookup: Record<string, MainFormat> = {}
|
||||
78
src/editor-mode/parser/configs/ShifterNodeConfigs.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { SyntaxNode } from "@lezer/common";
|
||||
|
||||
export const ShifterNodeConfigs: { [P in string]: {query: string, getOpen: (node: SyntaxNode) => SyntaxNode | null} } = {
|
||||
["table-row"]: {
|
||||
query: "table-row",
|
||||
getOpen(node) {
|
||||
while (!node.name.includes("table-row-0")) {
|
||||
node = node.prevSibling!;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
},
|
||||
["url"]: {
|
||||
query: "url",
|
||||
getOpen(node) {
|
||||
return node;
|
||||
}
|
||||
},
|
||||
["math"]: {
|
||||
query: "math",
|
||||
getOpen(node) {
|
||||
while (!node.name.includes("math-begin")) {
|
||||
node = node.prevSibling!;
|
||||
}
|
||||
if (node.to - node.from != 1) { return null }
|
||||
else { return node }
|
||||
}
|
||||
},
|
||||
["link"]: {
|
||||
query: "link(?<=internal-link)|link(?=-start|end)",
|
||||
getOpen(node) {
|
||||
while (!node.name.includes("link-start")) {
|
||||
node = node.prevSibling!;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
},
|
||||
["formatting-list"]: {
|
||||
query: "formatting-list",
|
||||
getOpen(node) {
|
||||
return node.parent;
|
||||
}
|
||||
},
|
||||
["hr"]: {
|
||||
query: "hr",
|
||||
getOpen(node) {
|
||||
return node;
|
||||
},
|
||||
},
|
||||
["codeblock-begin"]: {
|
||||
query: "codeblock-begin",
|
||||
getOpen(node) {
|
||||
return node;
|
||||
},
|
||||
},
|
||||
["formatting-header"]: {
|
||||
query: "formatting-header",
|
||||
getOpen(node) {
|
||||
node = node.parent!;
|
||||
if (node.name.includes("header-line")) {
|
||||
node = node.prevSibling!;
|
||||
}
|
||||
return node;
|
||||
},
|
||||
},
|
||||
["formatting-quote"]: {
|
||||
query: "formatting-quote",
|
||||
getOpen(node) {
|
||||
return node.parent;
|
||||
}
|
||||
},
|
||||
["hmd-footnote"]: {
|
||||
query: "hmd-footnote",
|
||||
getOpen(node) {
|
||||
return node.parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
src/editor-mode/parser/configs/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from "./ShifterNodeConfigs";
|
||||
export * from "./EditorDelimLookup";
|
||||
4
src/editor-mode/parser/index.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export * from "./Parser";
|
||||
export * from "./ParserState";
|
||||
export * from "./TokenQueue";
|
||||
export * from "./Tokenizer";
|
||||
12
src/editor-mode/parser/regexps/index.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { ShifterNodeConfigs } from "src/editor-mode/parser/configs"
|
||||
|
||||
export const SEMANTIC_INTERFERER_RE = /(?:codeblock|html|math)-(?:begin|end)|comment-(?:start|end)|cdata|tag$/;
|
||||
export const SKIPPED_NODE_RE = /table|code|formatting|escape|html|math|tag|url|barelink|atom|comment|string|meta|frontmatter|hr(?!\w)/;
|
||||
export const COLOR_TAG_RE = /\{[a-z0-9-]+\}/iy;
|
||||
export const SHIFTER_RE = (() => {
|
||||
let queries = "";
|
||||
for (let el in ShifterNodeConfigs) {
|
||||
queries += (queries ? "|" : "") + ShifterNodeConfigs[el as keyof typeof ShifterNodeConfigs].query;
|
||||
}
|
||||
return new RegExp(queries);
|
||||
})();
|
||||
30
src/editor-mode/parser/utils/composeChanges.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { ChangeSet } from "@codemirror/state";
|
||||
import { ChangedRange } from "src/types";
|
||||
|
||||
/**
|
||||
* [id] Mengakumulasi seluruh range yang dimuat oleh
|
||||
* `ChangeSet` untuk menghasilkan `ChangedRange` bila
|
||||
* memang terdapat pengubahan teks. `ChangedRange` dapat
|
||||
* difungsikan sebagai offset permulaan parsing,
|
||||
* menghindari reparsing seluruh dokumen.
|
||||
*/
|
||||
export function composeChanges(changes: ChangeSet): ChangedRange | null {
|
||||
if (changes.empty) {
|
||||
// Bila tidak terdapat pengubahan, hasilkan null
|
||||
return null;
|
||||
}
|
||||
let from: number, initTo: number, changedTo: number;
|
||||
changes.iterChangedRanges((fromA, toA, fromB, toB) => {
|
||||
// [id] Memilih offset terkecil sebagai offset awal pengubahan
|
||||
from = from === undefined ? fromA : Math.min(from, fromA);
|
||||
// [id] Memilih offset terbesar sebagai offset akhir pengubahan
|
||||
initTo = initTo === undefined ? toA : Math.max(initTo, toA);
|
||||
changedTo = changedTo === undefined ? toB : Math.max(changedTo, toB);
|
||||
}, false);
|
||||
return {
|
||||
from: from!,
|
||||
initTo: initTo!,
|
||||
changedTo: changedTo!,
|
||||
length: changedTo! - initTo!
|
||||
};
|
||||
}
|
||||
5
src/editor-mode/parser/utils/disableEscape.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { SKIPPED_NODE_RE } from "../regexps";
|
||||
|
||||
export function disableEscape() {
|
||||
SKIPPED_NODE_RE.compile("table|code|formatting|html|math|tag|url|barelink|atom|comment|string|meta|frontmatter|hr(?!\\w)");
|
||||
}
|
||||
14
src/editor-mode/parser/utils/findNode.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { SyntaxNode, Tree } from "@lezer/common";
|
||||
|
||||
export function findNode(tree: Tree, from: number, to: number, matcher: (node: SyntaxNode) => boolean): SyntaxNode | null {
|
||||
let node: SyntaxNode | null = null;
|
||||
tree.iterate({
|
||||
from, to, enter(nodeRef) {
|
||||
if (matcher(nodeRef.node)) {
|
||||
node = nodeRef.node;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
return node;
|
||||
}
|
||||
24
src/editor-mode/parser/utils/findShifterAt.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { SyntaxNode, Tree } from "@lezer/common";
|
||||
import { NodeSpec } from "src/types";
|
||||
import { SHIFTER_RE } from "src/editor-mode/parser/regexps";
|
||||
|
||||
export function findShifterAt(tree: Tree, offset: number): NodeSpec | null {
|
||||
let node: SyntaxNode | null = null,
|
||||
type: string | null = null;
|
||||
tree.iterate({
|
||||
from: offset, to: offset,
|
||||
enter(nodeRef) {
|
||||
if (nodeRef.name == "Document") { return }
|
||||
let match = SHIFTER_RE.exec(nodeRef.name);
|
||||
if (match) {
|
||||
node = nodeRef.node;
|
||||
type = match[0];
|
||||
return false;
|
||||
}
|
||||
},
|
||||
});
|
||||
if (node && type) {
|
||||
return { node, type }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
40
src/editor-mode/parser/utils/getContextFromNode.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { SyntaxNode } from "@lezer/common";
|
||||
import { LineCtx } from "src/enums";
|
||||
|
||||
export function getContextFromNode(node: SyntaxNode) {
|
||||
let nodeName = node.name,
|
||||
context = LineCtx.NONE;
|
||||
if (node.name == "Document") { return context }
|
||||
if (!node.name.startsWith("HyperMD")) {
|
||||
if (node.parent!.name == "Document") { return context }
|
||||
node = node.parent!;
|
||||
}
|
||||
if (nodeName.startsWith("hr", 8)) {
|
||||
context = LineCtx.HR_LINE;
|
||||
} else if (nodeName.startsWith("header", 8)) {
|
||||
context = LineCtx.HEADING;
|
||||
} else if (nodeName.startsWith("quote", 8)) {
|
||||
context = LineCtx.BLOCKQUOTE;
|
||||
} else if (nodeName.startsWith("codeblock", 8)) {
|
||||
context = LineCtx.CODEBLOCK;
|
||||
} else if (nodeName.startsWith("list")) {
|
||||
if (nodeName.includes("nobullet")) {
|
||||
context = LineCtx.LIST;
|
||||
} else {
|
||||
context = LineCtx.LIST_HEAD;
|
||||
}
|
||||
} else if (nodeName.startsWith("footnote", 8)) {
|
||||
if (node.firstChild?.name.includes("footnote")) {
|
||||
context = LineCtx.FOOTNOTE_HEAD;
|
||||
} else {
|
||||
context = LineCtx.FOOTNOTE;
|
||||
}
|
||||
} else if (nodeName.startsWith("table", 8)) {
|
||||
if (nodeName.includes("table-row-1")) {
|
||||
context = LineCtx.TABLE_DELIM;
|
||||
} else {
|
||||
context = LineCtx.TABLE;
|
||||
}
|
||||
}
|
||||
return context;
|
||||
}
|
||||
12
src/editor-mode/parser/utils/getShifterStart.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { SyntaxNode } from "@lezer/common";
|
||||
import { NodeSpec } from "src/types";
|
||||
import { ShifterNodeConfigs } from "src/editor-mode/parser/configs";
|
||||
|
||||
export function getShifterStart(spec: NodeSpec) {
|
||||
let node: SyntaxNode | null = spec.node,
|
||||
type = spec.type;
|
||||
if (node = ShifterNodeConfigs[type]?.getOpen(node)) {
|
||||
return node.from;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
18
src/editor-mode/parser/utils/hasInterferer.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { SyntaxNode, Tree } from "@lezer/common";
|
||||
import { SEMANTIC_INTERFERER_RE } from "src/editor-mode/parser/regexps";
|
||||
import { findNode } from "src/editor-mode/parser/utils";
|
||||
|
||||
export function hasInterferer(tree: Tree, from: number, to: number) {
|
||||
let matcher = (node: SyntaxNode) => {
|
||||
let match = SEMANTIC_INTERFERER_RE.exec(node.name);
|
||||
if (match) {
|
||||
if (
|
||||
(match[0] == "math-begin" || match[0] == "math-end") &&
|
||||
node.to - node.from < 2
|
||||
) { return false }
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
return !!findNode(tree, from, to, matcher);
|
||||
}
|
||||
9
src/editor-mode/parser/utils/index.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
export * from "./composeChanges";
|
||||
export * from "./findNode";
|
||||
export * from "./findShifterAt";
|
||||
export * from "./getContextFromNode";
|
||||
export * from "./getShifterStart";
|
||||
export * from "./hasInterferer";
|
||||
export * from "./retrieveDelimSpec";
|
||||
export * from "./validateDelim";
|
||||
export * from "./disableEscape";
|
||||
11
src/editor-mode/parser/utils/retrieveDelimSpec.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { Format, TokenRole } from "src/enums";
|
||||
import { DelimSpec, MainFormat } from "src/types";
|
||||
import { FormatRules } from "src/shared-configs";
|
||||
|
||||
export function retrieveDelimSpec(type: MainFormat, role: TokenRole): DelimSpec {
|
||||
if (type == Format.HIGHLIGHT) {
|
||||
throw TypeError("Type must be either insertion, spoiler, sup, or sub");
|
||||
}
|
||||
let { char, length, exactLen } = FormatRules[type];
|
||||
return { char, length, exactLen, role }
|
||||
}
|
||||
19
src/editor-mode/parser/utils/validateDelim.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { TokenRole } from "src/enums";
|
||||
import { DelimSpec } from "src/types";
|
||||
|
||||
export function validateDelim(str: string, offset: number, spec: DelimSpec) {
|
||||
let length = 0, valid = false;
|
||||
while (str[offset + length] == spec.char) { length++ }
|
||||
if (spec.exactLen && length == spec.length || !spec.exactLen && length >= spec.length) {
|
||||
let char: string;
|
||||
if (spec.role == TokenRole.OPEN) {
|
||||
char = str[offset + length];
|
||||
} else if (spec.role == TokenRole.CLOSE) {
|
||||
char = str[offset - 1];
|
||||
} else {
|
||||
throw TypeError("");
|
||||
}
|
||||
if (char && char != " " && char != "\t") { valid = true }
|
||||
}
|
||||
return { valid, length };
|
||||
}
|
||||
|
|
@ -1,456 +0,0 @@
|
|||
import { EditorSelection } from "@codemirror/state";
|
||||
import { Format, TokenLevel } from "src/enums";
|
||||
import { IndexCache, PlainRange, Token, TokenGroup } from "src/types";
|
||||
import { EditorParser } from "src/editor-mode/preprocessor/parser";
|
||||
import { Region, joinRegions } from "src/editor-mode/utils/range-utils";
|
||||
import { isInlineFormat } from "src/format-configs/format-utils";
|
||||
|
||||
/**
|
||||
* Appliance for managing selection-based decorations that are tied to
|
||||
* the tokens effectively, avoiding, in some cases, over-iteration and
|
||||
* redrawing decorations which indeed aren't selected and can be reused
|
||||
* again. Only applied to the line breaks and fenced div opening
|
||||
* decorations, which we can't bound decorating them with the viewport,
|
||||
* not even visibleRanges. Although, it's still useful for formatting
|
||||
* commands for both block and inline tokens.
|
||||
*/
|
||||
export class SelectionObserver {
|
||||
/** Tokens that are touched or intersected by selection. */
|
||||
public selectedRegions: Record<TokenLevel, Region> = {
|
||||
[TokenLevel.INLINE]: [],
|
||||
[TokenLevel.BLOCK]: []
|
||||
}
|
||||
|
||||
/**
|
||||
* All selection-based decorations, e.g. omitted delimiters, that are
|
||||
* associated with these tokens should be redrawn.
|
||||
*/
|
||||
public changedRegions: Record<TokenLevel, Region> = {
|
||||
[TokenLevel.INLINE]: [],
|
||||
[TokenLevel.BLOCK]: []
|
||||
}
|
||||
|
||||
/**
|
||||
* Collection of offset ranges, to be used as filter ranges for the
|
||||
* previously drawn selection-based decorations.
|
||||
*/
|
||||
public filterRegions: Record<TokenLevel, Region> = {
|
||||
[TokenLevel.INLINE]: [],
|
||||
[TokenLevel.BLOCK]: []
|
||||
}
|
||||
|
||||
private _indexCaches: Record<TokenLevel | "selection", IndexCache> = {
|
||||
[TokenLevel.INLINE]: { number: 0 },
|
||||
[TokenLevel.BLOCK]: { number: 0 },
|
||||
selection: { number: 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapped indexes of tokens to each index of corresponding selection that
|
||||
* is touched by them. For example, `maps[0]` contains all indexes of
|
||||
* tokens that touched the first selection. May be empty if corresponding
|
||||
* selection wasn't touched by any token.
|
||||
*/
|
||||
private _maps: Partial<Record<TokenLevel, number[]>>[] = [];
|
||||
|
||||
public selection: EditorSelection;
|
||||
public readonly parser: EditorParser;
|
||||
|
||||
constructor(parser: EditorParser) {
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Observe given selection to catch tokens that touched it, then map the
|
||||
* old selected region with the new one, producing latest changed region
|
||||
* that can be used to produce RangeSet filter.
|
||||
*
|
||||
* @param isParsing When assigned to true, observer will reach into
|
||||
* reparsed ranges produced by the parser, and join it with the
|
||||
* selectedRegion, resulting fresh changedRegion.
|
||||
*
|
||||
* @param restart Restart the observer to its initial state. You
|
||||
* shouldn't pass any value into it. Use restartObserver() instead.
|
||||
*/
|
||||
public observe(selection: EditorSelection, isParsing: boolean, restart: boolean = false): void {
|
||||
this.selection = selection;
|
||||
this._checkIndexCache();
|
||||
|
||||
for (let level = TokenLevel.BLOCK as TokenLevel; level <= TokenLevel.INLINE; level++) {
|
||||
let oldSelectedRegion = this.selectedRegions[level];
|
||||
this._locateSelectedTokens(level);
|
||||
|
||||
// At the moment, changed and filter region are only applied to block-
|
||||
// level tokens.
|
||||
if (level == TokenLevel.INLINE) continue;
|
||||
this._mapChangedRegion(level, oldSelectedRegion, isParsing, restart);
|
||||
this.createFilter(level);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart the observer to its initial state, i.e. clear old selected
|
||||
* region and reobserve given selection.
|
||||
*/
|
||||
public restartObserver(selection: EditorSelection, isParsing: boolean): void {
|
||||
this.selectedRegions = {
|
||||
[TokenLevel.BLOCK]: [],
|
||||
[TokenLevel.INLINE]: []
|
||||
};
|
||||
this.observe(selection, isParsing, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate region of the tokens that touch or intersect the cursor or
|
||||
* selection.
|
||||
*/
|
||||
private _locateSelectedTokens(level: TokenLevel): Region {
|
||||
let newSelectedRegion: Region = [],
|
||||
curRange: PlainRange | undefined;
|
||||
this._clearMaps();
|
||||
|
||||
// Moving the cached index to the fore edge of the selection and then
|
||||
// starting to look ahead involved tokens is not as efficient as way that
|
||||
// look involved tokens behind without altering the cached index. Note
|
||||
// that the efficiency of this process isn't so noticable in case of few
|
||||
// tokens exist.
|
||||
this._lookBehind(level, (_, index, selectionIndex) => {
|
||||
if (!this._maps[selectionIndex]?.[level]) {
|
||||
this._maps[selectionIndex] = {
|
||||
[level]: [index]
|
||||
};
|
||||
} else {
|
||||
this._maps[selectionIndex][level].unshift(index);
|
||||
}
|
||||
if (!curRange || curRange.from != index + 1) {
|
||||
curRange = { from: index, to: index + 1 };
|
||||
newSelectedRegion.unshift(curRange);
|
||||
} else {
|
||||
curRange.from--;
|
||||
}
|
||||
});
|
||||
|
||||
curRange = newSelectedRegion.at(-1);
|
||||
|
||||
this._lookAhead(level, (_, index, selectionIndex) => {
|
||||
if (!this._maps[selectionIndex]?.[level]) {
|
||||
this._maps[selectionIndex] = {
|
||||
[level]: [index]
|
||||
};
|
||||
} else {
|
||||
this._maps[selectionIndex][level].push(index);
|
||||
}
|
||||
if (!curRange || curRange.to != index) {
|
||||
curRange = { from: index, to: index + 1 };
|
||||
newSelectedRegion.push(curRange);
|
||||
} else {
|
||||
curRange.to++;
|
||||
}
|
||||
});
|
||||
|
||||
this._indexCaches[level].number = newSelectedRegion[0]?.from ?? this._indexCaches[level].number;
|
||||
return this.selectedRegions[level] = newSelectedRegion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will move the cached index to the last token if it's went out of the
|
||||
* range.
|
||||
*/
|
||||
private _checkIndexCache(): void {
|
||||
if (this._indexCaches[TokenLevel.INLINE].number >= this.parser.inlineTokens.length)
|
||||
this._indexCaches[TokenLevel.INLINE].number = Math.max(this.parser.inlineTokens.length - 1, 0);
|
||||
|
||||
if (this._indexCaches[TokenLevel.BLOCK].number >= this.parser.blockTokens.length)
|
||||
this._indexCaches[TokenLevel.BLOCK].number = Math.max(this.parser.blockTokens.length - 1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look the tokens behind untill the start offset of the first selection.
|
||||
* Runs the callback when the current token touches the selection.
|
||||
*/
|
||||
private _lookBehind(level: TokenLevel, callback: (token: Token, index: number, selectionIndex: number) => unknown): void {
|
||||
let selectionRanges = this.selection.ranges,
|
||||
selectionIndex = selectionRanges.length - 1,
|
||||
tokens = this.parser.getTokens(level),
|
||||
goalOffset = selectionRanges[0].from;
|
||||
|
||||
// Using index taken from the cache as an anchor.
|
||||
for (let i = this._indexCaches[level].number - 1; i >= 0 && goalOffset <= tokens[i].to; i--) {
|
||||
while (selectionRanges[selectionIndex].from > tokens[i].to) { selectionIndex-- }
|
||||
if (!selectionRanges[selectionIndex]) { break }
|
||||
if (selectionRanges[selectionIndex].to < tokens[i].from) { continue }
|
||||
callback(tokens[i], i, selectionIndex);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Look the tokens ahead untill the end offset of the last selection.
|
||||
* Runs the callback when the current token touches the selection.
|
||||
*/
|
||||
private _lookAhead(level: TokenLevel, callback: (token: Token, index: number, selectionIndex: number) => boolean | void): void {
|
||||
let selectionRanges = this.selection.ranges,
|
||||
selectionIndex = 0,
|
||||
tokens = this.parser.getTokens(level),
|
||||
goalOffset = selectionRanges.at(-1)!.to;
|
||||
|
||||
// Using index taken from the cache as an anchor.
|
||||
for (let i = this._indexCaches[level].number; i < tokens.length && goalOffset >= tokens[i].from; i++) {
|
||||
while (selectionRanges[selectionIndex].to < tokens[i].from) { selectionIndex++ }
|
||||
if (!selectionRanges[selectionIndex]) { break }
|
||||
if (selectionRanges[selectionIndex].from > tokens[i].to) { continue }
|
||||
callback(tokens[i], i, selectionIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* When the cursor or selection was moved or changed, `DelimOmitter` must
|
||||
* redraw the `HiddenWidget` in order to decide which delimiter should be
|
||||
* hidden due to its syntax is touching the cursor/selection. To make
|
||||
* redrawing process more efficient, obviously because of possibility
|
||||
* that there is delimiter(s) that's still be hidden, we should only
|
||||
* redraw what have been changed by the move of the cursor/selection, or
|
||||
* by a document change. Hence, `mapChangedRegion` comes to map the
|
||||
* token indexes region that should be redrawn.
|
||||
*/
|
||||
private _mapChangedRegion(level: TokenLevel, oldSelectedRegion: Region, isParsing: boolean, restart?: boolean): Region {
|
||||
let reparsedRange = this.parser.reparsedRanges[level],
|
||||
reparsedLength = reparsedRange.changedTo - reparsedRange.initTo,
|
||||
mappedRegion: Region = [],
|
||||
tokensAdded = reparsedRange.from != reparsedRange.changedTo,
|
||||
tokensLen = this.parser.getTokens(level).length;
|
||||
if (restart)
|
||||
return this.changedRegions[level] = tokensLen
|
||||
? [{ from: 0, to: tokensLen }]
|
||||
: [];
|
||||
|
||||
// Don't map the previous selected region when there is actually no
|
||||
// parsing activity.
|
||||
if (!isParsing || reparsedRange.from == reparsedRange.initTo && !reparsedLength)
|
||||
return this.changedRegions[level] = joinRegions(oldSelectedRegion, this.selectedRegions[level]);
|
||||
|
||||
// Use either reparsed range or selected region directly when there is no
|
||||
// token selected before.
|
||||
if (!oldSelectedRegion.length) {
|
||||
if (tokensAdded)
|
||||
return this.changedRegions[level] = joinRegions([{ from: reparsedRange.from, to: reparsedRange.changedTo }], this.selectedRegions[level]);
|
||||
|
||||
return this.changedRegions[level] = this.selectedRegions[level];
|
||||
}
|
||||
|
||||
// We should map the old selected region with the reparsed range before
|
||||
// joinning it with the new one.
|
||||
for (let i = 0, isTouched = false; i < oldSelectedRegion.length;) {
|
||||
if (oldSelectedRegion[i].to < reparsedRange.from) {
|
||||
mappedRegion.push(Object.assign({}, oldSelectedRegion[i]));
|
||||
if (++i >= oldSelectedRegion.length) {
|
||||
if (tokensAdded)
|
||||
mappedRegion.push({ from: reparsedRange.from, to: reparsedRange.changedTo });
|
||||
break;
|
||||
}
|
||||
} else if (oldSelectedRegion[i].from < reparsedRange.initTo) {
|
||||
isTouched = true;
|
||||
let from = Math.min(reparsedRange.from, oldSelectedRegion[i].from),
|
||||
to = Math.max(reparsedRange.changedTo, oldSelectedRegion[i].to + reparsedLength);
|
||||
if (from != to)
|
||||
mappedRegion.push({ from, to });
|
||||
do { i++ } while (i < oldSelectedRegion.length && oldSelectedRegion[i].from < reparsedRange.initTo)
|
||||
} else {
|
||||
if (!isTouched && tokensAdded)
|
||||
mappedRegion.push({ from: reparsedRange.from, to: reparsedRange.changedTo });
|
||||
do {
|
||||
mappedRegion.push({
|
||||
from: oldSelectedRegion[i].from + reparsedLength,
|
||||
to: oldSelectedRegion[i].to + reparsedLength
|
||||
});
|
||||
i++;
|
||||
} while (i < oldSelectedRegion.length)
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize the mapping of the changed region.
|
||||
return this.changedRegions[level] = joinRegions(mappedRegion, this.selectedRegions[level]);
|
||||
}
|
||||
|
||||
/** Empty any of index maps. */
|
||||
private _clearMaps(): void {
|
||||
this._maps = new Array(this.selection.ranges.length).map(() => ({}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Will move the cached index of the selections to the last index if it
|
||||
* exceeds the amount of the selection ranges.
|
||||
*/
|
||||
private _checkSelectionIndexCache(): void {
|
||||
if (this._indexCaches.selection.number >= this.selection.ranges.length)
|
||||
this._indexCaches.selection.number = this.selection.ranges.length - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create filter region that will be used as filtering boundary in
|
||||
* `RangeSet<Decoration>.filter()`.
|
||||
*/
|
||||
public createFilter(level: TokenLevel): Region {
|
||||
let tokens = level == TokenLevel.INLINE
|
||||
? this.parser.inlineTokens
|
||||
: this.parser.blockTokens;
|
||||
if (!tokens.length)
|
||||
return this.filterRegions[level] = [Object.assign({}, this.parser.lastStreamPoint)];
|
||||
|
||||
let filterRegion: Region = [],
|
||||
changedRegion = this.changedRegions[level],
|
||||
// Inline tokens can be touched each other. To avoid filtering uninvolved
|
||||
// decoration, filtering offset should be more inwardly indented. Doing
|
||||
// so in block-level decorations makes filtering miss them due to
|
||||
// frontmost position and using line decoration which the actually length
|
||||
// is 0.
|
||||
side = level == TokenLevel.INLINE ? 1 : 0;
|
||||
|
||||
for (let i = 0; i < changedRegion.length; i++) {
|
||||
let range = changedRegion[i],
|
||||
filterFrom: number,
|
||||
filterTo: number;
|
||||
// Sometimes, the range can exceed the length of the tokens.
|
||||
if (range.to > tokens.length) {
|
||||
if (range.from >= tokens.length)
|
||||
filterFrom = this.parser.lastStreamPoint.from;
|
||||
else
|
||||
filterFrom = tokens[range.from].from + side;
|
||||
filterTo = this.parser.lastStreamPoint.to;
|
||||
} else {
|
||||
filterFrom = tokens[range.from].from + side;
|
||||
filterTo = tokens[range.to - 1].to - side;
|
||||
}
|
||||
filterRegion.push({ from: filterFrom, to: filterTo });
|
||||
}
|
||||
|
||||
return this.filterRegions[level] = filterRegion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over tokens involved in the changed region. `to` in each range
|
||||
* isn't included in the iteration.
|
||||
*/
|
||||
public iterateChangedRegion(level: TokenLevel, callback?: (token: Token, index: number, tokens: TokenGroup, inSelection: boolean) => unknown): void {
|
||||
let tokens = this.parser.getTokens(level),
|
||||
changedRegion = this.changedRegions[level],
|
||||
selectedRegion = this.selectedRegions[level];
|
||||
for (let i = 0, j = 0; i < changedRegion.length; i++) {
|
||||
let changedRange = changedRegion[i];
|
||||
for (let index = changedRange.from; index < changedRange.to; index++) {
|
||||
// Checking whether the current token was in selection or not. We don't
|
||||
// use looping directly to check inSelection state, due to the fact that
|
||||
// changed region was a union of the selected region and the others.
|
||||
let inSelection = false;
|
||||
if (selectedRegion[j] && selectedRegion[j].to <= index) { j++ }
|
||||
if (selectedRegion[j] && selectedRegion[j].from <= index) {
|
||||
inSelection = true;
|
||||
}
|
||||
// Possibly to be undefined, espically when the last token was removed.
|
||||
if (!tokens[index]) { continue }
|
||||
callback?.(tokens[index], index, tokens, inSelection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate over tokens that are inside, intersected by, or touched by
|
||||
* selection. `to` in each range isn't included in the iteration. Return
|
||||
* it `false` to cut off the iteration.
|
||||
*
|
||||
* @param watchPos - If true, the last argument of the callback will be
|
||||
* passed, either `"covered"` (selection covers all across the range
|
||||
* of the token), `"intersect"` (selection covers a part of the token),
|
||||
* `"adjacent"` (selection just touches one of its edges), or
|
||||
* `"covering"` (range of the selection being covered by the token).
|
||||
* Otherwise, will be passed as `undefined`.
|
||||
*/
|
||||
public iterateSelectedRegion(
|
||||
level: TokenLevel,
|
||||
watchPos: boolean,
|
||||
callback?: (token: Token, index: number, tokens: TokenGroup, pos: undefined | "covered" | "intersect" | "covering" | "adjacent") => void | boolean
|
||||
) {
|
||||
let tokens = this.parser.getTokens(level),
|
||||
selectionRanges = this.selection.ranges,
|
||||
selectedRegion = this.selectedRegions[level];
|
||||
|
||||
// i => selected range index
|
||||
// j => selection range index
|
||||
// k => token index
|
||||
for (let i = 0, j = 0; i < selectedRegion.length; i++) {
|
||||
let selectedRange = selectedRegion[i];
|
||||
for (let k = selectedRange.from; k < selectedRange.to; k++) {
|
||||
let token = tokens[k],
|
||||
pos: "covered" | "intersect" | "adjacent" | "covering" | undefined;
|
||||
|
||||
// Withdraw selection range index and reassign it by the first selection
|
||||
// index that touched current token, if the current token shared the same
|
||||
// selection with the previous one.
|
||||
for (let prevIndex = j - 1; prevIndex >= 0; prevIndex--) {
|
||||
if (selectionRanges[prevIndex].to < token.from) break;
|
||||
if (selectionRanges[prevIndex].from <= token.to) j = prevIndex;
|
||||
}
|
||||
|
||||
// There is no token that isn't touched by any selection at least.
|
||||
while (token.to < selectionRanges[j].from) j++;
|
||||
|
||||
// A single token could be touched by more than one selection.
|
||||
while (selectionRanges[j] && token.to >= selectionRanges[j].from) {
|
||||
if (watchPos) {
|
||||
// Relative position precedence order:
|
||||
// covered -> covering -> intersect -> adjacent
|
||||
if (pos != "covered" && token.from >= selectionRanges[j].from && token.to <= selectionRanges[j].to) {
|
||||
pos = "covered";
|
||||
} else if (pos != "covering" && token.from < selectionRanges[j].from && token.to > selectionRanges[j].to) {
|
||||
pos = "covering";
|
||||
} else if (pos === undefined && (token.from == selectionRanges[j].to || token.to == selectionRanges[j].from)) {
|
||||
pos = "adjacent";
|
||||
} else if (pos === undefined || pos === "adjacent") {
|
||||
pos = "intersect";
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
|
||||
// If callback returned false, cut the iteration off.
|
||||
if (callback?.(token, k, tokens, pos) === false) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Check that the given range touches the current selection or not. */
|
||||
public touchSelection(from: number, to: number): boolean {
|
||||
this._checkSelectionIndexCache();
|
||||
let selectionRanges = this.selection.ranges,
|
||||
// The index cache seems to have a significant effect in the case of many
|
||||
// of the selections were applied at the same time.
|
||||
indexCache = this._indexCaches.selection,
|
||||
curRange = selectionRanges[indexCache.number] ?? selectionRanges.at(-1)!;
|
||||
|
||||
if (to < curRange.from) {
|
||||
while (indexCache.number >= 0) {
|
||||
if (from <= curRange.to && to >= curRange.from) { return true }
|
||||
curRange = selectionRanges[--indexCache.number];
|
||||
}
|
||||
indexCache.number = 0;
|
||||
}
|
||||
|
||||
else if (from > curRange.to) {
|
||||
while (indexCache.number < selectionRanges.length) {
|
||||
if (from <= curRange.to && to >= curRange.from) { return true }
|
||||
curRange = selectionRanges[++indexCache.number];
|
||||
}
|
||||
indexCache.number = selectionRanges.length - 1;
|
||||
}
|
||||
|
||||
else
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Pick index map of selected tokens based on their type. */
|
||||
public pickMaps(type: Format): (number[] | undefined)[] {
|
||||
let level = isInlineFormat(type) ? TokenLevel.INLINE : TokenLevel.BLOCK,
|
||||
tokens = this.parser.getTokens(level);
|
||||
return this._maps.map(maps => maps[level]?.filter(tokenIndex => tokens[tokenIndex].type == type));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
import { SyntaxNode } from "@lezer/common";
|
||||
import { InlineFormat } from "src/types";
|
||||
|
||||
export const EditorDelimLookup: Record<string, InlineFormat> = {}
|
||||
|
||||
export const ShifterNodeConfigs: Record<string, { query: string, getOpen: (node: SyntaxNode) => SyntaxNode | null }> = {
|
||||
["table-row"]: {
|
||||
query: "table-row",
|
||||
getOpen(node) {
|
||||
while (!node.name.includes("table-row-0")) {
|
||||
node = node.prevSibling!;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
},
|
||||
["url"]: {
|
||||
query: "url",
|
||||
getOpen(node) {
|
||||
return node;
|
||||
}
|
||||
},
|
||||
["math"]: {
|
||||
query: "math",
|
||||
getOpen(node) {
|
||||
while (!node.name.includes("math-begin")) {
|
||||
let prevSibling = node.prevSibling ?? node.parent?.prevSibling?.lastChild ?? null;
|
||||
if (!prevSibling) { return null }
|
||||
node = prevSibling;
|
||||
}
|
||||
if (node.to - node.from != 1) { return null }
|
||||
else { return node }
|
||||
}
|
||||
},
|
||||
["link"]: {
|
||||
query: "link(?<=internal-link)|link(?=-start|end)",
|
||||
getOpen(node) {
|
||||
while (!node.name.includes("link-start")) {
|
||||
node = node.prevSibling!;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
},
|
||||
["formatting-list"]: {
|
||||
query: "formatting-list",
|
||||
getOpen(node) {
|
||||
return node.parent;
|
||||
}
|
||||
},
|
||||
["hr"]: {
|
||||
query: "hr",
|
||||
getOpen(node) {
|
||||
return node;
|
||||
},
|
||||
},
|
||||
["codeblock-begin"]: {
|
||||
query: "codeblock-begin",
|
||||
getOpen(node) {
|
||||
return node;
|
||||
},
|
||||
},
|
||||
["formatting-header"]: {
|
||||
query: "formatting-header",
|
||||
getOpen(node) {
|
||||
node = node.parent!;
|
||||
if (node.name.includes("header-line")) {
|
||||
node = node.prevSibling!;
|
||||
}
|
||||
return node;
|
||||
},
|
||||
},
|
||||
["formatting-quote"]: {
|
||||
query: "formatting-quote",
|
||||
getOpen(node) {
|
||||
return node.parent;
|
||||
}
|
||||
},
|
||||
["hmd-footnote"]: {
|
||||
query: "hmd-footnote",
|
||||
getOpen(node) {
|
||||
return node.parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const SEMANTIC_INTERFERER_RE = /(?:codeblock|html|math)-(?:begin|end)|comment-(?:start|end)|cdata|tag$/;
|
||||
export const SKIPPED_NODE_RE = /table|code|formatting|escape|html|math|tag|url|barelink|atom|comment|string|meta|frontmatter|internal-link|hr(?!\w)/;
|
||||
export const COLOR_TAG_RE = /\{[a-z0-9-]+\}/iy;
|
||||
export const SHIFTER_RE = (() => {
|
||||
let queries = "";
|
||||
for (let el in ShifterNodeConfigs) {
|
||||
queries += (queries ? "|" : "") + ShifterNodeConfigs[el as keyof typeof ShifterNodeConfigs].query;
|
||||
}
|
||||
return new RegExp(queries);
|
||||
})();
|
||||
|
|
@ -1,790 +0,0 @@
|
|||
import { ChangeSet, Line, Text } from "@codemirror/state";
|
||||
import { Tree, TreeCursor } from "@lezer/common";
|
||||
import { PluginSettings, TokenGroup, ChangedRange, PlainRange, InlineFormat, Token } from "src/types";
|
||||
import { Format, LineCtx, MarkdownViewMode, TokenLevel, TokenStatus } from "src/enums";
|
||||
import { Formats, InlineRules } from "src/format-configs/rules";
|
||||
import { handleFencedDivTag, handleInlineTag, Tokenizer } from "src/editor-mode/preprocessor/tokenizer";
|
||||
import { findNode, getContextFromNode, disableEscape, findShifterAt, getShifterStart, hasInterferer, reenableEscape } from "src/editor-mode/preprocessor/syntax-node";
|
||||
import { EditorDelimLookup, SKIPPED_NODE_RE } from "src/editor-mode/preprocessor/parser-configs";
|
||||
import { isBlankLine, getBlockEndAt, TextCursor } from "src/editor-mode/utils/doc-utils";
|
||||
import { findTokenIndexAt, provideTokenPartsRanges } from "src/editor-mode/utils/token-utils";
|
||||
|
||||
/**
|
||||
* Used for (re)configuring the state, especially in
|
||||
* the case of document or tree change
|
||||
*/
|
||||
type ParserStateConfig = {
|
||||
doc: Text,
|
||||
tree: Tree,
|
||||
startAt: number,
|
||||
stopAt?: number | Line
|
||||
settings: PluginSettings,
|
||||
}
|
||||
|
||||
function _isTerminalChar(char: string, settings: PluginSettings): boolean {
|
||||
for (let definedDelim in EditorDelimLookup)
|
||||
if (char == definedDelim) return true;
|
||||
|
||||
if (settings.fencedDiv & MarkdownViewMode.EDITOR_MODE && char == ":")
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function _composeChanges(changes: ChangeSet): ChangedRange | null {
|
||||
if (changes.empty) {
|
||||
// Bila tidak terdapat pengubahan, hasilkan null
|
||||
return null;
|
||||
}
|
||||
let from: number, initTo: number, changedTo: number;
|
||||
changes.iterChangedRanges((fromA, toA, _, toB) => {
|
||||
// [id] Memilih offset terkecil sebagai offset awal pengubahan
|
||||
from = from === undefined ? fromA : Math.min(from, fromA);
|
||||
// [id] Memilih offset terbesar sebagai offset akhir pengubahan
|
||||
initTo = initTo === undefined ? toA : Math.max(initTo, toA);
|
||||
changedTo = changedTo === undefined ? toB : Math.max(changedTo, toB);
|
||||
}, false);
|
||||
return {
|
||||
from: from!,
|
||||
initTo: initTo!,
|
||||
changedTo: changedTo!,
|
||||
length: changedTo! - initTo!
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Interferer node is node that, if it was found within the changed range
|
||||
* the end offset of stream will be moved forward to the end offset of
|
||||
* the new tree. That's including the delimiters of codeblock, mathblock,
|
||||
* or comment block.
|
||||
*/
|
||||
function _checkInterferer(changedRange: ChangedRange, newTree: Tree, oldTree: Tree): boolean {
|
||||
return (
|
||||
hasInterferer(newTree, changedRange.from, changedRange.changedTo) ||
|
||||
hasInterferer(oldTree, changedRange.from, changedRange.initTo)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A place storing token based on its type, to be resolved through the
|
||||
* `EditorParser` and `EditorParserState` when satisfies certain
|
||||
* conditions, such as the token finally reaches its closing delimiter or
|
||||
* faces a context boundary.
|
||||
*/
|
||||
class _TokenQueue {
|
||||
/** Contains all queued tokens (if any), each is paired by its format type. */
|
||||
private _tokenMap: Partial<Record<Format, Token>> = {};
|
||||
private _state: EditorParserState;
|
||||
|
||||
/**
|
||||
* Attach a state to the queue. Often used when
|
||||
* initializing the parsing.
|
||||
*/
|
||||
public attachState(state: EditorParserState): void {
|
||||
this._state = state;
|
||||
this._state.queue = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detach currently attached state from the queue. Often used
|
||||
* when the parsing was done.
|
||||
*/
|
||||
public detachState(): void {
|
||||
(this._state.queue as unknown) = undefined;
|
||||
(this._state as unknown) = undefined;
|
||||
}
|
||||
|
||||
/** Checking whether the token with `type` format is queued or not. */
|
||||
public isQueued(type: Format): boolean {
|
||||
return !!this._tokenMap[type];
|
||||
}
|
||||
|
||||
/** Push a token into the queue, exactly into the token map. */
|
||||
public push(token: Token): void {
|
||||
// Any token pushed into the queue will instantly be stated as `PENDING`.
|
||||
token.status = TokenStatus.PENDING;
|
||||
this._tokenMap[token.type] = token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get queued token as specified by `type` parameter.
|
||||
* Returns `null` if it isn't queued.
|
||||
*/
|
||||
public getToken(type: Format): Token | null {
|
||||
return this._tokenMap[type] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve type-specific token(s) in the queue. Resolving it means
|
||||
* that the token will no longer be in `PENDING` status. Instead, it
|
||||
* will be stated as `ACTIVE` or `INACTIVE` depending on presence of
|
||||
* closing delimiter, if it is required for that. Then, resolved token
|
||||
* will be ejected from the map.
|
||||
*
|
||||
* @param closed If false and the token's type requires to be closed,
|
||||
* then the token will be resolved as `INACTIVE`. Otherwise, it will
|
||||
* be stated as `ACTIVE`.
|
||||
*
|
||||
* @param closedByBlankLine Resolved token is either closed by a blank
|
||||
* line or not. It has no effect when `closed` is `true`.
|
||||
*
|
||||
* @param to Only needed when `closed` is `false`. Used to specify
|
||||
* the end offset of the resolved token.
|
||||
*/
|
||||
public resolve(types: Format[], closed: boolean, closedByBlankLine: boolean, to = this._state.globalOffset): void {
|
||||
for (let type of types) {
|
||||
let token = this.getToken(type);
|
||||
// If token with this type doesn't exist, then continue to the next one.
|
||||
if (!token) continue;
|
||||
|
||||
// When it is an inline token.
|
||||
if (token.level == TokenLevel.INLINE) {
|
||||
// There is a type -that is highlight- that doesn't need to be closed.
|
||||
if (!closed && InlineRules[type as InlineFormat].mustBeClosed) {
|
||||
token.status = TokenStatus.INACTIVE;
|
||||
} else {
|
||||
token.status = TokenStatus.ACTIVE;
|
||||
}
|
||||
// When it is a block token.
|
||||
} else {
|
||||
// Block token doesn't need to be closed.
|
||||
// Only the validity of its tag affects its status.
|
||||
if (token.validTag) {
|
||||
token.status = TokenStatus.ACTIVE;
|
||||
} else {
|
||||
token.status = TokenStatus.INACTIVE;
|
||||
}
|
||||
}
|
||||
|
||||
// Assign "to" value into token.to when "closed" is false.
|
||||
if (!closed) {
|
||||
token.to = to;
|
||||
// Determine that the resolved token is either located after the blank line or not.
|
||||
token.closedByBlankLine = closedByBlankLine;
|
||||
}
|
||||
|
||||
// Eject the token from the queue.
|
||||
delete this._tokenMap[type];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve all existing token in the queue. Often used when facing context boundary,
|
||||
* blank line, or table separator. Should be executed without any closing delimiter
|
||||
* has been met.
|
||||
*/
|
||||
public resolveAll(closedByBlankLine: boolean, to = this._state.globalOffset): void {
|
||||
this.resolve(Formats.ALL, false, closedByBlankLine, to);
|
||||
}
|
||||
|
||||
/** Clear all queued tokens. */
|
||||
public clear(): void {
|
||||
this._tokenMap = {};
|
||||
}
|
||||
}
|
||||
|
||||
export class EditorParserState {
|
||||
public readonly doc: Text;
|
||||
public readonly tree: Tree;
|
||||
public readonly settings: PluginSettings;
|
||||
public readonly textCursor: TextCursor;
|
||||
public endOfStream: number;
|
||||
|
||||
public cursor: TreeCursor | null;
|
||||
public line: Line;
|
||||
public offset: number;
|
||||
/** block start */
|
||||
public isBlockStart: boolean;
|
||||
|
||||
public inlineTokens: TokenGroup;
|
||||
public blockTokens: TokenGroup;
|
||||
public queue: _TokenQueue;
|
||||
|
||||
public curCtx: LineCtx = LineCtx.NONE;
|
||||
public prevCtx: LineCtx = LineCtx.NONE;
|
||||
|
||||
constructor(config: ParserStateConfig, inlineTokens: TokenGroup, blockTokens: TokenGroup) {
|
||||
this.doc = config.doc;
|
||||
this.tree = config.tree;
|
||||
this.textCursor = TextCursor.atOffset(this.doc, config.startAt);
|
||||
this.line = this.textCursor.curLine;
|
||||
this.offset = config.startAt - this.line.from;
|
||||
this.inlineTokens = inlineTokens;
|
||||
this.blockTokens = blockTokens;
|
||||
this.cursor = this.tree.cursor();
|
||||
this.settings = config.settings;
|
||||
this.nextCursor();
|
||||
|
||||
this.endOfStream = config.stopAt instanceof Line
|
||||
? config.stopAt.number
|
||||
: this.doc.lineAt(config.stopAt ?? this.tree.length).number;
|
||||
|
||||
// if previous line is a blank line or the
|
||||
// current line is the first line, then the current one
|
||||
// should be a block start
|
||||
let prevLine = this.prevLine;
|
||||
this.isBlockStart = prevLine
|
||||
? isBlankLine(prevLine)
|
||||
: true;
|
||||
}
|
||||
|
||||
/** global offset */
|
||||
public get globalOffset(): number {
|
||||
return this.offset + this.line.from;
|
||||
}
|
||||
|
||||
public get linePos(): number {
|
||||
return this.line.number;
|
||||
}
|
||||
|
||||
public get lineStr(): string {
|
||||
return this.line.text;
|
||||
}
|
||||
|
||||
public get char(): string {
|
||||
let char = this.line.text[this.offset];
|
||||
if (!char && !this.isLastLine()) {
|
||||
return "\n";
|
||||
}
|
||||
return char ?? "";
|
||||
}
|
||||
|
||||
public get prevLine(): Line | null {
|
||||
return this.textCursor.getPrevLine();
|
||||
}
|
||||
|
||||
public advance(n = 1): boolean {
|
||||
let restLen = this.lineStr.length - this.offset;
|
||||
if (!restLen) {
|
||||
this.queue.resolve(Formats.SPACE_RESTRICTED_INLINE, false, false);
|
||||
/* this.inlineQueue.resolve(SpaceRestrictedFormats); */
|
||||
return false;
|
||||
}
|
||||
|
||||
if (n > restLen)
|
||||
this.offset += restLen;
|
||||
else
|
||||
this.offset += n;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public setGlobalOffset(globalOffset: number): void {
|
||||
if (globalOffset > this.doc.length) {
|
||||
this.line = this.textCursor.gotoLast().curLine;
|
||||
this.offset = this.line.length;
|
||||
} else {
|
||||
this.line = this.doc.lineAt(globalOffset);
|
||||
this.offset = globalOffset - this.line.from;
|
||||
}
|
||||
}
|
||||
|
||||
public isSpace(side: -1 | 0 | 1 = 0): boolean {
|
||||
let char = this.lineStr[this.offset + side];
|
||||
return char == " " || char == "\t" || !char;
|
||||
}
|
||||
|
||||
public seekWhitespace(maxOffset = this.line.length): number | null {
|
||||
let offset = this.offset;
|
||||
for (let char = this.line.text[offset]; offset < maxOffset; char = this.line.text[++offset])
|
||||
if (char == " " || char == "\t")
|
||||
return offset;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public advanceLine(skipBlankLine = true): null | Line {
|
||||
if (this.linePos >= this.endOfStream) {
|
||||
this.queue.resolveAll(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
this.textCursor.next()
|
||||
this.line = this.textCursor.curLine;
|
||||
this.offset = 0;
|
||||
this.isBlockStart = false;
|
||||
this.resolveContext();
|
||||
if (skipBlankLine) this.trySkipBlankLine();
|
||||
return this.line;
|
||||
}
|
||||
|
||||
public trySkipBlankLine(): boolean {
|
||||
if (this.isBlankLine()) {
|
||||
// block start can be the line after the blank one.
|
||||
this.isBlockStart = true;
|
||||
// resolve all tokens that remain in the queue.
|
||||
this.queue.resolveAll(true, this.line.to);
|
||||
/* this.blockQueue.resolve(this.line.to);
|
||||
this.inlineQueue.resolveAll(this.line.to); */
|
||||
// if there is trailing blank lines, then skip them all
|
||||
while (this.linePos < this.endOfStream) {
|
||||
this.textCursor.next();
|
||||
this.line = this.textCursor.curLine;
|
||||
if (!this.isBlankLine()) break;
|
||||
}
|
||||
// Is sufficient to resolve the current context once,
|
||||
// because a sequence of blank lines should have the
|
||||
// same context.
|
||||
this.resolveContext();
|
||||
return true;
|
||||
}
|
||||
// returning false indicates that the current line isn't a blank line
|
||||
return false;
|
||||
}
|
||||
|
||||
public isLastLine(): boolean {
|
||||
return this.line.number >= this.doc.lines;
|
||||
}
|
||||
|
||||
public isBlankLine(): boolean {
|
||||
return isBlankLine(this.line);
|
||||
}
|
||||
|
||||
public nextCursor(enter = true): boolean {
|
||||
if (this.cursor) {
|
||||
if (this.cursor.next(enter) && this.cursor.name != "Document") return true;
|
||||
this.cursor = null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public cursorPos(endSide: 0 | -1 = 0): "after" | "before" | "touch" | null {
|
||||
let globalOffset = this.globalOffset;
|
||||
if (!this.cursor) return null;
|
||||
if (globalOffset < this.cursor.from) return "after";
|
||||
if (globalOffset > this.cursor.to + endSide) return "before";
|
||||
return "touch";
|
||||
}
|
||||
|
||||
public processCursor(): "hl_delim" | "table_sep" | "skipped" | null {
|
||||
let cursorPos = this.cursorPos(-1);
|
||||
while (cursorPos == "before") {
|
||||
this.nextCursor();
|
||||
cursorPos = this.cursorPos(-1);
|
||||
}
|
||||
if (cursorPos != "touch") return null;
|
||||
let nodeName = this.cursor!.name;
|
||||
if (nodeName.includes("formatting-highlight")) return "hl_delim";
|
||||
if (nodeName.includes("table-sep")) return "table_sep";
|
||||
if (SKIPPED_NODE_RE.test(nodeName)) return "skipped";
|
||||
return null;
|
||||
}
|
||||
|
||||
public skipCursorRange(): boolean {
|
||||
if (!this.cursor) return false;
|
||||
let cursorTo = this.cursor.to - this.line.from,
|
||||
whitespaceOffset = this.seekWhitespace(cursorTo);
|
||||
if (whitespaceOffset !== null) {
|
||||
this.offset = whitespaceOffset;
|
||||
this.queue.resolve(Formats.SPACE_RESTRICTED_INLINE, false, false);
|
||||
/* this.inlineQueue.resolve(SpaceRestrictedFormats); */
|
||||
}
|
||||
this.offset = cursorTo;
|
||||
return true;
|
||||
}
|
||||
|
||||
public getContext(line = this.line): LineCtx {
|
||||
if (line.number != this.linePos) {
|
||||
let node = findNode(
|
||||
this.tree, line.from, line.from,
|
||||
(node) => node.parent?.name == "Document"
|
||||
);
|
||||
if (node) return getContextFromNode(node);
|
||||
}
|
||||
|
||||
else if (this.cursorPos() == "touch") {
|
||||
let node = this.cursor!.node;
|
||||
return getContextFromNode(node);
|
||||
}
|
||||
|
||||
return LineCtx.NONE;
|
||||
}
|
||||
|
||||
public setContext(ctx: LineCtx): void {
|
||||
this.prevCtx = this.curCtx;
|
||||
this.curCtx = ctx;
|
||||
}
|
||||
|
||||
public resolveContext(): void {
|
||||
while (this.cursorPos() == "before") this.nextCursor();
|
||||
this.setContext(this.getContext());
|
||||
let isSkip = false,
|
||||
toBeResolved = false,
|
||||
includesHl = false,
|
||||
offset = this.line.from;
|
||||
|
||||
switch (this.curCtx) {
|
||||
case LineCtx.HR_LINE:
|
||||
case LineCtx.CODEBLOCK:
|
||||
case LineCtx.TABLE_DELIM:
|
||||
isSkip = true;
|
||||
toBeResolved = true;
|
||||
break;
|
||||
case LineCtx.BLOCKQUOTE:
|
||||
if (this.prevCtx != LineCtx.BLOCKQUOTE) {
|
||||
toBeResolved = true;
|
||||
}
|
||||
break;
|
||||
case LineCtx.LIST_HEAD:
|
||||
includesHl = true;
|
||||
// eslint-disable-next-line no-fallthrough
|
||||
case LineCtx.HEADING:
|
||||
case LineCtx.FOOTNOTE_HEAD:
|
||||
toBeResolved = true;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (this.prevCtx) {
|
||||
case LineCtx.HEADING:
|
||||
case LineCtx.TABLE:
|
||||
toBeResolved = true;
|
||||
offset -= 1;
|
||||
}
|
||||
|
||||
if (!this.offset) {
|
||||
if (toBeResolved) {
|
||||
this.queue.resolve(Formats.ALL_BLOCK, false, false, offset);
|
||||
this.queue.resolve(Formats.NON_BUILTIN_INLINE, false, false, offset);
|
||||
this.isBlockStart = true;
|
||||
}
|
||||
if (includesHl) this.queue.resolve([Format.HIGHLIGHT], false, false, offset);
|
||||
}
|
||||
|
||||
if (isSkip) this.skipCursorRange();
|
||||
if (this.curCtx) this.nextCursor();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The core of editor-mode parser. It only parses the document that in
|
||||
* the `tree` range, then stores the result.
|
||||
*/
|
||||
export class EditorParser {
|
||||
private _state: EditorParserState;
|
||||
private _queue: _TokenQueue = new _TokenQueue();
|
||||
private _changeSet: ChangeSet | null = null;
|
||||
|
||||
public inlineTokens: TokenGroup = [];
|
||||
public blockTokens: TokenGroup = [];
|
||||
|
||||
public reparsedRanges: Record<TokenLevel, { from: number, initTo: number, changedTo: number }>;
|
||||
public lastStreamPoint: PlainRange = { from: 0, to: 0 };
|
||||
public oldTree: Tree;
|
||||
public lastStop: number;
|
||||
|
||||
readonly settings: PluginSettings;
|
||||
|
||||
constructor(settings: PluginSettings) {
|
||||
this.settings = settings;
|
||||
if (!settings.editorEscape)
|
||||
disableEscape();
|
||||
}
|
||||
|
||||
/** Get the parsed tokens. */
|
||||
public getTokens(level: TokenLevel): TokenGroup {
|
||||
return level == TokenLevel.INLINE
|
||||
? this.inlineTokens
|
||||
: this.blockTokens;
|
||||
}
|
||||
|
||||
public hasStoredChanges(): boolean {
|
||||
return !!this._changeSet;
|
||||
}
|
||||
|
||||
public storeChanges(changeSet: ChangeSet) {
|
||||
this._changeSet = this._changeSet
|
||||
? this._changeSet.compose(changeSet)
|
||||
: changeSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize parsing, so the parser would parse from the start of the
|
||||
* document. Should be use when actual initialization, or if there is a
|
||||
* setting change that need the parser to parse from the start.
|
||||
*/
|
||||
public initParse(doc: Text, tree: Tree, stopAt?: number): void {
|
||||
// Toggle escape.
|
||||
if (this.settings.editorEscape)
|
||||
reenableEscape();
|
||||
else
|
||||
disableEscape();
|
||||
|
||||
// Flush all the results.
|
||||
this.lastStreamPoint.from = 0;
|
||||
this.inlineTokens = [];
|
||||
this.blockTokens = [];
|
||||
|
||||
// Start parsing.
|
||||
this._defineState({ doc, tree, startAt: 0, stopAt, settings: this.settings });
|
||||
this._streamParse();
|
||||
this.reparsedRanges = {
|
||||
[TokenLevel.INLINE]: { from: 0, initTo: 0, changedTo: this.inlineTokens.length },
|
||||
[TokenLevel.BLOCK]: { from: 0, initTo: 0, changedTo: this.blockTokens.length }
|
||||
}
|
||||
|
||||
this.oldTree = tree;
|
||||
this.lastStop = stopAt ?? tree.length;
|
||||
|
||||
this._changeSet = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the change comes from the document or the length difference
|
||||
* between previous parsed tree and the current one.
|
||||
*/
|
||||
public applyChange(doc: Text, tree: Tree, stopAt = tree.length): void {
|
||||
|
||||
// Start stream offset can be the shortest length of both old and new
|
||||
// tree, or the start offset of the changed range.
|
||||
let changedRange = this._changeSet ? _composeChanges(this._changeSet) : null,
|
||||
startStreamOffset = this.lastStop == stopAt
|
||||
? stopAt
|
||||
: Math.min(stopAt, this.lastStop) + 1;
|
||||
|
||||
this._changeSet = null;
|
||||
|
||||
if (changedRange)
|
||||
startStreamOffset = Math.min(startStreamOffset, changedRange.from);
|
||||
|
||||
// Nearest next blank line is the end stream, unless there is no blank
|
||||
// line exist within the new tree range, or the changed range encounters
|
||||
// such interferer node.
|
||||
let endStreamLine = changedRange && _checkInterferer(changedRange, tree, this.oldTree)
|
||||
? getBlockEndAt(doc, changedRange.changedTo)
|
||||
: getBlockEndAt(doc, stopAt);
|
||||
|
||||
let config: ParserStateConfig = {
|
||||
doc, tree,
|
||||
startAt: startStreamOffset,
|
||||
stopAt: endStreamLine,
|
||||
settings: this.settings
|
||||
};
|
||||
|
||||
this._defineState(config);
|
||||
|
||||
// Tokens that are located after the end of stream will be considered as
|
||||
// left tokens, and will be shifted by the length of the changed range.
|
||||
let leftTokens: Record<"inline" | "block", TokenGroup> = {
|
||||
inline: this._getLeftTokens(
|
||||
this._filterTokens(this.inlineTokens, this.reparsedRanges[TokenLevel.INLINE]),
|
||||
changedRange,
|
||||
endStreamLine
|
||||
),
|
||||
block: this._getLeftTokens(
|
||||
this._filterTokens(this.blockTokens, this.reparsedRanges[TokenLevel.BLOCK]),
|
||||
changedRange,
|
||||
endStreamLine
|
||||
)
|
||||
};
|
||||
|
||||
// If there are any terminal characters -are that being used as
|
||||
// delimiters- located exactly before the start offset of the stream,
|
||||
// then we need to shift it backward.
|
||||
this._shiftOffset();
|
||||
|
||||
// Shift the left tokens.
|
||||
this._remapLeftTokens(leftTokens.inline, changedRange);
|
||||
this._remapLeftTokens(leftTokens.block, changedRange);
|
||||
|
||||
// Start parsing.
|
||||
this._streamParse();
|
||||
|
||||
// Post-parsing.
|
||||
this.reparsedRanges[TokenLevel.INLINE].changedTo = this.inlineTokens.length;
|
||||
this.reparsedRanges[TokenLevel.BLOCK].changedTo = this.blockTokens.length;
|
||||
this.inlineTokens = this.inlineTokens.concat(leftTokens.inline);
|
||||
this.blockTokens = this.blockTokens.concat(leftTokens.block);
|
||||
this.oldTree = tree;
|
||||
this.lastStop = stopAt ?? tree.length;
|
||||
}
|
||||
|
||||
/** Define new state as parsing begins. */
|
||||
private _defineState(config: ParserStateConfig): void {
|
||||
this._state = new EditorParserState(config, this.inlineTokens, this.blockTokens);
|
||||
this._queue.attachState(this._state);
|
||||
if (this.oldTree) this._shiftOffsetByNode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shift the start offset backward to the shifter node if encounters it.
|
||||
* It uses the old tree and the new one, to indetify if there is a
|
||||
* shifter node touching the start offset. Shift can be done if at least
|
||||
* we found it in one of the two trees.
|
||||
*
|
||||
* Must be done along with defining new state.
|
||||
*/
|
||||
private _shiftOffsetByNode() {
|
||||
let oldOffset = this._state.globalOffset,
|
||||
newNode = findShifterAt(this._state.tree, oldOffset),
|
||||
oldNode = findShifterAt(this.oldTree, oldOffset),
|
||||
newOffset: number | null = null;
|
||||
|
||||
// Try to find it in the new tree.
|
||||
if (newNode)
|
||||
newOffset = getShifterStart(newNode);
|
||||
|
||||
// Try to find it in the old tree, even if shifter be found in the new
|
||||
// tree. Because, it could be different type.
|
||||
if (oldNode) {
|
||||
let oldStart = getShifterStart(oldNode);
|
||||
if (oldStart !== null && (newOffset === null || oldStart < newOffset)) {
|
||||
newOffset = oldStart;
|
||||
}
|
||||
}
|
||||
|
||||
if (newOffset !== null) {
|
||||
this._state.setGlobalOffset(newOffset);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shift the start offset backward if there is terminal character
|
||||
* located exactly before the offset.
|
||||
*/
|
||||
private _shiftOffset(): boolean {
|
||||
if (this._state.offset == 0) return false;
|
||||
|
||||
let prevOffset = this._state.offset - 1,
|
||||
str = this._state.lineStr,
|
||||
char = str[prevOffset];
|
||||
|
||||
if (_isTerminalChar(char, this.settings)) {
|
||||
while (str[prevOffset - 1] == char) { prevOffset-- }
|
||||
this._state.offset = prevOffset;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Detach the state from the parser. Used after finishing the parse. */
|
||||
private _detachState(): void {
|
||||
this._queue.detachState();
|
||||
(this._state as EditorParserState | undefined) = undefined;
|
||||
}
|
||||
|
||||
/** Parse the given document. */
|
||||
private _streamParse(): void {
|
||||
let prevLine: Line | null,
|
||||
state = this._state;
|
||||
this.lastStreamPoint.from = state.globalOffset;
|
||||
|
||||
// Get previous line context.
|
||||
if (prevLine = this._state.prevLine) {
|
||||
state.setContext(state.getContext(prevLine));
|
||||
}
|
||||
// Try to skip current line if it is a blank line. If fails, resolve the
|
||||
// current line context.
|
||||
if (!state.trySkipBlankLine())
|
||||
state.resolveContext();
|
||||
|
||||
// Parse each line untill reach the end of the stream.
|
||||
do { this._parseLine() } while (state.advanceLine())
|
||||
this.lastStreamPoint.to = state.globalOffset;
|
||||
|
||||
// Flush queue and state.
|
||||
this._queue.clear();
|
||||
this._detachState();
|
||||
}
|
||||
|
||||
/** Parse a single line. */
|
||||
private _parseLine(): void {
|
||||
let state = this._state;
|
||||
|
||||
// Try to parse block level token.
|
||||
if (
|
||||
this.settings.fencedDiv & MarkdownViewMode.EDITOR_MODE &&
|
||||
this._state.offset == 0 && state.isBlockStart
|
||||
) Tokenizer.block(state, Format.FENCED_DIV);
|
||||
|
||||
while (true) {
|
||||
// Resolve space-restricted tokens if the state encountered a whitespace.
|
||||
if (state.isSpace())
|
||||
state.queue.resolve(Formats.SPACE_RESTRICTED_INLINE, false, false);
|
||||
|
||||
let nodeType = state.processCursor(),
|
||||
type = EditorDelimLookup[state.char];
|
||||
|
||||
// Running parse on each character.
|
||||
if (nodeType == "skipped") {
|
||||
state.skipCursorRange();
|
||||
} else if (nodeType == "table_sep") {
|
||||
state.queue.resolve(Formats.ALL_INLINE, false, false);
|
||||
state.advance();
|
||||
} else if (type && (type != Format.HIGHLIGHT || nodeType == "hl_delim")) {
|
||||
// Highlight already be parsed by builtin parser.
|
||||
Tokenizer.inline(state, type);
|
||||
} else if (!state.advance()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _filterTokens(tokens: TokenGroup, reparsedRange: typeof this.reparsedRanges[TokenLevel]): TokenGroup {
|
||||
let reparsedFrom: number | undefined,
|
||||
reparsedTo: number | undefined,
|
||||
offset = this._state.globalOffset,
|
||||
index = findTokenIndexAt(tokens, offset) ?? tokens.length;
|
||||
|
||||
for (let curToken = tokens[index]; index < tokens.length; curToken = tokens[++index]) {
|
||||
// Keep find token touched by the current offset
|
||||
if (curToken.to < offset) continue;
|
||||
|
||||
let { openRange, tagRange } = provideTokenPartsRanges(curToken);
|
||||
if (openRange.to < offset) {
|
||||
curToken.to = curToken.from;
|
||||
curToken.closeLen = 0;
|
||||
reparsedFrom ??= index;
|
||||
this._queue.push(curToken);
|
||||
|
||||
if (curToken.tagLen && tagRange.to >= offset) {
|
||||
curToken.tagLen = offset - tagRange.from;
|
||||
if (curToken.type == Format.CUSTOM_SPAN || curToken.type == Format.HIGHLIGHT)
|
||||
handleInlineTag(this._state, curToken);
|
||||
else if (curToken.type == Format.FENCED_DIV)
|
||||
handleFencedDivTag(this._state, curToken);
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
reparsedTo = index + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
reparsedFrom ??= index;
|
||||
reparsedRange.from = reparsedFrom;
|
||||
reparsedRange.initTo = reparsedTo ?? index;
|
||||
return tokens.splice(index);
|
||||
}
|
||||
|
||||
private _getLeftTokens(filteredOut: TokenGroup, changedRange: ChangedRange | null, blockEndLine: Line): TokenGroup {
|
||||
let curIndex = 0,
|
||||
changedLen = changedRange?.length;
|
||||
|
||||
if (changedLen === undefined) return [];
|
||||
|
||||
while (
|
||||
curIndex < filteredOut.length &&
|
||||
filteredOut[curIndex].to <= blockEndLine.to - changedLen
|
||||
) curIndex++;
|
||||
return filteredOut.slice(curIndex);
|
||||
}
|
||||
|
||||
private _remapLeftTokens(reusedTokens: TokenGroup, changedRange: ChangedRange | null): void {
|
||||
if (!reusedTokens || !changedRange) return;
|
||||
|
||||
let offsetDiffer = changedRange.changedTo - changedRange.initTo;
|
||||
for (
|
||||
let i = 0, token = reusedTokens[i];
|
||||
i < reusedTokens.length;
|
||||
token = reusedTokens[++i]
|
||||
) {
|
||||
token.from += offsetDiffer;
|
||||
token.to += offsetDiffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
import { SyntaxNode, Tree } from "@lezer/common";
|
||||
import { SEMANTIC_INTERFERER_RE, SHIFTER_RE, ShifterNodeConfigs, SKIPPED_NODE_RE } from "src/editor-mode/preprocessor/parser-configs";
|
||||
import { LineCtx } from "src/enums";
|
||||
|
||||
interface NodeSpec {
|
||||
node: SyntaxNode,
|
||||
type: string
|
||||
}
|
||||
|
||||
export function disableEscape(): void {
|
||||
SKIPPED_NODE_RE.compile("table|code|formatting|html|math|tag|url|barelink|atom|comment|string|meta|frontmatter|hr(?!\\w)");
|
||||
}
|
||||
|
||||
export function reenableEscape(): void {
|
||||
SKIPPED_NODE_RE.compile("table|code|formatting|escape|html|math|tag|url|barelink|atom|comment|string|meta|frontmatter|hr(?!\\w)");
|
||||
}
|
||||
|
||||
export function findNode(tree: Tree, from: number, to: number, matcher: (node: SyntaxNode) => boolean): SyntaxNode | null {
|
||||
let node: SyntaxNode | null = null;
|
||||
tree.iterate({
|
||||
from, to, enter(nodeRef) {
|
||||
if (matcher(nodeRef.node)) {
|
||||
node = nodeRef.node;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
return node;
|
||||
}
|
||||
|
||||
export function findShifterAt(tree: Tree, offset: number): NodeSpec | null {
|
||||
let node: SyntaxNode | null = null,
|
||||
type: string | null = null;
|
||||
tree.iterate({
|
||||
from: offset, to: offset,
|
||||
enter(nodeRef) {
|
||||
if (nodeRef.name == "Document") { return }
|
||||
let match = SHIFTER_RE.exec(nodeRef.name);
|
||||
if (match) {
|
||||
node = nodeRef.node;
|
||||
type = match[0];
|
||||
return false;
|
||||
}
|
||||
},
|
||||
});
|
||||
if (node && type) return { node, type };
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getContextFromNode(node: SyntaxNode): LineCtx {
|
||||
let nodeName = node.name,
|
||||
context = LineCtx.NONE;
|
||||
if (node.name == "Document") { return context }
|
||||
if (!node.name.startsWith("HyperMD")) {
|
||||
if (node.parent!.name == "Document") { return context }
|
||||
node = node.parent!;
|
||||
}
|
||||
// 8 is the length of "HyperMD-" string
|
||||
if (nodeName.startsWith("hr", 8)) {
|
||||
context = LineCtx.HR_LINE;
|
||||
} else if (nodeName.startsWith("header", 8)) {
|
||||
context = LineCtx.HEADING;
|
||||
} else if (nodeName.startsWith("quote", 8)) {
|
||||
context = LineCtx.BLOCKQUOTE;
|
||||
} else if (nodeName.startsWith("codeblock", 8)) {
|
||||
context = LineCtx.CODEBLOCK;
|
||||
} else if (nodeName.startsWith("list", 8)) {
|
||||
if (nodeName.includes("nobullet")) {
|
||||
context = LineCtx.LIST;
|
||||
} else {
|
||||
context = LineCtx.LIST_HEAD;
|
||||
}
|
||||
} else if (nodeName.startsWith("footnote", 8)) {
|
||||
if (node.firstChild?.name.includes("footnote")) {
|
||||
context = LineCtx.FOOTNOTE_HEAD;
|
||||
} else {
|
||||
context = LineCtx.FOOTNOTE;
|
||||
}
|
||||
} else if (nodeName.startsWith("table", 8)) {
|
||||
if (nodeName.includes("table-row-1")) {
|
||||
context = LineCtx.TABLE_DELIM;
|
||||
} else {
|
||||
context = LineCtx.TABLE;
|
||||
}
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
export function getShifterStart(spec: NodeSpec): number | null {
|
||||
let node: SyntaxNode | null = spec.node,
|
||||
type = spec.type;
|
||||
if (node = ShifterNodeConfigs[type]?.getOpen(node)) {
|
||||
return node.from;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function hasInterferer(tree: Tree, from: number, to: number): boolean {
|
||||
let matcher = (node: SyntaxNode) => {
|
||||
let match = SEMANTIC_INTERFERER_RE.exec(node.name);
|
||||
if (match) {
|
||||
if (
|
||||
(match[0] == "math-begin" || match[0] == "math-end") &&
|
||||
node.to - node.from < 2
|
||||
) { return false }
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
return !!findNode(tree, from, to, matcher);
|
||||
}
|
||||
|
|
@ -1,209 +0,0 @@
|
|||
import { Format, TokenLevel, Delimiter, TokenStatus } from "src/enums";
|
||||
import { BlockFormat, InlineFormat, Token } from "src/types";
|
||||
import { BlockRules, InlineRules } from "src/format-configs/rules";
|
||||
import { EditorParserState } from "src/editor-mode/preprocessor/parser";
|
||||
import { isInlineFormat } from "src/format-configs/format-utils";
|
||||
|
||||
/** Describe the rule that has to be satisfied by the delimiter */
|
||||
interface DelimSpec {
|
||||
/** Should be single character */
|
||||
char: string,
|
||||
/** Delimiter length */
|
||||
length: number,
|
||||
/**
|
||||
* If true, then delimiter length must be the same as
|
||||
* in the predetermined rule. Otherwise, the defined length
|
||||
* act as minimum length.
|
||||
*/
|
||||
exactLen: boolean,
|
||||
/** Should be `Delimiter.OPEN` or `Delimiter.CLOSE` */
|
||||
role: Delimiter,
|
||||
/**
|
||||
* When true, any space after the opening delimiter or before the closing
|
||||
* one doesn't make the delimiter invalid. Default is false.
|
||||
*/
|
||||
allowSpaceOnDelim?: boolean
|
||||
}
|
||||
|
||||
function _isAlphanumeric(char: string): boolean {
|
||||
let charCode = char.charCodeAt(0);
|
||||
return (
|
||||
charCode >= 0x30 && charCode <= 0x39 ||
|
||||
charCode >= 0x41 && charCode <= 0x5a ||
|
||||
charCode >= 0x61 && charCode <= 0x7a
|
||||
);
|
||||
}
|
||||
|
||||
function _retrieveDelimSpec(type: Format, role: Delimiter): DelimSpec {
|
||||
let char: string, length: number, exactLen: boolean, allowSpaceOnDelim: boolean;
|
||||
if (isInlineFormat(type)) {
|
||||
({ char, length, exactLen } = InlineRules[type]);
|
||||
allowSpaceOnDelim = false;
|
||||
} else {
|
||||
({ char, length, exactLen } = BlockRules[type]);
|
||||
allowSpaceOnDelim = true;
|
||||
}
|
||||
return { char, length, exactLen, role, allowSpaceOnDelim }
|
||||
}
|
||||
|
||||
|
||||
function _validateDelim(str: string, offset: number, spec: DelimSpec): { valid: boolean, length: number } {
|
||||
let length = 0, valid = false;
|
||||
while (str[offset + length] == spec.char) length++;
|
||||
if (spec.exactLen && length == spec.length || !spec.exactLen && length >= spec.length) {
|
||||
let char: string;
|
||||
if (spec.role == Delimiter.OPEN) {
|
||||
char = str[offset + length];
|
||||
} else if (spec.role == Delimiter.CLOSE) {
|
||||
char = str[offset - 1];
|
||||
} else {
|
||||
throw TypeError("");
|
||||
}
|
||||
if (spec.allowSpaceOnDelim || char && char != " " && char != "\t") { valid = true }
|
||||
}
|
||||
return { valid, length };
|
||||
}
|
||||
|
||||
function _handleClosingDelim(state: EditorParserState, token: Token, closeLen: number): void {
|
||||
token.closeLen = closeLen;
|
||||
token.to = state.globalOffset + closeLen;
|
||||
state.advance(closeLen);
|
||||
state.queue.resolve([token.type], true, false);
|
||||
}
|
||||
|
||||
export function handleInlineTag(state: EditorParserState, token: Token): void {
|
||||
let offset = state.offset,
|
||||
initTagLen = token.tagLen,
|
||||
str = state.lineStr,
|
||||
noSpace = token.type == Format.HIGHLIGHT;
|
||||
if (token.validTag) {
|
||||
if (str[offset - 1] == "}") return;
|
||||
token.validTag = false;
|
||||
}
|
||||
if (token.tagLen == 0) {
|
||||
if (str[offset] != "{") return;
|
||||
token.tagLen++;
|
||||
offset++;
|
||||
}
|
||||
for (let char = str[offset]; offset < str.length; char = str[++offset]) {
|
||||
if (!_isAlphanumeric(char) && char != "-" && (char != " " || noSpace)) break;
|
||||
token.tagLen++;
|
||||
}
|
||||
if (token.tagLen > 1 && str[offset] == "}") {
|
||||
token.validTag = true;
|
||||
token.tagLen++;
|
||||
}
|
||||
state.advance(token.tagLen - initTagLen);
|
||||
}
|
||||
|
||||
export function handleFencedDivTag(state: EditorParserState, token: Token): void {
|
||||
token.validTag = false;
|
||||
let offset = token.openLen + token.tagLen,
|
||||
initTagLen = token.tagLen,
|
||||
str = state.lineStr;
|
||||
while (offset < str.length) {
|
||||
let char = str[offset];
|
||||
if (char == " " || char == "-" || _isAlphanumeric(char)) { token.tagLen++; offset++ }
|
||||
else break;
|
||||
}
|
||||
state.advance(token.tagLen - initTagLen);
|
||||
if (offset >= str.length) token.validTag = true;
|
||||
else state.queue.resolve([token.type], false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokens will only be created through this. Each method
|
||||
* returns whether `true` or `false`, indicating the success
|
||||
* of the tokenization. Parsed token will be automatically
|
||||
* inserted to the token group.
|
||||
*/
|
||||
export const Tokenizer = {
|
||||
/**
|
||||
* Used for parsing block token. Should only be executed when the
|
||||
* current line was a block start.
|
||||
*/
|
||||
block(state: EditorParserState, type: BlockFormat): boolean {
|
||||
// Block token is only parsed when the current line is a block start.
|
||||
if (!state.isBlockStart) return false;
|
||||
// Retrieve DelimSpec based on input type.
|
||||
let spec = _retrieveDelimSpec(type, Delimiter.OPEN),
|
||||
// Verifiy that the delimiter was valid and gets its length.
|
||||
{ valid, length: openLen } = _validateDelim(state.lineStr, state.offset, spec);
|
||||
// Advance along the given delimiter length.
|
||||
state.advance(openLen);
|
||||
// If it isn't valid, then abort it.
|
||||
if (!valid) return false;
|
||||
let token: Token = {
|
||||
type,
|
||||
level: TokenLevel.BLOCK,
|
||||
status: TokenStatus.PENDING,
|
||||
from: state.globalOffset - openLen,
|
||||
to: state.globalOffset - openLen,
|
||||
openLen,
|
||||
closeLen: 0,
|
||||
tagLen: 0,
|
||||
// Block tag doesn't overlapped over the content.
|
||||
tagAsContent: false,
|
||||
validTag: false,
|
||||
closedByBlankLine: false
|
||||
};
|
||||
// Queue and push the token to the token group.
|
||||
state.blockTokens.push(token);
|
||||
state.queue.push(token);
|
||||
// Currently, block token only has fenced div type. Therefore,
|
||||
// the tokenizer parses its tag directly without checking its
|
||||
// type.
|
||||
handleFencedDivTag(state, token);
|
||||
// Indicate that tokenizing run successfully.
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Used for parsing inline token. Should be executed twice only when the
|
||||
* parser state encountered allegedly closing delimiter of the queued token.
|
||||
*/
|
||||
inline(state: EditorParserState, type: InlineFormat): boolean {
|
||||
// Get the token according to the input type, may be null.
|
||||
let token = state.queue.getToken(type),
|
||||
// Which delimiter is encountered by the state.
|
||||
// Determined by the presence of queued token.
|
||||
role = state.queue.isQueued(type) ? Delimiter.CLOSE : Delimiter.OPEN,
|
||||
// Get delimiter specification according to its type.
|
||||
spec = _retrieveDelimSpec(type, role),
|
||||
// Check whether it's highlight, custom span, or neither.
|
||||
isHighlight = type == Format.HIGHLIGHT,
|
||||
isCustomSpan = type == Format.CUSTOM_SPAN,
|
||||
// Verifiy that the delimiter was valid and gets its length.
|
||||
{ valid, length } = _validateDelim(state.lineStr, state.offset, spec);
|
||||
// If it isn't valid, then abort it.
|
||||
if (!valid) {
|
||||
state.advance(length);
|
||||
return false;
|
||||
}
|
||||
// If there is a queued token with this type, then finalize it.
|
||||
if (token)
|
||||
_handleClosingDelim(state, token, length);
|
||||
// Else, create new token and push it into the queue.
|
||||
else {
|
||||
let token: Token = {
|
||||
type,
|
||||
level: TokenLevel.INLINE,
|
||||
status: TokenStatus.PENDING,
|
||||
from: state.globalOffset,
|
||||
to: state.globalOffset,
|
||||
openLen: length,
|
||||
closeLen: 0,
|
||||
tagLen: 0,
|
||||
tagAsContent: isHighlight || isCustomSpan,
|
||||
validTag: false,
|
||||
closedByBlankLine: false
|
||||
};
|
||||
state.inlineTokens.push(token);
|
||||
state.queue.push(token);
|
||||
state.advance(length);
|
||||
// If this token can have a tag, then try to parse it.
|
||||
if (isHighlight || isCustomSpan) handleInlineTag(state, token);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
1
src/editor-mode/state-fields/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./parserField";
|
||||
24
src/editor-mode/state-fields/parserField.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { StateEffect, StateField } from "@codemirror/state";
|
||||
import { Parser } from "src/editor-mode/parser";
|
||||
import { ParseContext, syntaxTree } from "@codemirror/language";
|
||||
import { Tree } from "@lezer/common";
|
||||
import { settingsFacet } from "../facets";
|
||||
|
||||
export const parserField = StateField.define({
|
||||
create(state) {
|
||||
let parser = new Parser(state.facet(settingsFacet.reader));
|
||||
parser.initParse(state.doc, syntaxTree(state));
|
||||
return parser;
|
||||
},
|
||||
update(parser, transaction) {
|
||||
let oldTree = syntaxTree(transaction.startState),
|
||||
newTree = syntaxTree(transaction.state);
|
||||
transaction.effects.forEach((value: StateEffect<{context: ParseContext, tree: Tree}>) => {
|
||||
newTree = (value.value?.tree ?? newTree);
|
||||
});
|
||||
if (transaction.docChanged || oldTree.length != newTree.length) {
|
||||
parser.applyChange(transaction.newDoc, newTree, oldTree, transaction.changes);
|
||||
}
|
||||
return parser;
|
||||
}
|
||||
});
|
||||
|
|
@ -1,247 +0,0 @@
|
|||
import ExtendedMarkdownSyntax from "main";
|
||||
import { IconName, Menu, Editor, MarkdownFileInfo, MarkdownView, Platform } from "obsidian";
|
||||
import { Direction, EditorView } from "@codemirror/view";
|
||||
import { IndexCache, TagConfig, InlineFormat } from "src/types";
|
||||
import { Format, TokenLevel, TokenStatus } from "src/enums";
|
||||
import { getActiveCanvasNodeCoords } from "src/editor-mode/utils/canvas-utils";
|
||||
import { Formatter } from "src/editor-mode/formatting/formatter";
|
||||
import { supportTag } from "src/format-configs/format-utils";
|
||||
import { tagMenuOptionCaches, instancesStore } from "src/editor-mode/cm-extensions";
|
||||
import { ctxMenuCommands } from "src/editor-mode/formatting/commands";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
||||
interface ITagMenu extends TagMenu {};
|
||||
|
||||
export function extendEditorCtxMenu(menu: Menu, editor: Editor, ctx: MarkdownFileInfo | MarkdownView) {
|
||||
menu.addItem(item => {
|
||||
let subMenu = item.setSubmenu(),
|
||||
cmView = editor.activeCm || editor.cm,
|
||||
activeFormats: Partial<Record<InlineFormat, boolean>> = {},
|
||||
commands = ctxMenuCommands;
|
||||
subMenu.onload = function () {
|
||||
if (Platform.isMobile) { return }
|
||||
subMenu.registerDomEvent(subMenu.dom, "mouseover", () => {
|
||||
if (subMenu.currentSubmenu && subMenu.currentSubmenu !== subMenu.items[subMenu.selected]?.submenu) {
|
||||
subMenu.closeSubmenu();
|
||||
// @ts-ignore
|
||||
subMenu.openSubmenuSoon(subMenu.items[subMenu.selected]);
|
||||
}
|
||||
});
|
||||
};
|
||||
if (!(cmView instanceof EditorView)) {
|
||||
throw ReferenceError("EditorView not found");
|
||||
}
|
||||
let selectionObserver = cmView.state.facet(instancesStore).observer;
|
||||
selectionObserver.iterateSelectedRegion(TokenLevel.INLINE, true, (token , _, __, pos) => {
|
||||
let type = token.type as InlineFormat;
|
||||
if (token.status != TokenStatus.ACTIVE || activeFormats[type]) { return }
|
||||
if (pos == "covered" || pos == "covering") {
|
||||
activeFormats[type] = true;
|
||||
}
|
||||
});
|
||||
subMenu.addSections(["selection-extended-inline", "selection-extended-block"]);
|
||||
item.setTitle("More format...");
|
||||
item.setIcon("paintbrush-vertical");
|
||||
item.setSection("selection");
|
||||
for (let i = 0; i < commands.length; i++) {
|
||||
subMenu.addItem(item => {
|
||||
let type = commands[i].type as InlineFormat;
|
||||
item.setTitle(commands[i].ctxMenuTitle);
|
||||
item.setIcon(commands[i].icon);
|
||||
item.setSection("selection-extended-inline");
|
||||
if (activeFormats[type]) {
|
||||
item.setChecked(true);
|
||||
}
|
||||
if (type == Format.HIGHLIGHT || type == Format.CUSTOM_SPAN) {
|
||||
let tagSubMenu = item.setSubmenu();
|
||||
TagMenu.bindMenu(tagSubMenu, cmView, type);
|
||||
}
|
||||
item.onClick(() => {
|
||||
commands[i].editorCallback(editor, ctx);
|
||||
menu.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export class TagMenu extends Menu {
|
||||
mainPlugin: ExtendedMarkdownSyntax;
|
||||
itemIndexCache: IndexCache = { number: 0 };
|
||||
view: EditorView;
|
||||
type: Format;
|
||||
formatter: Formatter;
|
||||
CLS_PREFIX = "ems-menu-item";
|
||||
ICON: string;
|
||||
constructor(view: EditorView, type: Format) {
|
||||
super();
|
||||
this.constructMenu(view, type);
|
||||
}
|
||||
constructMenu(view: EditorView, type: Format) {
|
||||
let internalInstances = view.state.facet(instancesStore);
|
||||
this.view = view;
|
||||
this.type = type;
|
||||
this.formatter = internalInstances.formatter;
|
||||
this.mainPlugin = internalInstances.mainPlugin;
|
||||
this.bindIndexCache();
|
||||
this.setClassPrefix();
|
||||
this.setIcon();
|
||||
this.addConfigs(this.getConfigs());
|
||||
this.addDefaultItem();
|
||||
this.addRemoveItem();
|
||||
this.dom.addClass("ems-menu", "ems-tag-menu");
|
||||
if (type == Format.HIGHLIGHT) {
|
||||
this.dom.addClass("ems-color-menu");
|
||||
}
|
||||
}
|
||||
setClassPrefix() {
|
||||
if (this.type == Format.HIGHLIGHT) {
|
||||
this.CLS_PREFIX += " ems-highlight-";
|
||||
} else if (this.type == Format.CUSTOM_SPAN) {
|
||||
this.CLS_PREFIX += " ems-span-";
|
||||
} else if (this.type == Format.FENCED_DIV) {
|
||||
this.CLS_PREFIX += " ems-div-";
|
||||
}
|
||||
}
|
||||
setIcon() {
|
||||
if (this.type == Format.HIGHLIGHT) {
|
||||
this.ICON = "palette";
|
||||
} else {
|
||||
this.ICON = "shapes";
|
||||
}
|
||||
}
|
||||
bindIndexCache() {
|
||||
let lastOption = this.view.state.facet(tagMenuOptionCaches);
|
||||
switch (this.type) {
|
||||
case Format.HIGHLIGHT:
|
||||
this.itemIndexCache = lastOption.colorMenuItem;
|
||||
break;
|
||||
case Format.CUSTOM_SPAN:
|
||||
this.itemIndexCache = lastOption.spanTagMenuItem;
|
||||
break;
|
||||
case Format.FENCED_DIV:
|
||||
this.itemIndexCache = lastOption.divTagMenuItem;
|
||||
break;
|
||||
default:
|
||||
this.itemIndexCache = { number: 0 };
|
||||
}
|
||||
}
|
||||
getConfigs() {
|
||||
let configs: TagConfig[] = [],
|
||||
{ settings } = this.mainPlugin;
|
||||
if (this.type == Format.HIGHLIGHT) {
|
||||
configs = settings.colorConfigs;
|
||||
} else if (this.type == Format.CUSTOM_SPAN) {
|
||||
configs = settings.predefinedSpanTag;
|
||||
} else if (this.type == Format.FENCED_DIV) {
|
||||
configs = settings.predefinedDivTag;
|
||||
}
|
||||
return configs;
|
||||
}
|
||||
addConfigs(configs: TagConfig[]) {
|
||||
let { settings } = this.mainPlugin;
|
||||
configs.forEach(({ name, tag, showInMenu }) => {
|
||||
if (!showInMenu) { return }
|
||||
this.addConfigItem(name, this.ICON, this.CLS_PREFIX + tag, () => { this.format(tag) });
|
||||
});
|
||||
if (this.type == Format.HIGHLIGHT && settings.showAccentColor) {
|
||||
this.addConfigItem("Accent", this.ICON, this.CLS_PREFIX + "accent", () => { this.format("accent") });
|
||||
}
|
||||
}
|
||||
addConfigItem(name: string, icon: IconName, cls: string, callback: (evt: MouseEvent | KeyboardEvent) => unknown) {
|
||||
this.addItem(item => {
|
||||
item.setTitle(name);
|
||||
item.setIcon(icon);
|
||||
item.dom.addClass(...cls.split(" "));
|
||||
item.onClick(evt => {
|
||||
let index = this.items.findIndex(i => i == item);
|
||||
this.itemIndexCache.number = index;
|
||||
callback(evt);
|
||||
});
|
||||
});
|
||||
}
|
||||
addDefaultItem() {
|
||||
let { settings } = this.mainPlugin;
|
||||
if (
|
||||
this.type == Format.HIGHLIGHT && settings.showDefaultColor ||
|
||||
this.type == Format.CUSTOM_SPAN && settings.showDefaultSpanTag
|
||||
) {
|
||||
this.addConfigItem("Default", this.ICON, this.CLS_PREFIX + "default", () => { this.format("") });
|
||||
}
|
||||
}
|
||||
addRemoveItem() {
|
||||
let { settings } = this.mainPlugin;
|
||||
if (
|
||||
this.type == Format.HIGHLIGHT && settings.showRemoveColor ||
|
||||
this.type == Format.CUSTOM_SPAN && settings.showRemoveSpanTag
|
||||
) {
|
||||
this.addConfigItem("Remove", "eraser", "ems-menu-item ems-remove", () => { this.removeFormatting() });
|
||||
}
|
||||
}
|
||||
format(tag: string) {
|
||||
this.formatter.startFormat(this.view, this.type, tag);
|
||||
}
|
||||
removeFormatting() {
|
||||
this.formatter.startFormat(this.view, this.type, undefined, true);
|
||||
}
|
||||
showMenu() {
|
||||
this.checkItemIndexCache();
|
||||
this.view.requestMeasure({
|
||||
read: (view) => {
|
||||
let app = this.mainPlugin.app,
|
||||
canvasNodeCoords = getActiveCanvasNodeCoords(app),
|
||||
charOffset = view.state.selection.ranges.at(-1)!.to,
|
||||
charCoords = view.coordsForChar(charOffset);
|
||||
if (!charCoords) {
|
||||
let contentDOMCoords = view.contentDOM.getBoundingClientRect(),
|
||||
firstBlock = view.lineBlockAt(charOffset),
|
||||
isRTL = view.textDirection == Direction.RTL;
|
||||
charCoords = {
|
||||
top: contentDOMCoords.top,
|
||||
bottom: contentDOMCoords.top + firstBlock.height,
|
||||
left: isRTL ? contentDOMCoords.right : contentDOMCoords.left,
|
||||
right: isRTL ? contentDOMCoords.left : contentDOMCoords.right
|
||||
};
|
||||
}
|
||||
return { charCoords, canvasNodeCoords };
|
||||
},
|
||||
write: (measure) => {
|
||||
let { charCoords, canvasNodeCoords } = measure;
|
||||
if (charCoords) {
|
||||
let menuCoords = { x: charCoords.left, y: charCoords.bottom };
|
||||
if (canvasNodeCoords) {
|
||||
menuCoords.x += canvasNodeCoords.x;
|
||||
menuCoords.y += canvasNodeCoords.y;
|
||||
}
|
||||
this.showAtPosition(menuCoords);
|
||||
this.select(this.itemIndexCache.number);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
checkItemIndexCache() {
|
||||
if (this.itemIndexCache.number >= this.items.length) {
|
||||
this.itemIndexCache.number = this.items.length - 1;
|
||||
}
|
||||
}
|
||||
static bindMenu(other: Menu, ...args: ConstructorParameters<typeof TagMenu>) {
|
||||
let methodNames = ["addConfigItem", "addConfigs", "bindIndexCache", "checkItemIndexCache", "constructMenu", "format", "removeFormatting", "showMenu", "setClassPrefix", "setIcon", "addDefaultItem", "addRemoveItem", "getConfigs"] as const;
|
||||
for (let methodName of methodNames) {
|
||||
Object.defineProperty(other, methodName, {
|
||||
value: this.prototype[methodName].bind(other)
|
||||
});
|
||||
}
|
||||
Object.defineProperty(other, "CLS_PREFIX", {
|
||||
value: "ems-menu-item",
|
||||
configurable: true,
|
||||
writable: true
|
||||
});
|
||||
(other as ITagMenu).constructMenu(...args);
|
||||
return other as ITagMenu;
|
||||
}
|
||||
static create(...args: ConstructorParameters<typeof TagMenu>) {
|
||||
let [view, type] = args;
|
||||
if (!supportTag(type)) { throw new TypeError("This format type doesn't support custom tag.") }
|
||||
return new this(view, type);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
import { App, MarkdownView } from "obsidian";
|
||||
|
||||
function _isCanvas(app: App) {
|
||||
return app.workspace.getMostRecentLeaf()?.view.getViewType() == "canvas";
|
||||
}
|
||||
|
||||
function _getActiveCanvasEditor(app: App) {
|
||||
if (_isCanvas(app)) {
|
||||
return app.workspace.activeEditor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getActiveCanvasNodeCoords(app: App) {
|
||||
let canvasEditor = _getActiveCanvasEditor(app),
|
||||
containerEl = (canvasEditor as MarkdownView)?.containerEl;
|
||||
if (containerEl) {
|
||||
return containerEl.getBoundingClientRect();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -1,291 +0,0 @@
|
|||
import { Line, Text, TextLeaf, TextNode } from "@codemirror/state";
|
||||
import { PlainRange } from "src/types";
|
||||
|
||||
// TODO: utilize TextCursor to all functions
|
||||
|
||||
type LineAddress = { parent: TextNode | TextLeaf, index: number }[];
|
||||
|
||||
function _isTextLeaf(doc: Text): doc is TextLeaf {
|
||||
return "text" in doc && doc.text instanceof Array;
|
||||
}
|
||||
|
||||
function _isTextNode(doc: Text): doc is TextNode {
|
||||
return doc.children !== null;
|
||||
}
|
||||
|
||||
export class ILine extends Line {
|
||||
public readonly from: number;
|
||||
public readonly to: number;
|
||||
public readonly number: number;
|
||||
public readonly text: string;
|
||||
|
||||
constructor(from: number, to: number, number: number, text: string) {
|
||||
super();
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
this.number = number;
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
export class TextCursor {
|
||||
public address: LineAddress;
|
||||
public curLine: Line;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public get doc(): Text {
|
||||
return this.address[0].parent;
|
||||
}
|
||||
|
||||
public next(): boolean {
|
||||
for (let deep = this.address.length - 1; deep >= 0; deep--) {
|
||||
let { parent, index } = this.address[deep],
|
||||
branches = _isTextLeaf(parent) ? parent.text : parent.children;
|
||||
if (index + 1 < branches.length) {
|
||||
this.address[deep].index = ++index;
|
||||
if (this.address.splice(deep + 1).length) while (_isTextNode(parent)) {
|
||||
parent = parent.children[index] as TextNode | TextLeaf;
|
||||
index = 0;
|
||||
this.address.push({ parent, index });
|
||||
}
|
||||
if (!_isTextLeaf(parent)) throw TypeError("TextLeaf not found!");
|
||||
let length = parent.text[index].length,
|
||||
from = this.curLine.to + 1;
|
||||
this.curLine = new ILine(
|
||||
from,
|
||||
from + length,
|
||||
this.curLine.number + 1,
|
||||
parent.text[index]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public prev(): boolean {
|
||||
for (let deep = this.address.length - 1; deep >= 0; deep--) {
|
||||
if (this.address[deep].index > 0) {
|
||||
this.address[deep].index--;
|
||||
let { parent, index } = this.address[deep];
|
||||
if (this.address.splice(deep + 1).length && _isTextNode(parent)) while (true) {
|
||||
let isTextLeaf: boolean;
|
||||
parent = (parent as TextNode).children[index] as TextNode | TextLeaf;
|
||||
if (_isTextLeaf(parent)) {
|
||||
isTextLeaf = true;
|
||||
index = parent.text.length - 1;
|
||||
} else {
|
||||
isTextLeaf = false;
|
||||
index = parent.children.length - 1;
|
||||
}
|
||||
this.address.push({ parent, index });
|
||||
if (isTextLeaf) break;
|
||||
}
|
||||
if (!_isTextLeaf(parent)) throw TypeError("TextLeaf not found!");
|
||||
let length = parent.text[index].length,
|
||||
to = this.curLine.from - 1;
|
||||
this.curLine = new ILine(
|
||||
to - length,
|
||||
to,
|
||||
this.curLine.number - 1,
|
||||
parent.text[index]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public goto(offset: number): TextCursor {
|
||||
if (offset < this.curLine.from) while (offset < this.curLine.from && this.prev());
|
||||
else if (offset > this.curLine.to) while (offset > this.curLine.to && this.next());
|
||||
return this;
|
||||
}
|
||||
|
||||
public gotoLine(linePos: number): TextCursor {
|
||||
if (linePos < this.curLine.number) while (this.curLine.number != linePos && this.prev());
|
||||
else if (linePos > this.curLine.number) while (this.curLine.number != linePos && this.next());
|
||||
return this;
|
||||
}
|
||||
|
||||
public gotoLast(): TextCursor {
|
||||
this.address.splice(1);
|
||||
let { parent, index } = this.address[0],
|
||||
root = parent;
|
||||
while (_isTextNode(parent)) {
|
||||
index = parent.children.length - 1;
|
||||
if (root !== parent) this.address.push({ parent, index });
|
||||
else this.address[0].index = index;
|
||||
parent = parent.children[index] as TextNode | TextLeaf;
|
||||
}
|
||||
index = parent.text.length - 1;
|
||||
if (root !== parent) this.address[0].index = index;
|
||||
else this.address.push({ parent, index });
|
||||
let lineStr = parent.text[index];
|
||||
this.curLine = {
|
||||
from: root.length - lineStr.length,
|
||||
to: root.length,
|
||||
text: lineStr,
|
||||
number: root.lines,
|
||||
length: lineStr.length
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
public getPrevLine(): Line | null {
|
||||
if (!this.prev() || this.curLine.number <= 1) return null;
|
||||
let prevLine = this.curLine;
|
||||
this.next();
|
||||
return prevLine;
|
||||
}
|
||||
|
||||
public getNextLine(): Line | null {
|
||||
if (!this.next() || this.curLine.number >= this.doc.lines) return null;
|
||||
let nextLine = this.curLine;
|
||||
this.prev();
|
||||
return nextLine;
|
||||
}
|
||||
|
||||
public static atOffset(doc: Text, offset: number): TextCursor {
|
||||
let { address, line: curLine } = getLineAddressAt(doc, offset),
|
||||
cursor = new TextCursor();
|
||||
cursor.address = address;
|
||||
cursor.curLine = curLine;
|
||||
return cursor;
|
||||
}
|
||||
}
|
||||
|
||||
export function getNextLine(doc: Text, offsetOrLine: number | Line): Line | null {
|
||||
let line = offsetOrLine instanceof Line ? offsetOrLine : doc.lineAt(offsetOrLine);
|
||||
if (line.number >= doc.lines) { return null }
|
||||
return doc.line(line.number + 1);
|
||||
}
|
||||
|
||||
export function getBlockEndAt(doc: Text, offsetOrLine: number | Line, blankLineAsEnd = true): Line {
|
||||
let line = offsetOrLine instanceof Line ? offsetOrLine : doc.lineAt(offsetOrLine);
|
||||
while (line.number < doc.lines) {
|
||||
line = doc.line(line.number + 1);
|
||||
if (isBlankLine(line)) {
|
||||
if (!blankLineAsEnd) { line = doc.line(line.number - 1) }
|
||||
break;
|
||||
}
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
export function getBlocks(doc: Text, range: PlainRange, lookBehind = false, lookAhead = false): { start: number, end: number }[] {
|
||||
let blocks: { start: number, end: number }[] = [],
|
||||
line = doc.lineAt(range.from),
|
||||
initLine = line,
|
||||
curBlock: { start: number, end: number } | undefined;
|
||||
if (!isBlankLine(line) && lookBehind) {
|
||||
let blockStart = getBlockStartAt(doc, line);
|
||||
curBlock = { start: blockStart.number, end: line.number + 1 };
|
||||
blocks.push(curBlock);
|
||||
}
|
||||
for (; range.to >= line.from; line = doc.line(line.number + 1)) {
|
||||
if (isBlankLine(line)) { curBlock = undefined }
|
||||
else if (curBlock) { curBlock.end++ }
|
||||
else {
|
||||
curBlock = { start: line.number, end: line.number + 1 };
|
||||
blocks.push(curBlock);
|
||||
}
|
||||
if (line.number >= doc.lines) { break }
|
||||
}
|
||||
if (curBlock && lookAhead) {
|
||||
curBlock.end = getBlockEndAt(doc, line, false).number + 1;
|
||||
}
|
||||
if (!blocks.length) {
|
||||
blocks.push({ start: initLine.number, end: initLine.number + 1 });
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
export function getBlockStartAt(doc: Text, offsetOrLine: number | Line): Line {
|
||||
let curLine = offsetOrLine instanceof Line ? offsetOrLine : doc.lineAt(offsetOrLine),
|
||||
prevLine = curLine.number > 1 ? doc.line(curLine.number - 1) : null;
|
||||
if (isBlankLine(curLine)) { return curLine }
|
||||
while (prevLine) {
|
||||
if (isBlankLine(prevLine)) { break }
|
||||
curLine = prevLine;
|
||||
prevLine = curLine.number > 1 ? doc.line(curLine.number - 1) : null;
|
||||
}
|
||||
return curLine;
|
||||
}
|
||||
|
||||
export function getPrevLine(doc: Text, offsetOrLineNum: number | Line): Line | null {
|
||||
let lineNum: number;
|
||||
if (offsetOrLineNum instanceof Line) {
|
||||
lineNum = offsetOrLineNum.number;
|
||||
} else {
|
||||
lineNum = doc.lineAt(offsetOrLineNum).number;
|
||||
}
|
||||
if (lineNum <= 1) { return null }
|
||||
return doc.line(lineNum - 1);
|
||||
}
|
||||
|
||||
export function isBlankLine(line: Line): boolean {
|
||||
return !line.text.trimEnd();
|
||||
}
|
||||
|
||||
export function isBlockEnd(doc: Text, line: Line): boolean {
|
||||
let nextLine = getNextLine(doc, line);
|
||||
return !nextLine || isBlankLine(nextLine);
|
||||
}
|
||||
|
||||
export function isBlockStart(doc: Text, line: Line): boolean {
|
||||
let prevLine = getPrevLine(doc, line);
|
||||
return !prevLine || isBlankLine(prevLine);
|
||||
}
|
||||
|
||||
export function sliceStrFromLine(line: Line, from: number, to: number): string {
|
||||
from -= line.from;
|
||||
to -= line.from;
|
||||
return line.text.slice(from, to);
|
||||
}
|
||||
|
||||
export function getLineAddressAt(doc: Text, offset: number): { address: LineAddress, line: Line } {
|
||||
let parent = doc,
|
||||
address: LineAddress = [],
|
||||
lineNum = 0,
|
||||
lineStr = "",
|
||||
passedLen = 0;
|
||||
|
||||
if (offset < 0 || offset > parent.length) throw RangeError();
|
||||
|
||||
while (_isTextNode(parent)) {
|
||||
let branches = parent.children;
|
||||
for (let i = 0; i < branches.length; i++) {
|
||||
let curBranch = branches[i];
|
||||
if (offset >= passedLen && offset <= passedLen + curBranch.length) {
|
||||
address.push({ parent, index: i });
|
||||
parent = curBranch;
|
||||
break;
|
||||
}
|
||||
passedLen += curBranch.length + 1;
|
||||
lineNum += curBranch.lines;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_isTextLeaf(parent)) throw TypeError();
|
||||
|
||||
for (let i = 0; i < parent.text.length; i++) {
|
||||
let curLineStr = parent.text[i];
|
||||
lineNum++;
|
||||
if (offset >= passedLen && offset <= passedLen + curLineStr.length) {
|
||||
address.push({ parent, index: i });
|
||||
lineStr = curLineStr;
|
||||
break;
|
||||
}
|
||||
passedLen += curLineStr.length + 1;
|
||||
}
|
||||
|
||||
let line = new ILine(
|
||||
passedLen,
|
||||
passedLen + lineStr.length,
|
||||
lineNum,
|
||||
lineStr
|
||||
);
|
||||
return { address, line };
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
import { Language, LanguageState } from "@codemirror/language";
|
||||
import { StateEffectType, StateField } from "@codemirror/state";
|
||||
|
||||
export function hookLangState(): {
|
||||
langStateField: StateField<LanguageState>,
|
||||
langStateFxType: StateEffectType<LanguageState>
|
||||
} {
|
||||
let langStateField = Object.getOwnPropertyDescriptor(Language, "state")?.value as unknown,
|
||||
langStateFxType = Object.getOwnPropertyDescriptor(Language, "setState")?.value as unknown;
|
||||
if (!(langStateField instanceof StateField && langStateFxType instanceof StateEffectType)) {
|
||||
throw TypeError();
|
||||
}
|
||||
|
||||
return { langStateField, langStateFxType };
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
import { PlainRange } from "src/types";
|
||||
|
||||
/**
|
||||
* Region defined here as a `PlainRange` collection arranged in an array.
|
||||
* Each range inside should be sorted in order according to its `from`.
|
||||
*/
|
||||
export type Region = PlainRange[];
|
||||
|
||||
/** Join two regions into a single region. */
|
||||
export function joinRegions(regionA: Region, regionB: Region): Region {
|
||||
let unionRegion: Region = [];
|
||||
for (let i = 0, j = 0; i < regionA.length || j < regionB.length;) {
|
||||
// Joinned range only occurs between two touched/intersected ranges.
|
||||
if (!regionB[j] || regionA[i] && regionA[i].to < regionB[j].from) {
|
||||
unionRegion.push(Object.assign({}, regionA[i]));
|
||||
i++; continue;
|
||||
}
|
||||
if (!regionA[i] || regionB[j] && regionB[j].to < regionA[i].from) {
|
||||
unionRegion.push(Object.assign({}, regionB[j]));
|
||||
j++; continue;
|
||||
}
|
||||
unionRegion.push({
|
||||
from: Math.min(regionA[i].from, regionB[j].from),
|
||||
to: Math.max(regionA[i].to, regionB[j].to)
|
||||
});
|
||||
i++; j++;
|
||||
}
|
||||
return unionRegion;
|
||||
}
|
||||
|
||||
/** Check whether the offset touches the range. */
|
||||
export function isTouched(offset: number, range: PlainRange): boolean {
|
||||
return offset >= range.from && offset <= range.to;
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
import { EditorSelection, SelectionRange, Text } from "@codemirror/state";
|
||||
|
||||
export function trimSelectionRange(range: SelectionRange, doc: Text): SelectionRange {
|
||||
let str = doc.sliceString(range.from, range.to),
|
||||
preSpaceLen = str.length - str.trimStart().length,
|
||||
postSpaceLen = str.length - str.trimEnd().length;
|
||||
if (str.length == preSpaceLen) {
|
||||
return EditorSelection.cursor(range.from);
|
||||
}
|
||||
return EditorSelection.range(range.from + preSpaceLen, range.to - postSpaceLen);
|
||||
}
|
||||
|
||||
export function trimSelection(selection: EditorSelection, doc: Text):EditorSelection {
|
||||
for (let i = 0; i < selection.ranges.length; i++) {
|
||||
let trimmed = trimSelectionRange(selection.ranges[i], doc);
|
||||
selection = selection.replaceRange(trimmed, i);
|
||||
}
|
||||
return selection;
|
||||
}
|
||||
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
import { PlainRange, Token, TokenGroup } from "src/types";
|
||||
|
||||
type TokenPartsRanges = Record<"openRange" | "closeRange" | "tagRange" | "contentRange", PlainRange>;
|
||||
interface IterTokenGroupSpec {
|
||||
tokens: TokenGroup,
|
||||
ranges: PlainRange[] | readonly PlainRange[]
|
||||
callback: (token: Token) => unknown,
|
||||
}
|
||||
|
||||
export function getTagRange(token: Token): PlainRange {
|
||||
let from = token.from + token.openLen,
|
||||
to = from + token.tagLen;
|
||||
return { from, to };
|
||||
}
|
||||
|
||||
export function provideTokenPartsRanges(token: Token): TokenPartsRanges {
|
||||
let openRange = { from: token.from, to: token.from + token.openLen },
|
||||
closeRange = { from: token.to - token.closeLen, to: token.to },
|
||||
tagRange = { from: openRange.to, to: openRange.to + token.tagLen },
|
||||
contentRange = { from: (token.tagAsContent ? tagRange.from : tagRange.to), to: closeRange.from };
|
||||
return { openRange, closeRange, tagRange, contentRange }
|
||||
}
|
||||
|
||||
export function isToken(range: PlainRange): range is Token {
|
||||
let { type, openLen, tagLen, closeLen } = range as Token;
|
||||
return type !== undefined && openLen !== undefined && tagLen !== undefined && closeLen !== undefined;
|
||||
}
|
||||
|
||||
export function iterTokenGroup(spec: IterTokenGroupSpec): void {
|
||||
let { tokens, ranges, callback } = spec,
|
||||
tokenI = findTokenIndexAt(tokens, ranges[0]?.from ?? 0),
|
||||
rangeI = 0;
|
||||
|
||||
if (tokenI === null) return;
|
||||
|
||||
while (
|
||||
tokenI < tokens.length &&
|
||||
rangeI < ranges.length
|
||||
) {
|
||||
if (ranges[rangeI].to <= tokens[tokenI].from) { rangeI++; continue }
|
||||
if (
|
||||
tokens[tokenI].from < ranges[rangeI].to &&
|
||||
tokens[tokenI].to > ranges[rangeI].from
|
||||
) {
|
||||
callback(tokens[tokenI]);
|
||||
}
|
||||
tokenI++;
|
||||
}
|
||||
}
|
||||
|
||||
export function findTokenIndexAt(tokens: TokenGroup, offset: number): number | null {
|
||||
if (!tokens.length) return null;
|
||||
|
||||
let base = 32768,
|
||||
factor = 2,
|
||||
index = 0;
|
||||
|
||||
while (base !== 32) {
|
||||
if (tokens.length > base)
|
||||
while (tokens[index].from <= offset) {
|
||||
let end = Math.min(index + base, tokens.length);
|
||||
if (tokens[end - 1].to < offset) index = end;
|
||||
else break;
|
||||
}
|
||||
|
||||
base /= factor;
|
||||
factor *= 2;
|
||||
}
|
||||
|
||||
for (
|
||||
let end = Math.min(index + base, tokens.length);
|
||||
index < end && tokens[index].to < offset;
|
||||
index++
|
||||
);
|
||||
|
||||
return index < tokens.length ? index : null;
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
/** Delimiter position. */
|
||||
export enum Delimiter {
|
||||
/** Opening delimiter */
|
||||
OPEN,
|
||||
/** Closing delimiter */
|
||||
CLOSE
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
export enum DisplayBehaviour {
|
||||
ALWAYS,
|
||||
TAG_TOUCHED,
|
||||
SYNTAX_TOUCHED
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
export enum Field {
|
||||
TOGGLE,
|
||||
MULTI_TOGGLE,
|
||||
DROPDOWN,
|
||||
COLOR,
|
||||
TEXT,
|
||||
TEXT_AREA,
|
||||
SLIDER
|
||||
}
|
||||
|
|
@ -2,18 +2,24 @@
|
|||
* Formatting type for each token.
|
||||
*/
|
||||
export enum Format {
|
||||
/** Insertion (underline) */
|
||||
INSERTION = 1,
|
||||
/** Spoiler */
|
||||
SPOILER,
|
||||
/** Superscript */
|
||||
SUPERSCRIPT,
|
||||
/** Subscript */
|
||||
SUBSCRIPT,
|
||||
/** Highlight */
|
||||
HIGHLIGHT,
|
||||
/** Custom span */
|
||||
CUSTOM_SPAN,
|
||||
/** Fenced div (custom block) */
|
||||
FENCED_DIV
|
||||
/** Align left mark */
|
||||
ALIGN_LEFT = 1,
|
||||
/** Align right mark */
|
||||
ALIGN_RIGHT,
|
||||
/** Align center mark */
|
||||
ALIGN_CENTER,
|
||||
/** Align justify mark */
|
||||
ALIGN_JUSTIFY,
|
||||
/** Insertion (underline) */
|
||||
INSERTION,
|
||||
/** Spoiler */
|
||||
SPOILER,
|
||||
/** Superscript */
|
||||
SUPERSCRIPT,
|
||||
/** Subscript */
|
||||
SUBSCRIPT,
|
||||
/** Highlight */
|
||||
HIGHLIGHT,
|
||||
/** Highlight color tag */
|
||||
HIGHLIGHT_COLOR_TAG,
|
||||
}
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
export enum LineCtx {
|
||||
NONE,
|
||||
HR_LINE,
|
||||
HEADING,
|
||||
BLOCKQUOTE,
|
||||
CODEBLOCK,
|
||||
LIST_HEAD,
|
||||
LIST,
|
||||
FOOTNOTE_HEAD,
|
||||
FOOTNOTE,
|
||||
TABLE,
|
||||
TABLE_DELIM
|
||||
NONE,
|
||||
HR_LINE,
|
||||
HEADING,
|
||||
BLOCKQUOTE,
|
||||
CODEBLOCK,
|
||||
LIST_HEAD,
|
||||
LIST,
|
||||
FOOTNOTE_HEAD,
|
||||
FOOTNOTE,
|
||||
TABLE,
|
||||
TABLE_DELIM
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
export enum MarkdownViewMode {
|
||||
NONE = 0, // 00
|
||||
PREVIEW_MODE = 1, // 01
|
||||
EDITOR_MODE = 2, // 10
|
||||
ALL = 3, // 11
|
||||
}
|
||||
6
src/enums/SettingOpt1.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export enum SettingOpt1 {
|
||||
DISABLED = 0, // 00
|
||||
PREVIEW_MODE = 1, // 01
|
||||
EDITOR_MODE = 2, // 10
|
||||
ALL = 3, // 11
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
export enum Theme {
|
||||
LIGHT,
|
||||
DARK
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
export enum TokenLevel {
|
||||
BLOCK,
|
||||
INLINE
|
||||
}
|
||||
19
src/enums/TokenRole.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Indicate that the token is recognized as a part of
|
||||
* tokens sequence, or a stand-alone one.
|
||||
*/
|
||||
export enum TokenRole {
|
||||
/** Opening delimiter */
|
||||
OPEN,
|
||||
/**
|
||||
* Act as a metadata stored in a token sequence.
|
||||
* In this case, it's only used as a highlight color tag
|
||||
*/
|
||||
TAG,
|
||||
/** Content of a token sequence */
|
||||
CONTENT,
|
||||
/** Closing delimiter */
|
||||
CLOSE,
|
||||
/** Stand-alone token that isn't a part of any sequence */
|
||||
SINGLE
|
||||
}
|
||||
|
|
@ -7,17 +7,17 @@
|
|||
* spoiler, sup, and sub.
|
||||
*/
|
||||
export enum TokenStatus {
|
||||
/**
|
||||
* The token is being processed through the parser queue.
|
||||
* After that, its status switched to either `ACTIVE` or `INACTIVE`.
|
||||
*/
|
||||
PENDING,
|
||||
/** The token is ready for the rendering phase. */
|
||||
ACTIVE,
|
||||
/**
|
||||
* The token stated as `INACTIVE` by default doesn't
|
||||
* go through rendering phase. Often used to stating
|
||||
* token sequence that doesn't have closing delimiter.
|
||||
*/
|
||||
INACTIVE
|
||||
/**
|
||||
* The token is being processed through the parser queue.
|
||||
* After that, its status switched to either `ACTIVE` or `INACTIVE`.
|
||||
*/
|
||||
PENDING,
|
||||
/** The token is ready for the rendering phase. */
|
||||
ACTIVE,
|
||||
/**
|
||||
* The token stated as `INACTIVE` by default doesn't
|
||||
* go through rendering phase. Often used to stating
|
||||
* token sequence that doesn't have closing delimiter.
|
||||
*/
|
||||
INACTIVE
|
||||
}
|
||||
|
|
@ -1,9 +1,5 @@
|
|||
export * from "./LineCtx";
|
||||
export * from "./Delimiter";
|
||||
export * from "./TokenRole";
|
||||
export * from "./TokenStatus";
|
||||
export * from "./Format";
|
||||
export * from "./MarkdownViewMode";
|
||||
export * from "./TokenLevel";
|
||||
export * from "./Field";
|
||||
export * from "./DisplayBehaviour";
|
||||
export * from "./Theme";
|
||||
export * from "./SettingOpt1";
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
import { Format, MarkdownViewMode } from "src/enums";
|
||||
import { PluginSettings, InlineFormat } from "src/types";
|
||||
import { InlineRules } from "src/format-configs/rules";
|
||||
import { EditorDelimLookup } from "src/editor-mode/preprocessor/parser-configs";
|
||||
import { PreviewDelimLookup } from "src/preview-mode/post-processor/configs";
|
||||
|
||||
export function configureDelimLookup(settings: PluginSettings): void {
|
||||
for (let key in EditorDelimLookup) {
|
||||
delete EditorDelimLookup[key];
|
||||
}
|
||||
for (let key in PreviewDelimLookup) {
|
||||
delete PreviewDelimLookup[key];
|
||||
}
|
||||
|
||||
// editor
|
||||
if (settings.insertion & MarkdownViewMode.EDITOR_MODE) {
|
||||
EditorDelimLookup[InlineRules[Format.INSERTION].char] = Format.INSERTION;
|
||||
}
|
||||
if (settings.spoiler & MarkdownViewMode.EDITOR_MODE) {
|
||||
EditorDelimLookup[InlineRules[Format.SPOILER].char] = Format.SPOILER;
|
||||
}
|
||||
if (settings.superscript & MarkdownViewMode.EDITOR_MODE) {
|
||||
EditorDelimLookup[InlineRules[Format.SUPERSCRIPT].char] = Format.SUPERSCRIPT;
|
||||
}
|
||||
if (settings.subscript & MarkdownViewMode.EDITOR_MODE) {
|
||||
EditorDelimLookup[InlineRules[Format.SUBSCRIPT].char] = Format.SUBSCRIPT;
|
||||
}
|
||||
if (settings.customHighlight & MarkdownViewMode.EDITOR_MODE) {
|
||||
EditorDelimLookup[InlineRules[Format.HIGHLIGHT].char] = Format.HIGHLIGHT;
|
||||
}
|
||||
if (settings.customSpan & MarkdownViewMode.EDITOR_MODE) {
|
||||
EditorDelimLookup[InlineRules[Format.CUSTOM_SPAN].char] = Format.CUSTOM_SPAN;
|
||||
}
|
||||
// preview
|
||||
if (settings.insertion & MarkdownViewMode.PREVIEW_MODE) {
|
||||
PreviewDelimLookup[InlineRules[Format.INSERTION].char] = Format.INSERTION;
|
||||
}
|
||||
if (settings.spoiler & MarkdownViewMode.PREVIEW_MODE) {
|
||||
PreviewDelimLookup[InlineRules[Format.SPOILER].char] = Format.SPOILER;
|
||||
}
|
||||
if (settings.superscript & MarkdownViewMode.PREVIEW_MODE) {
|
||||
PreviewDelimLookup[InlineRules[Format.SUPERSCRIPT].char] = Format.SUPERSCRIPT;
|
||||
}
|
||||
if (settings.subscript & MarkdownViewMode.PREVIEW_MODE) {
|
||||
PreviewDelimLookup[InlineRules[Format.SUBSCRIPT].char] = Format.SUBSCRIPT;
|
||||
}
|
||||
if (settings.customSpan & MarkdownViewMode.PREVIEW_MODE) {
|
||||
PreviewDelimLookup[InlineRules[Format.CUSTOM_SPAN].char] = Format.CUSTOM_SPAN;
|
||||
}
|
||||
}
|
||||
|
||||
export function isInlineFormat(type: Format): type is InlineFormat {
|
||||
return type >= Format.INSERTION && type <= Format.CUSTOM_SPAN
|
||||
}
|
||||
|
||||
export function supportTag(type: Format): type is Format.HIGHLIGHT | Format.CUSTOM_SPAN | Format.FENCED_DIV {
|
||||
return type >= Format.HIGHLIGHT || type == Format.FENCED_DIV;
|
||||
}
|
||||
|
||||
export function trimTag(tagStr: string): string {
|
||||
return tagStr
|
||||
.trim()
|
||||
.replaceAll(/\s{2,}/g, " ");
|
||||
}
|
||||
|
|
@ -1,130 +0,0 @@
|
|||
import { Format } from "src/enums";
|
||||
import { BlockFormat, InlineFormat, BlockFormatRule, InlineFormatRule } from "src/types";
|
||||
|
||||
export const BlockRules: Record<BlockFormat, BlockFormatRule> = {
|
||||
[Format.FENCED_DIV]: {
|
||||
char: ":",
|
||||
length: 3,
|
||||
exactLen: false,
|
||||
class: "fenced-div"
|
||||
}
|
||||
}
|
||||
|
||||
export const InlineRules: Record<InlineFormat, InlineFormatRule> = {
|
||||
[Format.INSERTION]: {
|
||||
char: "+",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
mustBeClosed: true,
|
||||
class: "ins",
|
||||
getEl: () => document.createElement("ins"),
|
||||
builtin: false
|
||||
},
|
||||
[Format.SPOILER]: {
|
||||
char: "|",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
mustBeClosed: true,
|
||||
class: "spoiler",
|
||||
getEl: () => {
|
||||
let spoilerEl = document.createElement("span");
|
||||
spoilerEl.addClass("spoiler");
|
||||
spoilerEl.addEventListener("click", (evt) => {
|
||||
let spoilerEl = evt.currentTarget as Element,
|
||||
isHidden = !spoilerEl.hasClass("spoiler-revealed");
|
||||
spoilerEl.toggleClass("spoiler-revealed", isHidden);
|
||||
});
|
||||
return spoilerEl;
|
||||
},
|
||||
builtin: false
|
||||
},
|
||||
[Format.SUPERSCRIPT]: {
|
||||
char: "^",
|
||||
length: 1,
|
||||
exactLen: true,
|
||||
allowSpace: false,
|
||||
mustBeClosed: true,
|
||||
class: "sup",
|
||||
getEl: () => document.createElement("sup"),
|
||||
builtin: false
|
||||
},
|
||||
[Format.SUBSCRIPT]: {
|
||||
char: "~",
|
||||
length: 1,
|
||||
exactLen: true,
|
||||
allowSpace: false,
|
||||
mustBeClosed: true,
|
||||
class: "sub",
|
||||
getEl: () => document.createElement("sub"),
|
||||
builtin: false
|
||||
},
|
||||
[Format.HIGHLIGHT]: {
|
||||
char: "=",
|
||||
length: 2,
|
||||
exactLen: false,
|
||||
allowSpace: true,
|
||||
mustBeClosed: false,
|
||||
class: "custom-highlight",
|
||||
getEl: () => document.createElement("mark"),
|
||||
builtin: true
|
||||
},
|
||||
[Format.CUSTOM_SPAN]: {
|
||||
char: "!",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
mustBeClosed: true,
|
||||
class: "custom-span",
|
||||
getEl() {
|
||||
let el = document.createElement("span");
|
||||
el.classList.add(InlineRules[Format.CUSTOM_SPAN].class);
|
||||
return el;
|
||||
},
|
||||
builtin: false
|
||||
}
|
||||
}
|
||||
|
||||
export const Formats = {
|
||||
ALL: (() => {
|
||||
let formats: Format[] = [];
|
||||
for (let format in InlineRules) { formats.push(parseInt(format)) }
|
||||
for (let format in BlockRules) { formats.push(parseInt(format)) }
|
||||
return formats;
|
||||
})(),
|
||||
ALL_BLOCK: (() => {
|
||||
let formats: BlockFormat[] = [];
|
||||
for (let format in BlockRules) { formats.push(parseInt(format)) }
|
||||
return formats;
|
||||
})(),
|
||||
ALL_INLINE: (() => {
|
||||
let formats: InlineFormat[] = [];
|
||||
for (let format in InlineRules) { formats.push(parseInt(format)) }
|
||||
return formats;
|
||||
})(),
|
||||
SPACE_RESTRICTED_INLINE: (() => {
|
||||
let formats: InlineFormat[] = [];
|
||||
for (let format in InlineRules) {
|
||||
let type = Number.parseInt(format) as InlineFormat;
|
||||
if (!InlineRules[type].allowSpace) { formats.push(type) }
|
||||
}
|
||||
return formats;
|
||||
})(),
|
||||
SPACE_ALLOWED_INLINE: (() => {
|
||||
let formats: InlineFormat[] = [];
|
||||
for (let format in InlineRules) {
|
||||
let type = Number.parseInt(format) as InlineFormat;
|
||||
if (InlineRules[type].allowSpace) { formats.push(type) }
|
||||
}
|
||||
return formats;
|
||||
})(),
|
||||
NON_BUILTIN_INLINE: (() => {
|
||||
let formats: InlineFormat[] = [];
|
||||
for (let format in InlineRules) {
|
||||
let type = Number.parseInt(format) as InlineFormat;
|
||||
if (!InlineRules[type].builtin) { formats.push(type) }
|
||||
}
|
||||
return formats;
|
||||
})()
|
||||
}
|
||||
3
src/preview-mode/configs/PreviewDelimLookup.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import { MainFormat } from "src/types";
|
||||
|
||||
export const PreviewDelimLookup: Record<string, MainFormat> = {}
|
||||
8
src/preview-mode/configs/SkippedClasses.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
export const SkippedClasses = [
|
||||
"internal-link",
|
||||
"external-link",
|
||||
"math",
|
||||
"internal-embed",
|
||||
"list-bullet",
|
||||
"collapse-indicator"
|
||||
];
|
||||
2
src/preview-mode/configs/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from "./SkippedClasses";
|
||||
export * from "./PreviewDelimLookup";
|
||||
148
src/preview-mode/parser/PreviewModeParser.ts
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
import { FormatRules } from "src/shared-configs";
|
||||
import { Format } from "src/enums";
|
||||
import { MainFormat2 } from "src/types";
|
||||
import { SkippedClasses, PreviewDelimLookup } from "src/preview-mode/configs";
|
||||
import { hasClasses, isWhitespace } from "src/preview-mode/utils";
|
||||
|
||||
export class PreviewModeParser {
|
||||
root: Element;
|
||||
walker: TreeWalker;
|
||||
offset = 0;
|
||||
curNode: Node;
|
||||
nodeChanged = false;
|
||||
stack: MainFormat2[] = [];
|
||||
queue: { [F in MainFormat2]: Range | null } = {
|
||||
[Format.INSERTION]: null,
|
||||
[Format.SPOILER]: null,
|
||||
[Format.SUPERSCRIPT]: null,
|
||||
[Format.SUBSCRIPT]: null
|
||||
}
|
||||
parsingQueue: PreviewModeParser[];
|
||||
constructor(root: Element, parsingQueue: PreviewModeParser[]) {
|
||||
this.root = root;
|
||||
this.walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT);
|
||||
this.walker.nextNode();
|
||||
this.curNode = this.walker.currentNode;
|
||||
this.parsingQueue = parsingQueue;
|
||||
}
|
||||
streamParse() {
|
||||
do {
|
||||
if (this.curNode instanceof Text) {
|
||||
this.offset = 0;
|
||||
this.parseTextNode();
|
||||
} else if (this.curNode instanceof Element) {
|
||||
if (this.isSkipped(this.curNode)) {
|
||||
this.resolve(Format.SUPERSCRIPT);
|
||||
this.resolve(Format.SUBSCRIPT);
|
||||
} else if (this.curNode.textContent) {
|
||||
this.parsingQueue.push(new PreviewModeParser(this.curNode, this.parsingQueue));
|
||||
if (/\s/.test(this.curNode.textContent)) {
|
||||
this.resolve(Format.SUPERSCRIPT);
|
||||
this.resolve(Format.SUBSCRIPT);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (this.nextNode())
|
||||
this.forceResolveAll();
|
||||
}
|
||||
parseTextNode() {
|
||||
let str = this.curNode.textContent ?? "";
|
||||
while (!this.nodeChanged && this.offset < str.length) {
|
||||
let char = str[this.offset],
|
||||
type = PreviewDelimLookup[char];
|
||||
if (char == " " || char == "\n" || char == "\t") {
|
||||
this.resolve(Format.SUPERSCRIPT);
|
||||
this.resolve(Format.SUBSCRIPT);
|
||||
this.offset++;
|
||||
continue;
|
||||
}
|
||||
if (!type || type == Format.HIGHLIGHT) {
|
||||
this.offset++;
|
||||
continue;
|
||||
}
|
||||
this.tokenize(type);
|
||||
}
|
||||
this.nodeChanged = false;
|
||||
}
|
||||
finalize(type: MainFormat2, open: Range, content: Range, close: Range) {
|
||||
let wrapper = FormatRules[type].getEl();
|
||||
close.deleteContents();
|
||||
content.surroundContents(wrapper);
|
||||
open.deleteContents();
|
||||
if (wrapper == this.curNode.nextSibling) {
|
||||
this.nextNode();
|
||||
} else {
|
||||
this.prevNode();
|
||||
}
|
||||
this.nodeChanged = true;
|
||||
}
|
||||
resolve(type: MainFormat2, close?: Range) {
|
||||
if (close && this.queue[type]) {
|
||||
let content = new Range(),
|
||||
open = this.queue[type];
|
||||
content.setStart(open.endContainer, open.endOffset);
|
||||
content.setEnd(close.startContainer, close.startOffset);
|
||||
this.stack.findLast((t, i) => {
|
||||
this.queue[t] = null;
|
||||
if (t == type) {
|
||||
this.stack.splice(i);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
this.finalize(type, open, content, close);
|
||||
} else {
|
||||
this.queue[type] = null;
|
||||
this.stack = this.stack.filter(t => t != type);
|
||||
}
|
||||
}
|
||||
forceResolveAll() {
|
||||
for (let i = Format.INSERTION as MainFormat2; i <= Format.SUPERSCRIPT; i++) {
|
||||
this.resolve(i);
|
||||
}
|
||||
}
|
||||
tokenize(type: MainFormat2) {
|
||||
let { length: reqLength, char } = FormatRules[type],
|
||||
str = this.curNode.textContent!,
|
||||
length = 0,
|
||||
hasOpen = !!this.queue[type],
|
||||
hasSpaceBefore = isWhitespace(str[this.offset - 1]),
|
||||
hasSpaceAfter: boolean;
|
||||
while (str[this.offset] == char) { this.offset++; length++ }
|
||||
hasSpaceAfter = isWhitespace(str[this.offset]);
|
||||
if (hasOpen && hasSpaceBefore || !hasOpen && hasSpaceAfter || length != reqLength) { return false }
|
||||
let range = new Range();
|
||||
range.setStart(this.curNode, this.offset - length);
|
||||
range.setEnd(this.curNode, this.offset);
|
||||
this.pushDelim(type, range);
|
||||
return true;
|
||||
}
|
||||
pushDelim(type: MainFormat2, delim: Range) {
|
||||
if (this.queue[type]) {
|
||||
this.resolve(type, delim);
|
||||
} else {
|
||||
this.queue[type] = delim;
|
||||
this.stack.push(type);
|
||||
}
|
||||
}
|
||||
nextNode() {
|
||||
if (this.walker.nextSibling()) {
|
||||
this.curNode = this.walker.currentNode;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
prevNode() {
|
||||
if (this.walker.previousSibling()) {
|
||||
this.curNode = this.walker.currentNode;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
isSkipped(el: Element) {
|
||||
return (
|
||||
hasClasses(el, SkippedClasses) ||
|
||||
el.tagName == "CODE" || el.tagName == "IMG"
|
||||
);
|
||||
}
|
||||
}
|
||||
1
src/preview-mode/parser/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./PreviewModeParser";
|
||||
14
src/preview-mode/post-processor/CustomAlign.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
export class CustomAlign {
|
||||
constructor() {}
|
||||
decorate(el: HTMLElement) {
|
||||
if (!(el.firstChild instanceof Text && el.firstChild.textContent)) { return }
|
||||
let textNode = el.firstChild,
|
||||
alignRe = /!!(?:left|right|center|justify)!!/y,
|
||||
match = alignRe.exec(textNode.textContent ?? "");
|
||||
if (match) {
|
||||
let align = match[0].slice(2, match[0].length - 2);
|
||||
textNode.replaceData(0, match[0].length, "");
|
||||
el.addClass("align-" + align);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/preview-mode/post-processor/CustomHighlight.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
export class CustomHighlight {
|
||||
constructor() {}
|
||||
decorate(el: HTMLElement) {
|
||||
let markElements = el.querySelectorAll<HTMLElement>("mark"),
|
||||
colorTagRe = /^\{([a-z0-9\-_]+)\}/i;
|
||||
markElements.forEach((el) => {
|
||||
if (!(el.firstChild instanceof Text && el.firstChild.textContent)) { return }
|
||||
let color = colorTagRe.exec(el.firstChild.textContent)?.[1];
|
||||
if (color) {
|
||||
let from = 0, to = from + color.length + 2;
|
||||
el.firstChild.replaceData(from, to - from, "");
|
||||
el.classList.add("custom-highlight", `custom-highlight-${color}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
53
src/preview-mode/post-processor/PreviewExtendedSyntax.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { MarkdownPostProcessor } from "obsidian";
|
||||
import { SettingOpt1 } from "src/enums";
|
||||
import { PreviewModeParser } from "src/preview-mode/parser";
|
||||
import { CustomAlign, CustomHighlight } from "src/preview-mode/post-processor";
|
||||
import { PluginSettings } from "src/types";
|
||||
|
||||
export class PreviewExtendedSyntax {
|
||||
private readonly query = "p, h1, h2, h3, h4, h5, h6, td, th, li:not(:has(p)), .callout-title-inner";
|
||||
private customAlign = new CustomAlign();
|
||||
private customHighlight = new CustomHighlight();
|
||||
settings: PluginSettings;
|
||||
constructor(settings: PluginSettings) {
|
||||
this.settings = Object.assign({}, settings);
|
||||
}
|
||||
private decorate(container: HTMLElement) {
|
||||
let targetedEls = container.querySelectorAll(this.query),
|
||||
parsingQueue: PreviewModeParser[] = [];
|
||||
for (let i = 0; i < targetedEls.length; i++) {
|
||||
new PreviewModeParser(targetedEls[i], parsingQueue).streamParse();
|
||||
for (let i = 0; i < parsingQueue.length; i++) {
|
||||
parsingQueue[i].streamParse();
|
||||
if (i >= 100) { throw Error(`${parsingQueue}`) }
|
||||
}
|
||||
parsingQueue.splice(0);
|
||||
}
|
||||
}
|
||||
private toBeDecorated(container: HTMLElement) {
|
||||
let firstChild = container.firstElementChild;
|
||||
if (firstChild && (
|
||||
firstChild instanceof HTMLParagraphElement ||
|
||||
firstChild instanceof HTMLTableElement ||
|
||||
firstChild instanceof HTMLUListElement ||
|
||||
firstChild instanceof HTMLOListElement ||
|
||||
firstChild.tagName == "BLOCKQUOTE" ||
|
||||
firstChild.classList.contains("callout")
|
||||
)) { return true }
|
||||
return false;
|
||||
}
|
||||
postProcess: MarkdownPostProcessor = container => {
|
||||
if (this.settings.customHighlight & SettingOpt1.PREVIEW_MODE) {
|
||||
this.customHighlight.decorate(container);
|
||||
}
|
||||
if (
|
||||
this.settings.customAlign & SettingOpt1.PREVIEW_MODE &&
|
||||
container.firstElementChild instanceof HTMLParagraphElement
|
||||
) {
|
||||
this.customAlign.decorate(container.firstElementChild);
|
||||
}
|
||||
if (this.toBeDecorated(container)) {
|
||||
this.decorate(container);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
import { InlineFormat } from "src/types";
|
||||
|
||||
export const COLOR_TAG_RE = /^\{([a-z0-9-]+)\}/i;
|
||||
export const CUSTOM_SPAN_TAG_RE = /^\{([a-z0-9 -]+)\}/i;
|
||||
export const FENCED_DIV_RE = /:{3,}(?=([a-z0-9 -]+))\1$/yi;
|
||||
|
||||
export const PreviewDelimLookup: Record<string, InlineFormat> = {}
|
||||
|
||||
export const SKIPPED_CLASSES = [
|
||||
"internal-link",
|
||||
"external-link",
|
||||
"math",
|
||||
"internal-embed",
|
||||
"list-bullet",
|
||||
"collapse-indicator"
|
||||
];
|
||||
|
|
@ -1,154 +0,0 @@
|
|||
import { MarkdownPostProcessor, MarkdownPostProcessorContext } from "obsidian";
|
||||
import { MarkdownViewMode, Format } from "src/enums";
|
||||
import { PluginSettings } from "src/types";
|
||||
import { InlineRules, BlockRules } from "src/format-configs/rules";
|
||||
import { PreviewModeParser } from "src/preview-mode/post-processor/parser";
|
||||
import { CUSTOM_SPAN_TAG_RE, COLOR_TAG_RE, FENCED_DIV_RE } from "src/preview-mode/post-processor/configs";
|
||||
|
||||
function _trimTag(tagStr: string): string {
|
||||
return tagStr
|
||||
.trim()
|
||||
.replaceAll(/\s{2,}/g, " ");
|
||||
}
|
||||
|
||||
function _isLeafBlock(el: HTMLElement): boolean {
|
||||
return (
|
||||
el instanceof HTMLParagraphElement ||
|
||||
el instanceof HTMLHeadingElement
|
||||
);
|
||||
}
|
||||
|
||||
function _isTableCellWrapper(el: HTMLElement): boolean {
|
||||
return el.hasClass("table-cell-wrapper");
|
||||
}
|
||||
|
||||
function _isCallout(el: HTMLElement): boolean {
|
||||
return el.hasClass("callout");
|
||||
}
|
||||
|
||||
function _drawCustomSpan(settings: PluginSettings, targetEl: HTMLElement): void {
|
||||
let baseCls = InlineRules[Format.CUSTOM_SPAN].class,
|
||||
customSpanElements = targetEl.querySelectorAll<HTMLElement>("." + baseCls);
|
||||
|
||||
customSpanElements.forEach((el) => {
|
||||
if (!(el.firstChild instanceof Text && el.firstChild.textContent)) return;
|
||||
let tag = CUSTOM_SPAN_TAG_RE.exec(el.firstChild.textContent)?.[1];
|
||||
if (tag) {
|
||||
let clsList = _trimTag(tag).split(" ");
|
||||
el.classList.add(...clsList);
|
||||
if (settings.showSpanTagInPreviewMode) { return }
|
||||
let from = 0, to = from + tag.length + 2;
|
||||
el.firstChild.replaceData(from, to - from, "");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function _drawCustomHighlight(settings: PluginSettings, targetEl: HTMLElement): void {
|
||||
let markElements = targetEl.querySelectorAll<HTMLElement>("mark"),
|
||||
baseCls = InlineRules[Format.HIGHLIGHT].class;
|
||||
|
||||
markElements.forEach((el) => {
|
||||
if (!(el.firstChild instanceof Text && el.firstChild.textContent)) { return }
|
||||
let color = COLOR_TAG_RE.exec(el.firstChild.textContent)?.[1];
|
||||
if (color) {
|
||||
el.classList.add(baseCls, `${baseCls}-${color}`);
|
||||
if (settings.showHlTagInPreviewMode) { return }
|
||||
let from = 0, to = from + color.length + 2;
|
||||
el.firstChild.replaceData(from, to - from, "");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function _drawFencedDiv(settings: PluginSettings, targetEl: HTMLElement): void {
|
||||
if (!(targetEl.firstChild instanceof Text && targetEl.firstChild.textContent)) return;
|
||||
|
||||
FENCED_DIV_RE.lastIndex = 0;
|
||||
let baseCls = BlockRules[Format.FENCED_DIV].class,
|
||||
textNode = targetEl.firstChild,
|
||||
lineBreakEl = targetEl.querySelector("br"),
|
||||
match = FENCED_DIV_RE.exec(textNode.textContent ?? "");
|
||||
|
||||
if (match) {
|
||||
let tag = match[1]!,
|
||||
clsList = _trimTag(tag).split(" ");
|
||||
targetEl.addClass(baseCls, ...clsList);
|
||||
if (settings.alwaysShowFencedDivTag & MarkdownViewMode.PREVIEW_MODE) return;
|
||||
targetEl.removeChild(textNode);
|
||||
if (lineBreakEl) { targetEl.removeChild(lineBreakEl) }
|
||||
}
|
||||
}
|
||||
|
||||
export class ReadingModeSyntaxExtender {
|
||||
private readonly _SELECTOR_QUERY = "p, h1, h2, h3, h4, h5, h6, td, th, li:not(:has(p)), .callout-title-inner" as const;
|
||||
private readonly _settings: PluginSettings;
|
||||
|
||||
constructor(settings: PluginSettings) {
|
||||
this._settings = settings;
|
||||
this.postProcess.sortOrder = 0;
|
||||
}
|
||||
|
||||
private _parseInline(targetEl: HTMLElement, capsulated = true): void {
|
||||
let targetedEls = _isTableCellWrapper(targetEl) || !capsulated && _isLeafBlock(targetEl)
|
||||
? [targetEl]
|
||||
: targetEl.querySelectorAll<HTMLElement>(this._SELECTOR_QUERY),
|
||||
parsingQueue: PreviewModeParser[] = [];
|
||||
|
||||
for (let i = 0; i < targetedEls.length; i++) {
|
||||
new PreviewModeParser(targetedEls[i], parsingQueue).streamParse();
|
||||
for (let i = 0; i < parsingQueue.length; i++) {
|
||||
parsingQueue[i].streamParse();
|
||||
if (i >= 100) { throw Error(`${parsingQueue}`) }
|
||||
}
|
||||
parsingQueue.splice(0);
|
||||
}
|
||||
}
|
||||
|
||||
private _isTargeted(sectionEl: HTMLElement, capsulated = true): boolean {
|
||||
let contentEl = capsulated ? sectionEl.firstElementChild as HTMLElement : sectionEl;
|
||||
if (
|
||||
_isTableCellWrapper(sectionEl) || // Intended to draw over editor table cell
|
||||
contentEl && (
|
||||
_isLeafBlock(contentEl) ||
|
||||
_isCallout(contentEl) ||
|
||||
contentEl instanceof HTMLTableElement ||
|
||||
contentEl instanceof HTMLUListElement ||
|
||||
contentEl instanceof HTMLOListElement ||
|
||||
contentEl.tagName == "BLOCKQUOTE"
|
||||
)) { return true }
|
||||
return false;
|
||||
}
|
||||
|
||||
private _draw(sectionEl: HTMLElement, capsulated = true): void {
|
||||
if (!this._isTargeted(sectionEl, capsulated)) return;
|
||||
|
||||
if (this._settings.customHighlight & MarkdownViewMode.PREVIEW_MODE)
|
||||
_drawCustomHighlight(this._settings, sectionEl);
|
||||
|
||||
if (this._settings.fencedDiv & MarkdownViewMode.PREVIEW_MODE) {
|
||||
let targetEl = capsulated ? sectionEl.firstElementChild : sectionEl;
|
||||
if (targetEl instanceof HTMLParagraphElement)
|
||||
_drawFencedDiv(this._settings, targetEl);
|
||||
}
|
||||
|
||||
this._parseInline(sectionEl, capsulated);
|
||||
|
||||
if (this._settings.customSpan & MarkdownViewMode.PREVIEW_MODE)
|
||||
_drawCustomSpan(this._settings, sectionEl);
|
||||
}
|
||||
|
||||
public postProcess: MarkdownPostProcessor = (containerOrSection: HTMLElement, ctx: MarkdownPostProcessorContext) => {
|
||||
let isWholeDoc = containerOrSection == ctx.containerEl,
|
||||
isDefaultExported = containerOrSection.parentElement?.hasClass("print") ?? false,
|
||||
capsulated = !isWholeDoc || isDefaultExported;
|
||||
|
||||
if (isWholeDoc) {
|
||||
if (!this._settings.decoratePDF) return;
|
||||
let sectionEls = containerOrSection.querySelectorAll<HTMLElement>(capsulated ? "&>div" : "&>*");
|
||||
for (let i = 0; i < sectionEls.length; i++) {
|
||||
this._draw(sectionEls[i], capsulated);
|
||||
}
|
||||
} else {
|
||||
this._draw(containerOrSection, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
src/preview-mode/post-processor/index.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export * from "./CustomAlign";
|
||||
export * from "./CustomHighlight";
|
||||
export * from "./PreviewExtendedSyntax";
|
||||
|
|
@ -1,165 +0,0 @@
|
|||
import { Format } from "src/enums";
|
||||
import { InlineFormat } from "src/types";
|
||||
import { Formats, InlineRules } from "src/format-configs/rules";
|
||||
import { SKIPPED_CLASSES, PreviewDelimLookup } from "src/preview-mode/post-processor/configs";
|
||||
|
||||
function _hasClasses(el: Element, classes: string[]): boolean {
|
||||
for (let cls of classes)
|
||||
if (el.classList.contains(cls)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function _isWhitespace(char: string): boolean {
|
||||
return char == " " || char == "\n" || char == "\t";
|
||||
}
|
||||
|
||||
export class PreviewModeParser {
|
||||
public root: Element;
|
||||
|
||||
private _walker: TreeWalker;
|
||||
private _offset = 0;
|
||||
private _curNode: Node;
|
||||
private _nodeChanged = false;
|
||||
private _stack: InlineFormat[] = [];
|
||||
private _queue: Partial<Record<InlineFormat, Range>> = {}
|
||||
private _parsingQueue: PreviewModeParser[];
|
||||
|
||||
constructor(root: Element, parsingQueue: PreviewModeParser[]) {
|
||||
this.root = root;
|
||||
this._walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT);
|
||||
this._walker.nextNode();
|
||||
this._curNode = this._walker.currentNode;
|
||||
this._parsingQueue = parsingQueue;
|
||||
}
|
||||
|
||||
public streamParse(): void {
|
||||
do {
|
||||
if (this._curNode instanceof Text) {
|
||||
this._offset = 0;
|
||||
this._parseTextNode();
|
||||
} else if (this._curNode instanceof Element) {
|
||||
if (this._isSkipped(this._curNode)) {
|
||||
this._resolve(Format.SUPERSCRIPT);
|
||||
this._resolve(Format.SUBSCRIPT);
|
||||
} else if (this._curNode.textContent) {
|
||||
this._parsingQueue.push(new PreviewModeParser(this._curNode, this._parsingQueue));
|
||||
if (/\s/.test(this._curNode.textContent)) {
|
||||
this._resolve(Format.SUPERSCRIPT);
|
||||
this._resolve(Format.SUBSCRIPT);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (this._nextNode());
|
||||
this._forceResolveAll();
|
||||
}
|
||||
|
||||
private _parseTextNode(): void {
|
||||
let str = this._curNode.textContent ?? "";
|
||||
while (!this._nodeChanged && this._offset < str.length) {
|
||||
let char = str[this._offset],
|
||||
type = PreviewDelimLookup[char];
|
||||
if (char == " " || char == "\n" || char == "\t") {
|
||||
this._resolve(Format.SUPERSCRIPT);
|
||||
this._resolve(Format.SUBSCRIPT);
|
||||
this._offset++;
|
||||
continue;
|
||||
}
|
||||
if (!type || type == Format.HIGHLIGHT) {
|
||||
this._offset++;
|
||||
continue;
|
||||
}
|
||||
this._tokenize(type);
|
||||
}
|
||||
this._nodeChanged = false;
|
||||
}
|
||||
|
||||
private _finalize(type: InlineFormat, open: Range, content: Range, close: Range): void {
|
||||
let wrapper = InlineRules[type].getEl();
|
||||
close.deleteContents();
|
||||
content.surroundContents(wrapper);
|
||||
open.deleteContents();
|
||||
if (wrapper == this._curNode.nextSibling) {
|
||||
this._nextNode();
|
||||
} else {
|
||||
this._prevNode();
|
||||
}
|
||||
this._nodeChanged = true;
|
||||
}
|
||||
|
||||
private _resolve(type: InlineFormat, close?: Range): void {
|
||||
if (close && this._queue[type]) {
|
||||
let content = new Range(),
|
||||
open = this._queue[type];
|
||||
content.setStart(open.endContainer, open.endOffset);
|
||||
content.setEnd(close.startContainer, close.startOffset);
|
||||
this._stack.findLast((t, i) => {
|
||||
delete this._queue[t];
|
||||
if (t == type) {
|
||||
this._stack.splice(i);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
this._finalize(type, open, content, close);
|
||||
} else {
|
||||
delete this._queue[type];
|
||||
this._stack = this._stack.filter(t => t != type);
|
||||
}
|
||||
}
|
||||
|
||||
private _forceResolveAll(): void {
|
||||
for (let i = 0; i < Formats.ALL_INLINE.length; i++)
|
||||
this._resolve(Formats.ALL_INLINE[i]);
|
||||
}
|
||||
|
||||
private _tokenize(type: InlineFormat): boolean {
|
||||
let { length: reqLength, char } = InlineRules[type],
|
||||
str = this._curNode.textContent!,
|
||||
length = 0,
|
||||
hasOpen = !!this._queue[type],
|
||||
hasSpaceBefore = _isWhitespace(str[this._offset - 1]),
|
||||
hasSpaceAfter: boolean;
|
||||
|
||||
while (str[this._offset] == char) { this._offset++; length++ }
|
||||
hasSpaceAfter = _isWhitespace(str[this._offset]);
|
||||
if (hasOpen && hasSpaceBefore || !hasOpen && hasSpaceAfter || length != reqLength) return false;
|
||||
|
||||
let range = new Range();
|
||||
range.setStart(this._curNode, this._offset - length);
|
||||
range.setEnd(this._curNode, this._offset);
|
||||
this._pushDelim(type, range);
|
||||
return true;
|
||||
}
|
||||
|
||||
private _pushDelim(type: InlineFormat, delim: Range): void {
|
||||
if (this._queue[type]) {
|
||||
this._resolve(type, delim);
|
||||
} else {
|
||||
this._queue[type] = delim;
|
||||
this._stack.push(type);
|
||||
}
|
||||
}
|
||||
|
||||
private _nextNode(): boolean {
|
||||
if (this._walker.nextSibling()) {
|
||||
this._curNode = this._walker.currentNode;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private _prevNode(): boolean {
|
||||
if (this._walker.previousSibling()) {
|
||||
this._curNode = this._walker.currentNode;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private _isSkipped(el: Element): boolean {
|
||||
return (
|
||||
_hasClasses(el, SKIPPED_CLASSES) ||
|
||||
el.tagName == "CODE" || el.tagName == "IMG"
|
||||
);
|
||||
}
|
||||
}
|
||||
6
src/preview-mode/utils/hasClasses.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export function hasClasses(el: Element, classes: string[]) {
|
||||
for (let cls of classes) {
|
||||
if (el.classList.contains(cls)) { return true }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
2
src/preview-mode/utils/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export * from "./hasClasses";
|
||||
export * from "./isWhitespace";
|
||||
3
src/preview-mode/utils/isWhitespace.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export function isWhitespace(char: string) {
|
||||
return char == " " || char == "\n" || char == "\t";
|
||||
}
|
||||
13
src/settings/DEFAULT_SETTINGS.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { SettingOpt1 } from "src/enums";
|
||||
import { PluginSettings } from "src/types";
|
||||
|
||||
export const DEFAULT_SETTINGS: PluginSettings = {
|
||||
insertion: SettingOpt1.ALL,
|
||||
spoiler: SettingOpt1.ALL,
|
||||
superscript: SettingOpt1.ALL,
|
||||
subscript: SettingOpt1.ALL,
|
||||
customHighlight: SettingOpt1.ALL,
|
||||
customAlign: SettingOpt1.ALL,
|
||||
editorEscape: false,
|
||||
colorButton: true
|
||||
};
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
import { getDefaultColorConfigs } from "src/utils/color-utils";
|
||||
import { DisplayBehaviour, MarkdownViewMode } from "src/enums";
|
||||
import { PluginSettings } from "src/types";
|
||||
|
||||
export const DEFAULT_SETTINGS: PluginSettings = {
|
||||
// General
|
||||
insertion: MarkdownViewMode.ALL,
|
||||
spoiler: MarkdownViewMode.ALL,
|
||||
superscript: MarkdownViewMode.ALL,
|
||||
subscript: MarkdownViewMode.ALL,
|
||||
customHighlight: MarkdownViewMode.ALL,
|
||||
customSpan: MarkdownViewMode.ALL,
|
||||
fencedDiv: MarkdownViewMode.ALL,
|
||||
|
||||
// Formatting
|
||||
tidyFormatting: true,
|
||||
openTagMenuAfterFormat: true,
|
||||
noStyledDivInSourceMode: true,
|
||||
|
||||
// Tag behavior
|
||||
hlTagDisplayBehaviour: DisplayBehaviour.SYNTAX_TOUCHED,
|
||||
spanTagDisplayBehaviour: DisplayBehaviour.SYNTAX_TOUCHED,
|
||||
showHlTagInPreviewMode: false,
|
||||
showSpanTagInPreviewMode: false,
|
||||
alwaysShowFencedDivTag: MarkdownViewMode.NONE,
|
||||
|
||||
// Custom Highlight
|
||||
colorButton: true,
|
||||
showAccentColor: true,
|
||||
showDefaultColor: true,
|
||||
showRemoveColor: true,
|
||||
lightModeHlOpacity: 0.4,
|
||||
darkModeHlOpacity: 0.5,
|
||||
get colorConfigs() { return getDefaultColorConfigs() },
|
||||
|
||||
// Custom span
|
||||
showDefaultSpanTag: true,
|
||||
showRemoveSpanTag: true,
|
||||
get predefinedSpanTag() { return [{ name: "", tag: "", showInMenu: true }] },
|
||||
|
||||
// Fenced div
|
||||
showDefaultDivTag: true,
|
||||
showRemoveDivTag: true,
|
||||
get predefinedDivTag() { return [{ name: "", tag: "", showInMenu: true }] },
|
||||
|
||||
// Others
|
||||
editorEscape: false,
|
||||
decoratePDF: true,
|
||||
}
|
||||
1
src/settings/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./DEFAULT_SETTINGS";
|
||||