-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: rewrite getAndRemoveConfig(str) function #2472
Conversation
Reactor getAndRemoveConfig function to a generic lexer instead of a complex regex. Correctly the behavior and only resolve valid configs. Warning invalid configs also.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@Koooooo-7 --
|
Thanks very much @Koooooo-7 for this! I've put my original CodeSandbox example online with your Docsify Preview build and @jhildenbiddle examples to help with testing - looking good so far 🙂 https://paulhibbitts.github.io/docsify-v5-preview/#/test-3 |
It will be resolve to
Cos the
I see, will refactor to retrieve the closest pre/next
Gotcha. Will update to clean the whole |
Hi @jhildenbiddle ---
And I add more test cases and comments on those cases we discussed. |
Thanks so much @Koooooo-7 , this is going to be a really nice improvement 🙂 I've updated my online Docsify Preview build with your latest Preview and things look good! https://paulhibbitts.github.io/docsify-v5-preview/#/test-3 |
Thx for you live preview for the changes !!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion on this:
- How does this handle multiple attributes when multiple values are provided?
[Text](page.md ':target=_blank :class=foo bar') [Text](page.md ':class=foo bar :target=_blank ')
It will be resolve to
{ target: "_blank", class: foo, class_appened_props: "bar" }
Instead of capturing multiple values using class
and class_appended_props
properties, why not require multiple values to be wrapped in quotes? This seems more natural since it is how attributes and values work in HTML. It will also make it easier to read multiple attribute values because all values are contained in a single property.
For example:
[Text](page.md ':foo :target=_blank :class="bar baz" This is the title')
{
foo: true,
target: "_blank",
class: "bar baz",
title: "This is the title" // links support title attribute in markdown
}
The rules would then be:
- All attributes must start with
:
- Attributes without values are valid (
:foo
) - Quotes are optional for single values (
:target=_blank
or:target="_blank"
) - Quotes are required for multiple attribute values (
:class="bar baz"'
)
The question is then if/how we want to handle stray, unquoted values. Consider the following example:
[Text](page.md ':foo=val1 val2 :bar=val3 val4'). // Missing quotes around multiple values
I would expect this to produce the following:
{
foo: "val1",
bar: "val3",
title: "val4",
}
What happens to val2
?
The easiest thing to do would be to just drop val2
because according to the rules this is neither an attribute (does not start with :
) or an attribute value.
Thoughts?
That would be more clearly. but it brings a breaking change for all the config rules for now. So I choose a compromise way to make it and doesn't breaking anything to get ride of the regex. Besides, the config of docsify is not that consistent.
It is hard to say a data attribute nor a docsify config within a string. If we decided include a breaking change on this. I think we can introduce a docsify config block. e.g.
Then, we have a clear line to distinguish between the docsify part and custom made for markdown owned. |
Makes sense. I didn't realize that the attribute parser is inconsistent across tags. I appreciate trying to not introduce a non-breaking change, but perhaps this is the opportunity make attribute parsing consistent in docsify for all markdown tags that support it. See below...
Can we add something like the proposed "block" of attributes above as a new v5 feature but retain the existing For example, we can drop the [Text](page.md ':[foo target="_blank" class="bar baz"] This is the title') Alternatively, we could use single or double brackets to start/end the pattern which would make it easy for us to parse using [Text](page.md '{foo: true, target: "_blank" class: "bar baz"} This is the title') [Text](page.md '{{foo: true, target: "_blank" class: "bar baz"}} This is the title') If we go this route, I would propose we not allow mixing new and old attribute styles like this: [Text](page.md '{{foo: true, class: "bar baz"}} :target=_blank This is the title') Doing this promotes continued use of the old This seems like a better approach than updating the |
--- @jhildenbiddle
Yes, there is a Based on this refactor doesn't breaking any current behavior, let's think about the new config functions.
First of all, I agree the notes way to reminder users to update its configs if needs in v5 and future, give user a buffer to know the changes. ✅ About how to do the redesign about the On your ideas. A:
It is okay,
There contains a extra quote validation between the markdown config block and the "values group". Although we have to face it currently, I suppose we'd better drop it if we could.
B:
Personally, I think this is a good idea. what we need we is getting the |
I think both styles have pros and cons:
Here is some additional feedback on each style: Colon Style
As you mention, this is something we already have to handle today. All of the following examples are valid regardless of how we handle attributes: [Text](page.md 'This is the title')
[Text](page.md "This is the title")
[Text](page.md "This is the title with 'single quotes' and a \"double quotes\"") Even if we went with "bracket style", we'd still have to handle single vs. double quotes: [Text](page.md '{foo: "bar"} This is the title')
[Text](page.md "{foo: 'bar'} This is the title")
[Text](page.md "{foo: 'bar', baz: \"buzz\"} This is the title with 'single quotes' and a \"double quotes\"")
This format feels like we're forcing users to work around how our parser works instead of designing our parser to work the way that makes the most sense to users. The closer we can get to the familiar `[Text](page.md ':[foo target="_blank" class="bar baz"] This is the title')` Bracket Style
My understanding is that single brackets ( [Text](page.md '{{foo: true, target: "_blank" class: "bar baz"}} This is the title') Final ThoughtsI am open to adding either colon style or bracket style as a new v5 feature, however I do not think we should promote the ability to specify multiple value with the previous If we go this route, do the changes in this PR (regex => lexer) still make sense? If so, we can keep them. If not, then let's focus on getting the new-and-improved format in place instead of "improving" code which we hope to remove in the next release. |
Thanks so much @Koooooo-7 @jhildenbiddle for working this out some more. My feeling are also mixed re: colon or brackets style... the use of [] is already in Markdown but that also means they are now used in multiple ways in a single line of Markdown which might be less than clear and not expected. To confirm, if this change went ahead existing single class assignments would still work - correct? |
Correct. My proposal is that keep the current behavior and add new functionality via a new attribute/value format. |
BTW, after coming across other examples of Markdown dialect classes (https://commonmark.thephpleague.com/2.4/extensions/attributes/ and https://masteringlaravel.io/daily/2023-10-05-applying-a-class-to-a-markdown-link) I am now tending even more to the use of {}. These examples were new to me 🙂 |
--- @jhildenbiddle
I think the
If we decide to replace it as a brand-new configs styles for users, I think this |
close via #2476 2476 |
Oh thanks for the additional info @jhildenbiddle , I was not aware of that factor in regards to the use of single or double brackets! ps - as a follow up test, it looks like |
Yes, cos we haven't support it yet. |
Correct. The markdown standard states that anything contained within the quotes will be included as part of the title attribute. It won’t matter which format we choose. As long as out “configuration block” is contained within the quotes, it will be included as part of the title value when the markdown is viewed in an environment other than docsify (like GitHub). |
Summary
Reactor
getAndRemoveConfig(str)
function to a generic Lexer instead of a complex regex.Correct current behavior and indicate docsify needs to resolve valid config keys. Warning invalid configs also.
Remove to use a tricky returned
title
as config options.Instead, new
KEY_appened_props
introduced.It will let the config parse as
:KEY=VALUE append_props...
, each config processor can handle its own logicfor the
Value
andappend_props
. e.g.Which can support
multi values
configs of one key such as #2471 .Related issue, if any:
What kind of change does this PR introduce?
Refactor
For any code change,
Does this PR introduce a breaking change?
Yes
No
Tested in the following browsers: