예제 #1
0
        /// <summary>
        /// Central routine of the folding strategy for markdown.
        /// It uses the parsed syntax tree in <paramref name="markdownDocument"/> to calculate all folding positions.
        /// </summary>
        /// <param name="markdownDocument">The parsed syntax tree of the markdown document.</param>
        /// <returns>Enumeration of foldings.</returns>
        public virtual IEnumerable <NewFolding> GetNewFoldings(Markdig.Syntax.MarkdownDocument markdownDocument)
        {
            const int maxLevel       = 4;
            var       newFoldMarkers = new List <NewFolding>();

            if (markdownDocument != null)
            {
                // since we fold only the h1, h2, h3, h3 tags, they all are top level blocks
                // thus there is no need to go down in the tree

                var previousBlocks = new Markdig.Syntax.HeadingBlock[maxLevel + 1]; // +1 because heading.Level is one-based

                for (int i = 0; i < markdownDocument.Count; ++i)
                {
                    if (markdownDocument[i] is Markdig.Syntax.HeadingBlock heading && heading.Level <= maxLevel)
                    {
                        // A heading block will close all previous heading with the same level or any sublevel above
                        for (int j = heading.Level; j <= maxLevel; ++j)
                        {
                            if (null != previousBlocks[j])
                            {
                                newFoldMarkers.Add(new NewFolding(previousBlocks[j].Span.End + 1, markdownDocument[i - 1].Span.End + 1));
                                previousBlocks[j] = null; // the previous block is processed then
                            }
                        }
                        previousBlocks[heading.Level] = heading;
                    }
                }

                // after all blocks have been processed, we have to close the unfinished blocks
                var end = markdownDocument.Span.End;
                for (int i = 0; i < previousBlocks.Length; ++i)
                {
                    if (null != previousBlocks[i])
                    {
                        newFoldMarkers.Add(new NewFolding(previousBlocks[i].Span.End + 1, markdownDocument.Span.End + 1));
                    }
                }
            }

            return(newFoldMarkers.OrderBy(f => f.StartOffset));
        }
예제 #2
0
        public async Task <DocumentCacheResult> TryCache()
        {
            if (markdown != null)
            {
                return(DocumentCacheResult.Success);
            }

            while (readingCache)
            {
                await Task.Delay(5);
            }

            if (markdown != null)
            {
                return(DocumentCacheResult.Success);
            }

            readingCache = true;
            try
            {
                using (var scope = _Services.CreateScope())
                {
                    var httpClient       = scope.ServiceProvider.GetRequiredService <HttpClient>();
                    var markdownPipeline = scope.ServiceProvider.GetRequiredService <MarkdownPipeline>();
                    markdown = Markdig.Markdown.Parse(await httpClient.GetStringAsync(Path), markdownPipeline);
                }
            } catch (HttpRequestException)
            {
                return(DocumentCacheResult.Offline);
            } catch
            {
                return(DocumentCacheResult.Error);
            } finally
            {
                readingCache = false;
            }

            return(DocumentCacheResult.Success);
        }
예제 #3
0
 internal PreviewRenderedEventArgs(Markdig.Syntax.MarkdownDocument document) : base()
 {
     this.Document = document;
 }