Skip to content

Instantly share code, notes, and snippets.

@edm00se

edm00se/References.md Secret

Last active Feb 25, 2016
Embed
What would you like to do?
Finding and replacing emojis...
var emojisUrl = "https://api.github.com/emojis"; // GitHub API for emoji mapping short name to URL of image
var ob = {}; //place holder for response, object keys short name to url of image
var re = /(\:\w+\:)(?=\s|[\!\.\?]|$)/gim;
var str = "dummy string to be replaced by text content of the markdown posts";
var ar = str.match(re); //array of occurences of values that match
var strAr = str.split(re); //array of string values, separated on matching occurences
strAr.forEach(function(curVal, i, array){
var nwVal = ob[curVal];
var tmpRe = new RegExp(curVal, 'gi');
str.replace(str,nwVal);
});
#!/usr/bin/env node
var util = require('util'),
fs = require('fs'),
request = require('request'),
argv = require('minimist')(process.argv.slice(2)),
filePath = argv._[0],
imagePathPrefix = '/images/emoji/',
emojisUrl = "https://api.github.com/emojis",
emojisOb = {},
options = {
url: emojisUrl,
headers: {
'User-Agent': 'Awesome-Octocat-App'
}
},
re = /(\:\w+\:)(?=\s|[\!\.\?]|$)/gim;
console.log('starting');
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var fCt = 0;
var mCt = 0;
emojisOb = JSON.parse(body);
var fileNameAr = fs.readdirSync(filePath);
for( var i=0; i<fileNameAr.length; i++ ){
var curVal = filePath+fileNameAr[i];
if( curVal.substr(-3) === '.md' ){
var file = curVal;
var contents = fs.readFileSync(file, 'utf-8');
//fs.readFile(file, 'utf-8', function(err, contents){
fCt++;
if( re.test(contents) ){
console.log('match found in '+file);
mCt++;
var result = contents;
var foundMatch = false;
for( var prop in emojisOb ){
if( contents.indexOf(':'+prop+':') > -1 ){
foundMatch = true;
console.log('found a match for '+prop+' in '+file);
var nwRe = new RegExp(":"+prop+":","gi");
result = result.replace(nwRe, '<img src="'+imagePathPrefix+prop+'.png'+'" alt="'+prop+'" style="height:auto;width:21px;">');
}
}
if(foundMatch){
fs.writeFile(file, result, 'utf-8', function(er){
if(er){
console.log('error: '+er);
}else{
console.log('writing file back with updates')
}
});
}
}
//});
}
}
console.log('found '+fCt+' .md files and '+mCt+' emoji short name occurrences');
}else{
console.log('error getting '+emojisUrl+', response status code: '+response.statusCode);
console.log('nothing to do, exiting');
}
});
console.log('done');
#!/usr/bin/env node
var util = require('util'),
fs = require('fs');
console.log('starting');
fs.readdir('./', function(err, files){
console.log('searching for .md');
files.filter(function(file){ return file.substr(-3) === '.md'; })
.forEach(function(file){
console.log('reading a .md file'+file);
});
});
console.log('done');
#!/usr/bin/env node
var emojisOb = require('./src/emojis.json');
var sampleUrl = "https://assets-cdn.github.com/images/icons/emoji/unicode/1f424.png?v5";
// console.log(String.fromCodePoint("0x1f37a"));
function getUni( key ){
var url = emojisOb[key];
if( null != url ){
var tmpCode = extractEmojiUnicodeFromUrl(url);
return "0x"+tmpCode;
}else{
return null;
}
}
function extractEmojiUnicodeFromUrl( val ){
var nwVal = val.split("unicode/")[1].split("?")[0].split(".png")[0];
console.log("found a val: "+nwVal);
return nwVal;
}
var myDrink = getUni("beers");
var o = String.fromCodePoint(myDrink);
console.log(o);
console.log("typeof: "+typeof o);
#!/usr/bin/env node
var util = require('util'),
fs = require('fs'),
// http = require('http'),
emojisUrl = "http://api.github.com/emojis",
emojisOb = {};
console.log('starting');
fs.readFile('./emojisResp.json', 'utf-8', function(err, data){
if(err){
console.log('error: '+err);
} else {
emojisOb = JSON.parse(data);
console.log('got emojis master ob');
}
});
var re = /(\:\w+\:)(?=\s|[\!\.\?]|$)/gim;
fs.readdir('./', function(err, files){
console.log('searching for .md');
files.filter(function(file){ return file.substr(-3) === '.md'; })
.forEach(function(file){
console.log('reading a .md file');
fs.readFile(file, 'utf-8', function(err, contents){
if( re.test(contents) ){
var result = contents;
var foundMatch = false;
for( var prop in emojisOb ){
if( contents.indexOf(':'+prop+':') > -1 ){
foundMatch = true;
console.log('found a match for '+prop+' in '+file);
var nwRe = new RegExp(":"+prop+":","gi");
result = result.replace(nwRe, '<img src="'+emojisOb[prop]+'" alt="'+prop+'" style="height:auto;width:21px;">');
}
}
if(foundMatch){
fs.writeFile(file, result, 'utf-8', function(er){
if(er){
console.log('error: '+er);
}else{
console.log('writing file back with '+prop+' replaced as '+'!['+prop+']('+emojisOb[prop]+')')
}
});
}
}
});
});
});
console.log('done');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.