parent
7301a2da16
commit
c0b5756a0a
|
|
@ -8,10 +8,9 @@ module.exports =
|
|||
|
||||
activate: (state) ->
|
||||
@subscriptions = new CompositeDisposable()
|
||||
@updateNav('watchDir')
|
||||
|
||||
@subscriptions.add atom.commands.add 'atom-workspace',
|
||||
'amWiki:updateNav': => @updateNav('updateNav')
|
||||
'amWiki:updateNav': => @updateNav()
|
||||
@subscriptions.add atom.commands.add 'atom-workspace',
|
||||
'amWiki:create': => @createWiki()
|
||||
|
||||
|
|
@ -21,16 +20,13 @@ module.exports =
|
|||
destroy: ->
|
||||
@subscriptions.dispose()
|
||||
|
||||
updateNav: (type) ->
|
||||
updateNav: ->
|
||||
editor = atom.workspace.getActiveTextEditor()
|
||||
return unless editor
|
||||
grammar = editor.getGrammar()
|
||||
return unless grammar
|
||||
return unless grammar.scopeName is 'source.gfm'
|
||||
if type == 'updateNav'
|
||||
navUpdate.updateNav(editor.getPath())
|
||||
else if type == 'watchDir'
|
||||
navUpdate.watchDir(editor.getPath())
|
||||
navUpdate.updateNav(editor.getPath())
|
||||
|
||||
createWiki: ->
|
||||
editor = atom.workspace.getActiveTextEditor()
|
||||
|
|
@ -59,19 +55,9 @@ module.exports =
|
|||
|
||||
thefile = new File(editor.getPath())
|
||||
|
||||
if thefile.getParent().getParent().getBaseName() is 'library'
|
||||
assetsDirPath = thefile.getParent().getParent().getParent().getPath()+"/assets"
|
||||
creatDirPath = assetsDirPath+'/'+thefile.getParent().getBaseName()
|
||||
writePath = assetsDirPath+'/'+thefile.getParent().getBaseName()+'/'
|
||||
insertPath = thefile.getParent().getBaseName()+'/'
|
||||
else
|
||||
if thefile.getParent().getBaseName() is 'library'
|
||||
assetsDirPath = thefile.getParent().getParent().getPath()+"/assets"
|
||||
creatDirPath = assetsDirPath+'/'
|
||||
writePath = assetsDirPath+'/'
|
||||
insertPath = ''
|
||||
else
|
||||
return
|
||||
return unless thefile.getParent().getParent().getBaseName() is 'library'
|
||||
|
||||
assetsDirPath = thefile.getParent().getParent().getPath()+"/assets"
|
||||
|
||||
crypto = require "crypto"
|
||||
md5 = crypto.createHash 'md5'
|
||||
|
|
@ -79,12 +65,12 @@ module.exports =
|
|||
|
||||
filename = "#{thefile.getBaseName().replace(/\.\w+$/, '').replace(/\s+/g,'')}-#{md5.digest('hex').slice(0,5)}.png"
|
||||
|
||||
@createDirectory creatDirPath, ()=>
|
||||
@writePng writePath, filename, imgbuffer, ()=>
|
||||
@createDirectory assetsDirPath+'/'+thefile.getParent().getBaseName(), ()=>
|
||||
@writePng assetsDirPath+'/'+thefile.getParent().getBaseName()+'/', filename, imgbuffer, ()=>
|
||||
# ascClip = "assets/#{filename}"
|
||||
# clipboard.writeText(ascClip)
|
||||
|
||||
@insertUrl "", editor
|
||||
@insertUrl ".getBaseName()}/#{filename})",editor
|
||||
|
||||
return false
|
||||
|
||||
|
|
|
|||
|
|
@ -15,24 +15,6 @@ var readDir = function (path, callback) {
|
|||
}
|
||||
});
|
||||
};
|
||||
var createMD = function(data){
|
||||
var markdown = '';
|
||||
markdown += '\n#### [首页](# "返回首页") ####\n';
|
||||
for (var dir in data) {
|
||||
if (data.hasOwnProperty(dir)) {
|
||||
if (dir == 'assets') {
|
||||
continue;
|
||||
}
|
||||
markdown += '\n##### ' + dir.substring(3) + ' #####\n';
|
||||
for (var i = 0; i < data[dir].length; i++) {
|
||||
var name = data[dir][i].split('.md')[0];
|
||||
markdown += '- [' + name.substring(4) + '](#file=' +
|
||||
dir + '/' + name + ' "' + name.substring(4) + '")\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
return markdown;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
updateNav: function (editorPath) {
|
||||
|
|
@ -43,26 +25,23 @@ module.exports = {
|
|||
var path = editorPath.split('library')[0] + 'library/';
|
||||
readDir(path, function (err, data) {
|
||||
if (!err) {
|
||||
var markdown = createMD(data);
|
||||
fs.writeFileSync(path + '_navigation_.md', markdown, 'utf-8');
|
||||
var markdown = '';
|
||||
markdown += '\n#### [首页](# "返回首页") ####\n';
|
||||
for (var dir in data) {
|
||||
if (data.hasOwnProperty(dir)) {
|
||||
if (dir == 'assets') {
|
||||
continue;
|
||||
}
|
||||
markdown += '\n##### ' + dir.substring(3) + ' #####\n';
|
||||
for (var i = 0; i < data[dir].length; i++) {
|
||||
var name = data[dir][i].split('.md')[0];
|
||||
markdown += '- [' + name.substring(4) + '](#file=' +
|
||||
dir + '/' + name + ' "' + name.substring(4) + '")\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(path + '_navigation_.md', markdown, 'utf-8');
|
||||
}
|
||||
});
|
||||
},
|
||||
watchDir: function(editorPath) {
|
||||
console.log(editorPath);
|
||||
if (editorPath.indexOf('library') < 0) {
|
||||
return;
|
||||
}
|
||||
var path = editorPath.split('library')[0] + 'library/';
|
||||
fs.watch(path, function(event, fileName){
|
||||
if (fileName != '_navigation_.md') {
|
||||
readDir(path, function (err, data) {
|
||||
if (!err) {
|
||||
var markdown = createMD(data);
|
||||
fs.writeFileSync(path + '_navigation_.md', markdown, 'utf-8');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
'submenu': [{
|
||||
'label': 'amWiki 文库',
|
||||
'submenu': [{
|
||||
'label': '手动刷新 “_navigation_.md”',
|
||||
'label': '刷新 “_navigation_.md”',
|
||||
'command': 'amWiki:updateNav'
|
||||
},{
|
||||
'label': '通过 “config.json” 文件创建新amWiki文库',
|
||||
|
|
|
|||
Loading…
Reference in New Issue