From 53a1a6b10d1ce126817f72adfeceb4ea130062d7 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Wed, 13 Feb 2019 12:58:38 -0600 Subject: [PATCH 1/3] Add support for a remote url parameter on code blocks. --- index.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 58a5103..7e3e3a6 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ var escape = require('escape-html'); +const fetch = require("node-fetch"); /* Generate HTML for the tab in the header @@ -38,27 +39,41 @@ module.exports = { blocks: { codetabs: { blocks: ['language'], - process: function(parentBlock) { + process: async function(parentBlock) { var blocks = [parentBlock].concat(parentBlock.blocks); - var tabsContent = ''; - var tabsHeader = ''; - blocks.forEach(function(block, i) { + let processBlock = async function(block, i) { var isActive = (i == 0); if (!block.kwargs.name) { throw new Error('Code tab requires a "name" property'); } - tabsHeader += createTab(block, i, isActive); - tabsContent += createTabBody(block, i, isActive); - }); + if (block.kwargs.url) { + block.body = await fetch(block.kwargs.url).then((res) => res.text()); + } + + return { + tabHeader: createTab(block, i, isActive), + tabContent: createTabBody(block, i, isActive) + }; + }; + + let buildOutput = function(formattedResults) { + let tabsHeader = ''; + let tabsContent = ''; + formattedResults.forEach(function(record) { + tabsHeader += record.tabHeader; + tabsContent+= record.tabContent; + }); + return '
' + + '
' + tabsHeader + '
' + + '
' + tabsContent + '
' + + '
'; + }; - return '
' + - '
' + tabsHeader + '
' + - '
' + tabsContent + '
' + - '
'; + return await Promise.all(blocks.map(processBlock)).then(buildOutput); } } } From 012cd2c22b6afa9aeb31ba5a464eef0812ad8730 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Wed, 13 Feb 2019 13:00:38 -0600 Subject: [PATCH 2/3] Document remote-code functionality. --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9bcd9b5..d0fe2ae 100644 --- a/README.md +++ b/README.md @@ -48,5 +48,8 @@ var React = require('react') {%- endcodetabs %} ``` +### Remote code sources - +Alternatively, you may provide a `url` attribute for a given language. In that case, +the body of the block is ignored and the content of the remote URL is used instead. The +syntax highlighting is still controlled by the `type` attribute. From d484c19eeeeda707a480073c966e9d0ec77016b4 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Wed, 13 Feb 2019 13:47:12 -0600 Subject: [PATCH 3/3] Style fix. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7e3e3a6..7833313 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ var escape = require('escape-html'); -const fetch = require("node-fetch"); +var fetch = require('node-fetch'); /* Generate HTML for the tab in the header