Created
November 19, 2014 07:31
-
-
Save lordfriend/1e6f1cc8b7cd3c1acdf6 to your computer and use it in GitHub Desktop.
grunt-markdown work with angular custom html
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
{ | |
preCompile: function(src, context) { | |
var match = src.match(/<example>[\s\S]+?<\/example>/mg); | |
if(match && match.length > 0) { | |
match.forEach(function(example) { | |
var subMatch = example.match(/<file\s*name=".+?">[\s\S]+?<\/file>/mg); | |
var group, newExample = example, | |
htmlContent, | |
appName = 'example' + exampleCount + 'App', | |
htmlFileName, | |
jsTemplate, | |
jsFileName; | |
if(subMatch && subMatch.length > 0) { | |
for( var i = 0; i < subMatch.length; i++) { | |
group = subMatch[i].match(/<file\s*name="(.+?)">\n*([\s\S]+?)\n*<\/file>/m); | |
var fileName = group[1], | |
code = group[2], | |
lang, codeWrapped; | |
if(path.extname(fileName) === '.html') { | |
lang = 'html'; | |
htmlFileName = fileName; | |
htmlContent = code; | |
} else if(path.extname(fileName) === '.js') { | |
lang = 'javascript'; | |
jsFileName = fileName; | |
jsTemplate = grunt.template.process(grunt.file.read('docs/src/script-template'), { | |
data: { | |
appName: appName, | |
jsContent: code | |
} | |
}); | |
grunt.file.write(path.join(__dirname, 'docs/dist/examples/example' + exampleCount, fileName), jsTemplate); | |
} | |
codeWrapped = '\n```'+lang+'\n'+code + '\n```\n'; | |
newExample = newExample.replace(code, codeWrapped); | |
} | |
// read template | |
var htmlTemplate = grunt.file.read('docs/src/example-template.html'); | |
console.log({ | |
appName: appName, | |
jsFile: jsFileName, | |
templateHTML: htmlContent | |
}); | |
htmlTemplate = grunt.template.process(htmlTemplate, { | |
data:{ | |
appName: appName, | |
jsFile: jsFileName, | |
templateHTML: htmlContent | |
} | |
}); | |
grunt.file.write(path.join(__dirname, 'docs/dist/examples/example' + exampleCount, htmlFileName), htmlTemplate); | |
} | |
// append an iframe to show demo | |
newExample = newExample + | |
'<iframe class="runnable-example-frame" src="/examples/example' + exampleCount +'/' + htmlFileName + '"></iframe>'; | |
// replace the code with wrapped code. | |
src = src.replace(example, newExample); | |
exampleCount++; | |
}); | |
} | |
return src; | |
} | |
} |
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
// prevent {{ }} being parsed by angular | |
{ | |
postCompile: function() { | |
var match = src.match(/<pre>[\s\S]+?<\/pre>/mg); | |
if(match && match.length > 0) { | |
match.forEach(function(str) { | |
var subMatch = str.match(/{{[\s\S]+?}}/mg); | |
var group, newStr = str; | |
if(subMatch && subMatch.length > 0) { | |
for( var i = 0; i < subMatch.length; i++) { | |
group = subMatch[i].match(/{{([\s\S]+?)}}/m); | |
newStr = newStr.replace(subMatch[i], '<span>{{</span>' + group[1] + '<span>}}</span>'); | |
} | |
} | |
src = src.replace(str, newStr); | |
}); | |
} | |
return src; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment