Skip to content

Commit 66a7917

Browse files
authored
support author with URL in markdown format (#86)
1 parent a2d87a8 commit 66a7917

File tree

8 files changed

+59
-8
lines changed

8 files changed

+59
-8
lines changed

docs/quickstart/usage_example/basics/configure_card.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Configure your card
33
cover: assets/logo.svg
44
id: configure_your_card
5-
author: Johnny Chen
5+
author: "[Johnny Chen](https://github.com/johnnychen94); Jane Doe"
66
date: 2020-09-13
77
description: This demo show you how to pass additional meta info of card to DemoCards.jl
88
---
@@ -44,13 +44,20 @@ For example, the markdown file of this page uses the following frontmatter:
4444
title: Configure your card
4545
cover: assets/logo.svg
4646
id: configure_your_card
47-
author: Johnny Chen
47+
author: "[Johnny Chen](https://github.com/johnnychen94); Jane Doe"
4848
date: 2020-09-13
4949
description: This demo show you how to pass additional meta info of card to DemoCards.jl
5050
---
51-
5251
```
5352

5453
As you can see, if configured, there will be badges for `author` and `date` info. If there are
5554
multiple authors, they could be splitted by semicolon `;`. For example, `author: Jane Doe; John Roe`
5655
would generate two author badges.
56+
57+
!!! tip
58+
If `author` is configured as markdown url format, then the generated badge will be clickable.
59+
60+
!!! warning
61+
A badly formatted YAML frontmatter will currently trigger a build failure with perhaps hard to
62+
understand error. Sometimes, you need to assist YAML parser by explicitly quoting the content
63+
with `""`. See the author field above as an instance.

docs/quickstart/usage_example/julia_demos/1.julia_demo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# id: juliademocard_example
88
# cover: assets/literate.png
99
# date: 2020-09-13
10-
# author: Johnny Chen
10+
# author: "[Johnny Chen](https://github.com/johnnychen94)"
1111
# julia: 1.0.1
1212
# description: This demo shows you how to write your demo in julia
1313
# ---

src/preview.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function generate_or_copy_pagedir(src_path, build_dir)
162162
page_dir = src_path
163163
cp(page_dir, abspath(build_dir, basename(page_dir)); force=true)
164164
else
165-
throw(ArgumentError("failed to parse demo page structure from path: $src_path"))
165+
throw(ArgumentError("failed to parse demo page structure from path: $src_path. There might be some invalid demo files."))
166166
end
167167

168168
return page_dir

src/types/card.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,19 @@ function make_badges(card::AbstractDemoCard)
9090
for author in split(card.author, ';')
9191
# TODO: also split on "and"
9292
isempty(author) && continue
93-
author_str = HTTP.escapeuri(strip(author))
94-
push!(badges, "![Author](https://img.shields.io/badge/Author-$(author_str)-blue)")
93+
94+
m = match(regex_md_url, author)
95+
if isnothing(m)
96+
author_str = HTTP.escapeuri(strip(author))
97+
push!(badges, "![Author](https://img.shields.io/badge/Author-$(author_str)-blue)")
98+
else
99+
# when markdown url is detected, create a link for it
100+
# author: [Johnny Chen](https://github.com/johnnychen94)
101+
name, url = strip.(m.captures)
102+
name = HTTP.escapeuri(name)
103+
badge_str = "[![Author](https://img.shields.io/badge/Author-$(name)-blue)]($url)"
104+
push!(badges, badge_str)
105+
end
95106
end
96107
end
97108
if card.date != DateTime(0)

src/utils.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,14 @@ function parse(T::Val, card::AbstractDemoCard)
193193
config = parse(T, body)
194194
# frontmatter has higher priority
195195
if !isempty(frontmatter)
196-
merge!(config, YAML.load(join(frontmatter, "\n")))
196+
yaml_config = try
197+
YAML.load(join(frontmatter, "\n"))
198+
catch err
199+
@warn "failed to parse YAML frontmatter, please double check the format"
200+
println(stderr, frontmatter)
201+
rethrow(err)
202+
end
203+
merge!(config, yaml_config)
197204
end
198205

199206
return config
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ---
2+
# author: "[Johnny Chen](https://github.com/johnnychen94); Jane Doe"
3+
# ---
4+
5+
# author with markdown url
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
author: "[Johnny Chen](https://github.com/johnnychen94); Jane Doe"
3+
---
4+
5+
author with markdown url

test/generate.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,20 @@
3737
@testset "throw_error" begin
3838
@suppress_err @test_throws LoadError preview_demos(joinpath(root, "broken_demo.jl"); throw_error=true)
3939
end
40+
41+
@testset "badges" begin
42+
@testset "author" begin
43+
for (dir, file) in [
44+
("julia", joinpath(root, "card", "julia", "author_with_md_url.jl")),
45+
("markdown", joinpath(root, "card", "markdown", "author_with_md_url.md"))
46+
]
47+
page_dir = @suppress_err preview_demos(file; theme="grid", require_html=false)
48+
md_file = joinpath(page_dir, dir, "author_with_md_url.md")
49+
@test isfile(md_file)
50+
line = read(md_file, String)
51+
@test occursin("[![Author](https://img.shields.io/badge/Author-Johnny%20Chen-blue)](https://github.com/johnnychen94)", line)
52+
@test occursin("![Author](https://img.shields.io/badge/Author-Jane%20Doe-blue)", line)
53+
end
54+
end
55+
end
4056
end

0 commit comments

Comments
 (0)