Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

removed async code, refactored code, some fixes #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Alex-Dobrynin
Copy link

everything in the title, but also fixed headings, if there are some headings with the same name, so hyperlinks should increment each time

Closes #14

}

public static string RemoveSpecialCharactersExcept(this string input, char[] allowedSpecialChars)
public static bool TryCreateUri(this string path, out Uri? uri)
Copy link
Owner

Choose a reason for hiding this comment

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

I know this does almost the same thing, I even used it myself, but for readabilty I used a simple http checker just because theoreticly you can create Uri without http and we don't want that in this case.


return sb.ToString();
public static string RemoveSpecialCharactersExcept(this string input, params char[] allowedSpecialChars)
Copy link
Owner

Choose a reason for hiding this comment

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

funny that I had just removed this almost exact same code a few months ago, again for readabilty and it didn't have performance gains in this case

@@ -7,10 +8,8 @@ public static class HyperlinkHelper
{
public static bool IsHeadingLink(string hyperlink)
{
Copy link
Owner

Choose a reason for hiding this comment

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

for readabilty (and debugging) using a local var has some pros, without having any cons because the compiler will optimize it later on.

linkedHeader = markdownView
.GetRootChildren()
.OfType<HeaderLabel>()
.FirstOrDefault(x => hyperlink.Equals(x.HeadingId, StringComparison.OrdinalIgnoreCase));
Copy link
Owner

Choose a reason for hiding this comment

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

good point, will use this

</ItemGroup>

</Project>
</Project>
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for showing that somehow strange packages where included

try
{
var parent = hyperlinkControl.Parent;
IView? linkedHeader = null;

// Search for Header with HeadingId
while (linkedHeader == null
&& parent != null)
while (linkedHeader is null
Copy link
Owner

Choose a reason for hiding this comment

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

I prefere using comparison operators when comparing stuff, for readabilty, in my case it provides some extra contrast because of the difference between alphabet characters and operators symbols.

But this is really personal, no cons or pros on this one. Sorry, gona keep it this way for now.


return absolutePath;
// Fallback to path combination with normalized slashes
return string.Format("{0}/{1}",
Copy link
Owner

Choose a reason for hiding this comment

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

I never try to have a competition to get as much as possible on 1 line, lines are free and are optimized away by the compiler.

Thats why I always try to do 1 action on each line of code (if the action is not to small).
Again....for readabilty, you probably notice that I'm really keen on readabilty ;-)

Good code should read as a book, a child should be able to read an understand it....(slightly exaggerated)

@@ -18,10 +20,10 @@ public class MauiFormattedTextViewSupplier : MauiBasicViewSupplier
var textViewStyle = GetTextBlockStyleFor(textBlock);
var spanStyle = GetSpanStyleFor(textBlock);

var collectedRootViews = new List<View?>();
var textSpansCache = new List<Span>();
List<View?> collectedRootViews = [];
Copy link
Owner

Choose a reason for hiding this comment

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

This indeed improves readabilty +1

.RemoveSpecialCharactersExcept('-')
.ToLower();

if (Headings.TryGetValue(headingId, out var list))
Copy link
Owner

Choose a reason for hiding this comment

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

This is a solution for something thats not really an issue. When someone uses same headers for multiple sections and it links to it afterwards, strange by the way, he should be able to imagine that the system will not know which one was intended. Because the system won't crash or five any other error this won't be a real issue, only bad text / markdown writing.

With including a number you are creating dynamic magical strings, what will never be linked to by the markdown writer as well.

@@ -140,15 +140,22 @@ public virtual void OnReferenceDefinitionsPublished(IEnumerable<MarkdownReferenc
Style = Styles?.ImageSubscriptionViewStyle,
Text = subscription
};
var stackLayout = new VerticalStackLayout
{
Style = Styles?.ImageLayoutViewStyle,
Copy link
Owner

Choose a reason for hiding this comment

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

I see what you are trying to do here, but the ImageLayoutViewStyle was also intended as an extra layer for the developer/designer to style optionaly because images can be hard to style or position sometimes.
Thats why I desided to always include a ImageLayoutView and give it a style, regardless of whether there is 1 or more children.

@@ -255,7 +257,7 @@ protected virtual async void OnHyperlinkTappedAsync(HyperlinkSpan hyperlinkSpan)

protected virtual View? TryCreateFormattedLabel(List<Span> textSpans, Style? style)
{
if (!textSpans.Any())
if (textSpans.Count == 0)
Copy link
Owner

Choose a reason for hiding this comment

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

fyi: Any is faster than Count, but almost negligible....so the only reason why I use Any is for Readability preference


switch (validViews.Count)
{
case 0:
return null;
case 1:
return validViews.First();
return validViews[0];
Copy link
Owner

Choose a reason for hiding this comment

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

this won't be faster when using lists, because lists uses indexers etc with First().
When using [0] approuch you probably are better of with ToArray() on the top, than it will be faster in the end.

but in the end the difference won't be noticeable.....just fyi ;-)

{
return url;
}

var isAbsolutePath = Uri.TryCreate(url, UriKind.Absolute, out _);
if (isAbsolutePath)
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
Copy link
Owner

Choose a reason for hiding this comment

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

I have used this previously, but it filtered more than desired....in this case

private CancellationTokenSource? _loadingCts;
protected readonly ILogger<MarkdownView>? Logger;
protected readonly VerticalStackLayout Container;
protected CancellationTokenSource? LoadingCts;
Copy link
Owner

Choose a reason for hiding this comment

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

most likely everything is handled on the UI thread with your approuch, so this token will never be used

foreach (var view in views)
{
loadingToken.ThrowIfCancellationRequested();
_layout.Add(view);
if (token.IsCancellationRequested) return;
Copy link
Owner

Choose a reason for hiding this comment

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

ThrowIfCancellationRequested has some pros and is designed to work with the Task and Cancelation system, I recommend using that. But this is not used in a Task, so it matters less and will probably never be used anyway.

It is good to remember that when a Method is finished without errors, like a return does, you expect it to have gone well. This way you don't have an indication that something has caused a cancellation.

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

Successfully merging this pull request may close these issues.

Remove async code in MarkDownView.cs
2 participants