Skip to content

Commit

Permalink
Fix a bug when theme.font has empty family value (theme-next#1015)
Browse files Browse the repository at this point in the history
* maybe need refactor
  • Loading branch information
1v9 authored Jul 26, 2019
1 parent c856e14 commit fb60a45
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions layout/_partials/head/external-fonts.swig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% set font_display = '&display=swap' %}
{% set font_subset = '&subset=latin,latin-ext' %}
{% set font_styles = ':300,300italic,400,400italic,700,700italic' %}
{% set font_host = font_config.host | default('//fonts.googleapis.com') %}

{% if font_config.global.family and font_config.global.external %}
{% set font_global = font_config.global.family + font_styles %}
Expand All @@ -27,11 +28,31 @@
{% set font_codes = font_config.codes.family + font_styles %}
{% endif %}

{# Get a font list from font_config #}
{% set font_families = [font_global, font_title, font_headings, font_posts, font_codes] %}

{# Original font list is not empty #}
{% if font_families !== '' %}
{% set font_host = font_config.host | default('//fonts.googleapis.com') %}
<link rel="stylesheet" href="{{ font_host }}/css?family={{ String(font_families | uniq | join('|')).slice(0, -1).concat(font_display, font_subset) }}"/>
{# Original font list includes empty value - Some font is not setted. #}
{% if font_families.includes('') %}
{# Unique filter original font list and join `/`, then convert it to font string #}
{% set font_families = String(font_families | uniq | join('|')) %}
{# Font string ends with `|` - The last font is not setted #}
{% if font_families.endsWith('|') %}
{# Remove the unneeded last `|` from font string #}
{% set font_families = font_families.slice(0, -1) %}
{# Font string contains `||` - The font in middle part is not fully setted. #}
{% else %}
{# Replace the unneeded `||` in font string with `|` #}
{% set font_families = font_families.replace('||', '|') %}
{% endif %}
{# Original font list doesn't include empty value - Every font is setted. #}
{% else %}
{# Unique filter original font list and join `/`, then convert it to font string #}
{% set font_families = String(font_families | uniq | join('|')) %}
{% endif %}
{# Merge extra parameters to the final processed font string #}
{##}<link rel="stylesheet" href="{{ font_host }}/css?family={{ font_families.concat(font_display, font_subset) }}"/>
{% endif %}

{% endif %}

0 comments on commit fb60a45

Please sign in to comment.