So, a description will be generated from it. Even a nested span.
diff --git a/README.md b/README.md index 541acfd..97deb35 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ See PardallMarkdown in action and learn how to use it by following this video: Add dependency and application into your `mix.exs`: ```elixir defp deps do -[{:pardall_markdown, "~> 0.3.2"} ...] +[{:pardall_markdown, "~> 0.3.3"} ...] end def application do @@ -208,7 +208,7 @@ The following configuration properties are available (all optional): - `title`: the post title. If not provided, a title will be generated from the post slug. - `date`: the date or date-time to be considered for the post, string, ISO format. If not provided, the file modification date will be considered as the post date. - `published`: a post without `published: true` set will be considered draft. The default can be inverted when the configuration `:is_content_draft_by_default` is set to `false`, this way, posts will always be considered as published, unless they contain: `published: false`. -- `summary`: post description or short content. +- `summary`: post description or short content. If `summary` is not provided, a summary will be generated from the Post's content/body. - `position`: if the post topmost taxonomy has a `:sort_by` rule set to `:position`, this is the value that will be used to sort the post (see below). - `slug`: override the post slug. As seem above, by default, slugs are generated from the file names and are the main, unique identifier of posts. - If you override the slug with this property, make sure to put the full path, prepended by a slash, example: `slug: "/my/custom/slug"`. diff --git a/lib/pardall_markdown/content/html_utils.ex b/lib/pardall_markdown/content/html_utils.ex index 5a560b9..e123e4c 100644 --- a/lib/pardall_markdown/content/html_utils.ex +++ b/lib/pardall_markdown/content/html_utils.ex @@ -1,6 +1,63 @@ defmodule PardallMarkdown.Content.HtmlUtils do alias PardallMarkdown.Content.Utils + def generate_summary_from_html(html, expected_length \\ 157) + def generate_summary_from_html(html, _) when html == nil or html == "", do: nil + + @doc """ + Extract text from paragraphs `
` of a HTML `html` string, + and assemble a string up until it reaches `expected_length` length. + + If the generated string length matches `expected_length`, an ellipsis + will be appended to it. If the generated string is smaller than `expected_length`, + then no ellipsis is added. + + If no text could be extracted from the input html, returns nil. + + ## Examples + + iex> PardallMarkdown.Content.HtmlUtils.generate_summary_from_html("So, a description will be generated from it. Even a nested span.
As you can see, this a long paragraph outside.
This is an anchor.") + "So, a description will be generated from it. Even a nested span. As you can see, this a long paragraph outside." + + iex> PardallMarkdown.Content.HtmlUtils.generate_summary_from_html("So, a description will be generated from it. Even a nested span.
Another paragraph?
Another paragraph 2?
Another paragraph 3?
As you can see, this a very long paragraph. As you can see, this a very long paragraph.
So, a description will be generated from it. Even a nested span.
+Another paragraph?
+Another paragraph 2?
+Another paragraph 3?
+As you can see, this a very long paragraph. As you can see, this a very long paragraph. As you can see, this a very long paragraph. As you can see, this a very long paragraph. As you can see, this a very long paragraph. As you can see, this a very long paragraph. As you can see, this a very long paragraph. As you can see, this a very long paragraph.
+As you can see, this a paragraph outside.
+ + This is an anchor. + """ + + assert HtmlUtils.generate_summary_from_html(html) == "So, a description will be generated from it. Even a nested span. Another paragraph? Another paragraph 2? Another paragraph 3? As you can see, this a very long..." + + html = ~S""" +So, a description will be generated from it. Even a nested span.
As you can see, this a long paragraph outside.
This is an anchor. + """ + + assert HtmlUtils.generate_summary_from_html(html) == "So, a description will be generated from it. Even a nested span. As you can see, this a long paragraph outside." + + html = "Do not delete Blender's Default Cube!
" + + assert HtmlUtils.generate_summary_from_html(html) == "Do not delete Blender's Default Cube!" + end + test "make internal links as live links" do html = ~S""" This is a link to an internal post. diff --git a/test/pardall_markdown/repository_test.exs b/test/pardall_markdown/repository_test.exs index bd79dcd..2f130e9 100644 --- a/test/pardall_markdown/repository_test.exs +++ b/test/pardall_markdown/repository_test.exs @@ -5,7 +5,18 @@ defmodule PardallMarkdown.RepositoryTest do setup do Application.ensure_all_started(:pardall_markdown) # wait the Markdown content to be parsed and built - Process.sleep(100) + Process.sleep(300) + end + + @tag :post_summary + test "custom post summary and generated post summary" do + # Custom + post = Repository.get_by_slug!("/blog/dailies/first-day") + assert post.summary == "Custom post summary" + + # Generated + post = Repository.get_by_slug!("/blog/dailies/3d/blender/default-cube-not-deleted") + assert post.summary == "Do not delete the Default Cube!" end # still not accounting for per-folder indexing