Skip to content

Commit

Permalink
Push Markdown link refs to excerpt only as required (jekyll#7577)
Browse files Browse the repository at this point in the history
Merge pull request 7577
  • Loading branch information
ashmaroli authored and jekyllbot committed Mar 18, 2019
1 parent 3e2c8fd commit 9e137ba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
24 changes: 24 additions & 0 deletions features/post_excerpts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,27 @@ Feature: Post excerpts
And I should see "Liquid is not rendered at {{ page.url }}" in "_site/2017/07/06/unrendered-post.html"
But I should see "<p>Liquid is rendered at /2017/07/06/rendered-post.html</p>" in "_site/2017/07/06/rendered-post.html"
And I should see "<p>Liquid is not rendered at {{ page.url }}</p>\n<p>Liquid is rendered at /2017/07/06/rendered-post.html</p>" in "_site/index.html"

Scenario: Excerpts from posts with reference-style Markdown links
Given I have a configuration file with:
| key | value |
| permalink | "/:title:output_ext" |
| kramdown | { show_warnings: true } |
And I have an "index.html" page that contains "{% for post in site.posts %}{{ post.excerpt }}{% endfor %}"
And I have a _layouts directory
And I have a post layout that contains "{{ page.excerpt }}"
And I have a _posts directory
And I have the following posts:
| title | layout | date | content |
| Just Text Excerpt | post | 2019-03-06 | Install Jekyll\n\nNext Para [^1]\n\n[^1]: Lorem ipsum |
| Text and Footnote | post | 2019-03-07 | Alpha [^1]\n\nNext Para\n\n[^1]: Omega sigma |
| Text and Reference Link | post | 2019-03-08 | Read [docs][link]\n\nNext Para\n\n[link]: docs.jekyll.com |
| Text and Self-refencing Link | post | 2019-03-09 | Check out [jekyll]\n\nNext Para\n\n[jekyll]: jekyllrb.com |
When I run jekyll build
Then I should get a zero exit status
And I should not see "Kramdown warning" in the build output
But I should see exactly "<p>Install Jekyll</p>" in "_site/just-text-excerpt.html"
And I should see "<p>Alpha <sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote\">1</a></sup></p>" in "_site/text-and-footnote.html"
And I should see "<p>Omega sigma <a href=\"#fnref:1\" class=\"reversefootnote\">&#8617;</a></p>" in "_site/text-and-footnote.html"
And I should see "<p>Read <a href=\"docs.jekyll.com\">docs</a></p>" in "_site/text-and-reference-link.html"
And I should see "<p>Check out <a href=\"jekyllrb.com\">jekyll</a></p>" in "_site/text-and-self-refencing-link.html"
51 changes: 32 additions & 19 deletions lib/jekyll/excerpt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,47 @@ def render_with_liquid?
# Returns excerpt String

LIQUID_TAG_REGEX = %r!{%-?\s*(\w+)\s*.*?-?%}!m.freeze
MKDWN_LINK_REF_REGEX = %r!^ {0,3}\[[^\]]+\]:.+$!.freeze
MKDWN_LINK_REF_REGEX = %r!^ {0,3}(?:(\[[^\]]+\])(:.+))$!.freeze

def extract_excerpt(doc_content)
head, _, tail = doc_content.to_s.partition(doc.excerpt_separator)

# append appropriate closing tag(s) (for each Liquid block), to the `head` if the
# partitioning resulted in leaving the closing tag somewhere in the `tail` partition.
if head.include?("{%")
modified = false
tag_names = head.scan(LIQUID_TAG_REGEX)
tag_names.flatten!
tag_names.reverse_each do |tag_name|
next unless liquid_block?(tag_name)
next if head =~ endtag_regex_stash(tag_name)

modified = true
head << "\n{% end#{tag_name} %}"
end
print_build_warning if modified
end

return head if tail.empty?

head << "\n\n" << tail.scan(MKDWN_LINK_REF_REGEX).join("\n")
head = sanctify_liquid_tags(head) if head.include?("{%")
definitions = extract_markdown_link_reference_defintions(head, tail)
return head if definitions.empty?

head << "\n\n" << definitions.join("\n")
end

private

# append appropriate closing tag(s) (for each Liquid block), to the `head` if the
# partitioning resulted in leaving the closing tag somewhere in the `tail` partition.
def sanctify_liquid_tags(head)
modified = false
tag_names = head.scan(LIQUID_TAG_REGEX)
tag_names.flatten!
tag_names.reverse_each do |tag_name|
next unless liquid_block?(tag_name)
next if head =~ endtag_regex_stash(tag_name)

modified = true
head << "\n{% end#{tag_name} %}"
end

print_build_warning if modified
head
end

def extract_markdown_link_reference_defintions(head, tail)
[].tap do |definitions|
tail.scan(MKDWN_LINK_REF_REGEX).each do |segments|
definitions << segments.join if head.include?(segments[0])
end
end
end

def endtag_regex_stash(tag_name)
@endtag_regex_stash ||= {}
@endtag_regex_stash[tag_name] ||= %r!{%-?\s*end#{tag_name}.*?\s*-?%}!m
Expand Down

0 comments on commit 9e137ba

Please sign in to comment.