Skip to content

Instantly share code, notes, and snippets.

@lordfriend
Created November 19, 2014 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordfriend/1e6f1cc8b7cd3c1acdf6 to your computer and use it in GitHub Desktop.
Save lordfriend/1e6f1cc8b7cd3c1acdf6 to your computer and use it in GitHub Desktop.
grunt-markdown work with angular custom html
{
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;
}
}
// 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