feat(addCss): add helper to embed css file

This commit is contained in:
curben 2019-08-26 09:12:05 +09:30
parent 2dacb8f5fe
commit b4ff248560
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
2 changed files with 36 additions and 2 deletions

34
scripts/addCss.js Normal file
View File

@ -0,0 +1,34 @@
'use strict'
/* global hexo */
/*
* Helper to embed css file with support of custom attributes
* https://github.com/hexojs/hexo/pull/3690
*/
function cssHelper (...args) {
let result = '\n'
let items = args
if (!Array.isArray(args)) {
items = [args]
}
items.forEach(item => {
// Old syntax
if (typeof item === 'string' || item instanceof String) {
result += `<link rel="stylesheet" href="${item}">\n`
} else {
// New syntax
let tmpResult = '<link rel="stylesheet"'
for (const attribute in item) {
tmpResult += ` ${attribute}="${item[attribute]}"`
}
tmpResult += '>\n'
result += tmpResult
}
})
return result
}
hexo.extend.helper.register('addCss', cssHelper)

View File

@ -44,8 +44,8 @@
<%/* Fallback function for SRI */%> <%/* Fallback function for SRI */%>
<%- js('js/sri.min') %> <%- js('js/sri.min') %>
<%- css('css/typing') %> <%- addCss('css/typing.css') %>
<% if (theme.icons) { %> <% if (theme.icons) { %>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fork-awesome/1.1.7/css/fork-awesome.min.css" data-sri-fallback="/forkawesome/css/fork-awesome.min.css" integrity="sha384-mByhW6NjnxyShh67P9+fepUvYSd7Uz/qV6e2u4kA2Fi4ZkjXxIP2mRkyK9dwK24W" crossorigin="anonymous"> <%- addCss({ href: 'https://cdnjs.cloudflare.com/ajax/libs/fork-awesome/1.1.7/css/fork-awesome.min.css', 'data-sri-fallback': '/forkawesome/css/fork-awesome.min.css', integrity: 'sha384-mByhW6NjnxyShh67P9+fepUvYSd7Uz/qV6e2u4kA2Fi4ZkjXxIP2mRkyK9dwK24W', crossorigin: 'anonymous' }) %>
<% } %> <% } %>
</head> </head>