11'use strict' ;
22
33const _ = require ( 'lodash' ) ;
4+ const URL = require ( 'url' )
45
5- const { resourceHintAllowedTypes, addResourceHint} = require ( '../lib/resourceHints' ) ;
6+ const { resourceHintAllowedTypes, addResourceHint } = require ( '../lib/resourceHints' ) ;
67
78const fontProviders = {
89 'Google' : {
@@ -19,7 +20,7 @@ const fontProviders = {
1920 *
2021 * @returns {string }
2122 */
22- parser : function ( fonts ) {
23+ parser : function ( fonts ) {
2324 var collection = [ ] ,
2425 familyHash = { } ;
2526
@@ -53,13 +54,19 @@ const fontProviders = {
5354 return collection ;
5455 } ,
5556
56- buildLink : function ( fonts , fontDisplay ) {
57+ buildLink : function ( fonts , fontDisplay ) {
5758 const displayTypes = [ 'auto' , 'block' , 'swap' , 'fallback' , 'optional' ] ;
5859 fontDisplay = displayTypes . includes ( fontDisplay ) ? fontDisplay : 'swap' ;
59- return `<link href="https://fonts.googleapis.com/css?family=${ fonts . join ( '|' ) } &display=${ fontDisplay } " rel="stylesheet">` ;
60+ const uri = `https://fonts.googleapis.com/css?family=${ fonts . join ( '|' ) } &display=${ fontDisplay } `
61+ try {
62+ const url = URL . parse ( uri ) ;
63+ return `<link href="${ url . format ( ) } " rel="stylesheet">` ;
64+ } catch ( error ) {
65+ throw new Error ( `Invalid URL [${ uri } ]. Check configured fonts.` )
66+ }
6067 } ,
6168
62- buildFontLoaderConfig : function ( fonts ) {
69+ buildFontLoaderConfig : function ( fonts ) {
6370 function replaceSpaces ( font ) {
6471 return font . split ( '+' ) . join ( ' ' ) ;
6572 }
@@ -97,10 +104,10 @@ const fontProviders = {
97104 * @param {Object } options an optional object for additional configuration details
98105 * @returns {Object.<string, Array>|string }
99106 */
100- module . exports = function ( format , themeSettings , handlebars , options ) {
107+ module . exports = function ( format , themeSettings , handlebars , options ) {
101108
102109 const collectedFonts = { } ;
103- _ . each ( themeSettings , function ( value , key ) {
110+ _ . each ( themeSettings , function ( value , key ) {
104111 //check that -font is on end of string but not start of string
105112 const fontKeySuffix = '-font' ;
106113 if ( ! key . endsWith ( fontKeySuffix ) ) {
@@ -122,37 +129,37 @@ module.exports = function(format, themeSettings, handlebars, options) {
122129 } ) ;
123130
124131 // Parse font strings based on provider
125- const parsedFonts = _ . mapValues ( collectedFonts , function ( value , key ) {
132+ const parsedFonts = _ . mapValues ( collectedFonts , function ( value , key ) {
126133 return fontProviders [ key ] . parser ( value ) ;
127134 } ) ;
128135
129136 // Format output based on requested format
130- switch ( format ) {
131- case 'linkElements' :
137+ switch ( format ) {
138+ case 'linkElements' :
132139
133- const formattedFonts = _ . mapValues ( parsedFonts , function ( value , key ) {
134- if ( options . globals && options . state ) {
135- fontProviders [ key ] . generateResourceHints ( options . globals , options . state , value , options . fontDisplay ) ;
136- }
137- return fontProviders [ key ] . buildLink ( value , options . fontDisplay ) ;
138- } ) ;
139- return new handlebars . SafeString ( _ . values ( formattedFonts ) . join ( '' ) ) ;
140-
141- case 'webFontLoaderConfig' :
142- // Build configs
143- const configs = _ . mapValues ( parsedFonts , function ( value , key ) {
144- return fontProviders [ key ] . buildFontLoaderConfig ( value ) ;
145- } ) ;
146-
147- // Merge them
148- const fontLoaderConfig = { } ;
149- _ . each ( configs , function ( config ) {
150- return Object . assign ( fontLoaderConfig , config ) ;
151- } ) ;
152- return fontLoaderConfig ;
153-
154- case 'providerLists' :
155- default :
156- return parsedFonts ;
140+ const formattedFonts = _ . mapValues ( parsedFonts , function ( value , key ) {
141+ if ( options . globals && options . state ) {
142+ fontProviders [ key ] . generateResourceHints ( options . globals , options . state , value , options . fontDisplay ) ;
143+ }
144+ return fontProviders [ key ] . buildLink ( value , options . fontDisplay ) ;
145+ } ) ;
146+ return new handlebars . SafeString ( _ . values ( formattedFonts ) . join ( '' ) ) ;
147+
148+ case 'webFontLoaderConfig' :
149+ // Build configs
150+ const configs = _ . mapValues ( parsedFonts , function ( value , key ) {
151+ return fontProviders [ key ] . buildFontLoaderConfig ( value ) ;
152+ } ) ;
153+
154+ // Merge them
155+ const fontLoaderConfig = { } ;
156+ _ . each ( configs , function ( config ) {
157+ return Object . assign ( fontLoaderConfig , config ) ;
158+ } ) ;
159+ return fontLoaderConfig ;
160+
161+ case 'providerLists' :
162+ default :
163+ return parsedFonts ;
157164 }
158165}
0 commit comments