-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from SukkaW/refactor-once
Feat: implements #127
- Loading branch information
Showing
10 changed files
with
210 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
/* Minify JSON inside <script> tags */ | ||
export default function minifyJson(tree) { | ||
// Match all <script> tags which have JSON mime type | ||
tree.match({tag: 'script', attrs: {type: /(\/|\+)json/}}, node => { | ||
let content = (node.content || []).join(''); | ||
if (! content) { | ||
return node; | ||
} | ||
const rNodeAttrsTypeJson = /(\/|\+)json/; | ||
|
||
try { | ||
content = JSON.stringify(JSON.parse(content)); | ||
} catch (error) { | ||
return node; | ||
export function onContent() { | ||
return (content, node) => { | ||
let newContent = content; | ||
if (node.attrs && node.attrs.type && rNodeAttrsTypeJson.test(node.attrs.type)) { | ||
try { | ||
newContent = JSON.stringify(JSON.parse((content || []).join(''))); | ||
} catch (error) { | ||
// Invalid JSON | ||
} | ||
} | ||
|
||
node.content = [content]; | ||
return node; | ||
}); | ||
|
||
return tree; | ||
return newContent; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.