mirror of
https://github.com/Nioux/AideDeJeu.git
synced 2025-12-16 15:19:56 +00:00
Refonte ids version longue
This commit is contained in:
parent
4e68d6e962
commit
562879993b
2 changed files with 72 additions and 55 deletions
|
|
@ -52,28 +52,25 @@ namespace AideDeJeu.ViewModels
|
||||||
List<SearchedItem> primaryItems = new List<SearchedItem>();
|
List<SearchedItem> primaryItems = new List<SearchedItem>();
|
||||||
List<SearchedItem> secondaryItems = new List<SearchedItem>();
|
List<SearchedItem> secondaryItems = new List<SearchedItem>();
|
||||||
var cleanSearchText = Tools.Helpers.RemoveDiacritics(searchText).ToLower();
|
var cleanSearchText = Tools.Helpers.RemoveDiacritics(searchText).ToLower();
|
||||||
foreach (var allItem in Store._AllItems)
|
foreach (var item in Store._AllItems)
|
||||||
{
|
{
|
||||||
foreach (var item in allItem.Value.Anchors)
|
var name = item.Value.Name;
|
||||||
|
var cleanName = Tools.Helpers.RemoveDiacritics(name).ToLower();
|
||||||
|
if (cleanName.Contains(cleanSearchText))
|
||||||
{
|
{
|
||||||
var name = item.Value.Name;
|
primaryItems.Add(new SearchedItem() { Item = item.Value, Preview = name });
|
||||||
var cleanName = Tools.Helpers.RemoveDiacritics(name).ToLower();
|
}
|
||||||
if (cleanName.Contains(cleanSearchText))
|
else
|
||||||
|
{
|
||||||
|
var markdown = item.Value.Markdown;
|
||||||
|
var cleanMarkdown = Tools.Helpers.RemoveDiacritics(markdown).ToLower();
|
||||||
|
if (cleanMarkdown.Contains(cleanSearchText))
|
||||||
{
|
{
|
||||||
primaryItems.Add(new SearchedItem() { Item = item.Value, Preview = name });
|
int position = cleanMarkdown.IndexOf(cleanSearchText);
|
||||||
}
|
int startPosition = Math.Max(0, position - 30);
|
||||||
else
|
int endPosition = Math.Min(markdown.Length, position + searchText.Length + 30);
|
||||||
{
|
var preview = markdown.Substring(startPosition, endPosition - startPosition - 1);
|
||||||
var markdown = item.Value.Markdown;
|
secondaryItems.Add(new SearchedItem() { Item = item.Value, Preview = preview });
|
||||||
var cleanMarkdown = Tools.Helpers.RemoveDiacritics(markdown).ToLower();
|
|
||||||
if (cleanMarkdown.Contains(cleanSearchText))
|
|
||||||
{
|
|
||||||
int position = cleanMarkdown.IndexOf(cleanSearchText);
|
|
||||||
int startPosition = Math.Max(0, position - 30);
|
|
||||||
int endPosition = Math.Min(markdown.Length, position + searchText.Length + 30);
|
|
||||||
var preview = markdown.Substring(startPosition, endPosition - startPosition - 1);
|
|
||||||
secondaryItems.Add(new SearchedItem() { Item = item.Value, Preview = preview });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ namespace AideDeJeu.ViewModels
|
||||||
{
|
{
|
||||||
public class StoreViewModel : BaseViewModel
|
public class StoreViewModel : BaseViewModel
|
||||||
{
|
{
|
||||||
public static Item ToItem(string md)
|
public Item ToItem(string source, string md)
|
||||||
{
|
{
|
||||||
var pipeline = new MarkdownPipelineBuilder().UsePipeTables().Build();
|
var pipeline = new MarkdownPipelineBuilder().UsePipeTables().Build();
|
||||||
var document = MarkdownParser.Parse(md, pipeline);
|
var document = MarkdownParser.Parse(md, pipeline);
|
||||||
|
|
@ -33,7 +33,7 @@ namespace AideDeJeu.ViewModels
|
||||||
{
|
{
|
||||||
if (IsNewItem(block))
|
if (IsNewItem(block))
|
||||||
{
|
{
|
||||||
var item = ParseItem(ref enumerator);
|
var item = ParseItem(source, ref enumerator);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +48,7 @@ namespace AideDeJeu.ViewModels
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item ParseItem(ref ContainerBlock.Enumerator enumerator)
|
public Item ParseItem(string source, ref ContainerBlock.Enumerator enumerator)
|
||||||
{
|
{
|
||||||
var currentItem = GetNewItem(enumerator.Current);
|
var currentItem = GetNewItem(enumerator.Current);
|
||||||
|
|
||||||
|
|
@ -63,11 +63,13 @@ namespace AideDeJeu.ViewModels
|
||||||
{
|
{
|
||||||
if (IsClosingItem(block))
|
if (IsClosingItem(block))
|
||||||
{
|
{
|
||||||
|
currentItem.Id = GetNewAnchorId(source, currentItem.Name);
|
||||||
|
_AllItems[currentItem.Id] = currentItem;
|
||||||
return currentItem;
|
return currentItem;
|
||||||
}
|
}
|
||||||
else if (IsNewItem(block))
|
else if (IsNewItem(block))
|
||||||
{
|
{
|
||||||
var subItem = ParseItem(ref enumerator);
|
var subItem = ParseItem(source, ref enumerator);
|
||||||
|
|
||||||
var propertyName = subItem.GetType().Name;
|
var propertyName = subItem.GetType().Name;
|
||||||
|
|
||||||
|
|
@ -89,7 +91,7 @@ namespace AideDeJeu.ViewModels
|
||||||
|
|
||||||
else // if (block is ContainerBlock)
|
else // if (block is ContainerBlock)
|
||||||
{
|
{
|
||||||
ParseItemProperties(currentItem, block);
|
ParseItemProperties(source, currentItem, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentItem.Markdown += enumerator.Current.ToMarkdownString();
|
currentItem.Markdown += enumerator.Current.ToMarkdownString();
|
||||||
|
|
@ -98,34 +100,36 @@ namespace AideDeJeu.ViewModels
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentItem.Id = GetNewAnchorId(source, currentItem.Name);
|
||||||
|
_AllItems[currentItem.Id] = currentItem;
|
||||||
return currentItem;
|
return currentItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ParseItemProperties(Item item, Block block)
|
public void ParseItemProperties(string source, Item item, Block block)
|
||||||
{
|
{
|
||||||
switch (block)
|
switch (block)
|
||||||
{
|
{
|
||||||
case Markdig.Extensions.Tables.Table table:
|
case Markdig.Extensions.Tables.Table table:
|
||||||
ParseItemProperties(item, table);
|
ParseItemProperties(source, item, table);
|
||||||
break;
|
break;
|
||||||
case ContainerBlock blocks:
|
case ContainerBlock blocks:
|
||||||
ParseItemProperties(item, blocks);
|
ParseItemProperties(source, item, blocks);
|
||||||
break;
|
break;
|
||||||
case LeafBlock leaf:
|
case LeafBlock leaf:
|
||||||
ParseItemProperties(item, leaf.Inline);
|
ParseItemProperties(source, item, leaf.Inline);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ParseItemProperties(Item item, ContainerBlock blocks)
|
public void ParseItemProperties(string source, Item item, ContainerBlock blocks)
|
||||||
{
|
{
|
||||||
foreach (var block in blocks)
|
foreach (var block in blocks)
|
||||||
{
|
{
|
||||||
ParseItemProperties(item, block);
|
ParseItemProperties(source, item, block);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ParseItemProperties(Item item, ContainerInline inlines)
|
public void ParseItemProperties(string source, Item item, ContainerInline inlines)
|
||||||
{
|
{
|
||||||
if (inlines == null)
|
if (inlines == null)
|
||||||
{
|
{
|
||||||
|
|
@ -163,7 +167,7 @@ namespace AideDeJeu.ViewModels
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static bool IsNewItem(Block block)
|
public bool IsNewItem(Block block)
|
||||||
{
|
{
|
||||||
var htmlBlock = block as HtmlBlock;
|
var htmlBlock = block as HtmlBlock;
|
||||||
if (htmlBlock.Type == HtmlBlockType.Comment)
|
if (htmlBlock.Type == HtmlBlockType.Comment)
|
||||||
|
|
@ -180,7 +184,7 @@ namespace AideDeJeu.ViewModels
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsClosingItem(Block block)
|
public bool IsClosingItem(Block block)
|
||||||
{
|
{
|
||||||
var htmlBlock = block as HtmlBlock;
|
var htmlBlock = block as HtmlBlock;
|
||||||
if (htmlBlock.Type == HtmlBlockType.Comment)
|
if (htmlBlock.Type == HtmlBlockType.Comment)
|
||||||
|
|
@ -197,7 +201,7 @@ namespace AideDeJeu.ViewModels
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item GetNewItem(Block block)
|
public Item GetNewItem(Block block)
|
||||||
{
|
{
|
||||||
var htmlBlock = block as HtmlBlock;
|
var htmlBlock = block as HtmlBlock;
|
||||||
if (htmlBlock.Type == HtmlBlockType.Comment)
|
if (htmlBlock.Type == HtmlBlockType.Comment)
|
||||||
|
|
@ -221,14 +225,29 @@ namespace AideDeJeu.ViewModels
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public string GetNewAnchorId(string source, string name)
|
||||||
|
{
|
||||||
|
var baseid = Helpers.IdFromName(name);
|
||||||
|
var id = $"{source}.md#{baseid}";
|
||||||
|
int index = 0;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (!_AllItems.ContainsKey(name))
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
name = $"{source}.md#{baseid}{index}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
void AddAnchor(string source, Dictionary<string, Item> anchors, Item item)
|
void AddAnchor(string source, Dictionary<string, Item> anchors, Item item)
|
||||||
{
|
{
|
||||||
if (item != null && item.Name != null)
|
if (item != null && item.Name != null)
|
||||||
{
|
{
|
||||||
var basename = Helpers.IdFromName(item.Name);
|
var basename = Helpers.IdFromName(item.Name);
|
||||||
//var name = $"{source}.md#{basename}";
|
var name = $"{source}.md#{basename}";
|
||||||
var name = $"{basename}";
|
//var name = $"{basename}";
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|
@ -239,8 +258,8 @@ namespace AideDeJeu.ViewModels
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
//name = $"{source}.md#{basename}{index}";
|
name = $"{source}.md#{basename}{index}";
|
||||||
name = $"{basename}{index}";
|
//name = $"{basename}{index}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -261,8 +280,8 @@ namespace AideDeJeu.ViewModels
|
||||||
public Item Item { get; set; }
|
public Item Item { get; set; }
|
||||||
public Dictionary<string, Item> Anchors { get; set; } = new Dictionary<string, Item>();
|
public Dictionary<string, Item> Anchors { get; set; } = new Dictionary<string, Item>();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
public Dictionary<string, ItemWithAnchors> _AllItems = new Dictionary<string, ItemWithAnchors>();
|
public Dictionary<string, Item> _AllItems = new Dictionary<string, Item>();
|
||||||
|
|
||||||
public async Task PreloadAllItemsAsync()
|
public async Task PreloadAllItemsAsync()
|
||||||
{
|
{
|
||||||
|
|
@ -278,12 +297,12 @@ namespace AideDeJeu.ViewModels
|
||||||
var md = await Tools.Helpers.GetResourceStringAsync(resourceName);
|
var md = await Tools.Helpers.GetResourceStringAsync(resourceName);
|
||||||
if (md != null)
|
if (md != null)
|
||||||
{
|
{
|
||||||
var item = ToItem(md);
|
var item = ToItem(source, md);
|
||||||
if (item != null)
|
if (item != null)
|
||||||
{
|
{
|
||||||
var anchors = new Dictionary<string, Item>();
|
var anchors = new Dictionary<string, Item>();
|
||||||
MakeAnchors(source, anchors, item);
|
//MakeAnchors(source, anchors, item);
|
||||||
_AllItems[source] = new ItemWithAnchors() { Item = item, Anchors = anchors };
|
_AllItems[source] = item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -293,19 +312,20 @@ namespace AideDeJeu.ViewModels
|
||||||
|
|
||||||
public async Task<Item> GetItemFromDataAsync(string source, string anchor)
|
public async Task<Item> GetItemFromDataAsync(string source, string anchor)
|
||||||
{
|
{
|
||||||
|
var id = $"{source}.md#{anchor}";
|
||||||
//await Task.Delay(3000);
|
//await Task.Delay(3000);
|
||||||
if (!_AllItems.ContainsKey(source))
|
if (!_AllItems.ContainsKey(id))
|
||||||
{
|
{
|
||||||
//var md = await Tools.Helpers.GetStringFromUrl($"https://raw.githubusercontent.com/Nioux/AideDeJeu/master/Data/{source}.md");
|
//var md = await Tools.Helpers.GetStringFromUrl($"https://raw.githubusercontent.com/Nioux/AideDeJeu/master/Data/{source}.md");
|
||||||
var md = await Tools.Helpers.GetResourceStringAsync($"AideDeJeu.Data.{source}.md");
|
var md = await Tools.Helpers.GetResourceStringAsync($"AideDeJeu.Data.{source}.md");
|
||||||
if (md != null)
|
if (md != null)
|
||||||
{
|
{
|
||||||
var item = ToItem(md);
|
var item = ToItem(source, md);
|
||||||
if (item != null)
|
if (item != null)
|
||||||
{
|
{
|
||||||
var anchors = new Dictionary<string, Item>();
|
var anchors = new Dictionary<string, Item>();
|
||||||
MakeAnchors(source, anchors, item);
|
//MakeAnchors(source, anchors, item);
|
||||||
_AllItems[source] = new ItemWithAnchors() { Item = item, Anchors = anchors };
|
_AllItems[source] = item;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -317,15 +337,15 @@ namespace AideDeJeu.ViewModels
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var itemWithAnchors = _AllItems[source];
|
if (_AllItems.ContainsKey(id))
|
||||||
if (!string.IsNullOrEmpty(anchor))
|
|
||||||
{
|
{
|
||||||
if (itemWithAnchors.Anchors.ContainsKey(anchor))
|
return _AllItems[id];
|
||||||
{
|
|
||||||
return itemWithAnchors.Anchors[anchor];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return itemWithAnchors.Item;
|
else if (_AllItems.ContainsKey(source))
|
||||||
|
{
|
||||||
|
return _AllItems[source];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue