refactor(helpers): js() and css() helpers now support custom attributes

- https://github.com/hexojs/hexo/pull/3681
- https://github.com/hexojs/hexo/pull/3690
This commit is contained in:
curben 2019-11-14 02:27:25 +00:00
parent 5cd412bda6
commit 887f9ec116
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
4 changed files with 7 additions and 71 deletions

View File

@ -1,6 +1,6 @@
<% if (is_post()) { %> <% if (is_post()) { %>
<%- addJs({ src: '/libs/clipboard.js/2.0.4/clipboard.min.js', 'data-sri-fallback': '/js/clipboard.min.js', integrity: 'sha384-8CYhPwYlLELodlcQV713V9ZikA3DlCVaXFDpjHfP8Z36gpddf/Vrt47XmKDsCttu', crossorigin: 'anonymous', defer: true}) %> <%- js({ src: '/libs/clipboard.js/2.0.4/clipboard.min.js', 'data-sri-fallback': '/js/clipboard.min.js', integrity: 'sha384-8CYhPwYlLELodlcQV713V9ZikA3DlCVaXFDpjHfP8Z36gpddf/Vrt47XmKDsCttu', crossorigin: 'anonymous', defer: true}) %>
<% } %> <% } %>
<%/* javascript of Typing theme */%> <%/* javascript of Chameleon theme */%>
<%- addJs('/js/chameleon.js') %> <%- js('/js/chameleon.js') %>

View File

@ -44,11 +44,12 @@
<link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml"> <link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml">
<%/* Fallback function for SRI */%> <%/* Fallback function for SRI */%>
<%- addJs({ src: '/js/sri.min.js', 'async': true }) %> <%- js({ src: '/js/sri.min.js', 'async': true }) %>
<%- addCss('/css/chameleon.css') %> <%/* CSS of Chameleon theme */%>
<%- css('/css/chameleon.css') %>
<% if (theme.icons) { %> <% if (theme.icons) { %>
<%- addCss({ href: '/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' }) %> <%- css({ href: '/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>

View File

@ -1,32 +0,0 @@
'use strict'
/* global hexo */
/*
* Helper to embed css file with support of custom attributes
* https://github.com/hexojs/hexo/pull/3690
*/
hexo.extend.helper.register('addCss', (...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"'
Object.keys(item).forEach(attribute => {
tmpResult += ` ${attribute}="${item[attribute]}"`
})
tmpResult += '>\n'
result += tmpResult
}
})
return result
})

View File

@ -1,33 +0,0 @@
'use strict'
/* global hexo */
/*
* Helper to add <script src=""> with support of custom attributes
* https://github.com/hexojs/hexo/pull/3681
*/
hexo.extend.helper.register('addJs', (...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 += `<script src="${item}"></script>\n`
} else {
// New syntax
let tmpResult = '<script'
Object.keys(item).forEach(attribute => {
if (item[attribute] === true) tmpResult += ' ' + attribute
else tmpResult += ` ${attribute}="${item[attribute]}"`
})
tmpResult += '></script>\n'
result += tmpResult
}
})
return result
})