Destroyed Full feature integration with Android OS native apps (Linux edition) (markdown)

Maboroshy 2017-01-25 23:03:34 +04:00
parent 45417ea706
commit 91fb55a268

@ -1,244 +0,0 @@
This article is about using Android OS native apps to view and edit QOwnNotes made notes with maximum comfort and features. Most of the workarounds described here are specific for Linux but few can be used with any other OS. This article was composed on Linux and Android using the apps and workarounds described below.
## Android apps
QOwnNotes uses plain text files with markdown syntax to store it's notes. We can use any plain text editor to view and edit this notes on Android but for getting most of markdown syntax we need a markdown editor.
After trying almost every markdown editor available in Play Store I found two which are light and most suited for viewing and editing folder structured notes:
**[MarkdownX](https://play.google.com/store/apps/details?id=com.ryeeeeee.markdownx&hl=en)**
- Great editor with easy formatting and image inserting;
- File sorted by name;
- Hardcoded "save directory" (/storage/emulated/0/Android/data/com.ryeeeeee.markdownx/files/notes/);
- No syntax highlighting in editor;
- Not customizable.
**[Writeily Pro](https://play.google.com/store/apps/details?id=me.writeily&hl=en)**
- Much more customizable: you can set "save directory", change fonts, lock the app with pin or password and so on;
- Editor has syntax highlighting;
- Files sorted by modification time;
- No buttons for easy inserting or formatting, you'll have to write all the markdown code yourself.
Personally I like MarkdownX more but Writeily Pro is strong contender and have some features you might find useful.
I have not found any Android markdown editor which could render relative links. The importance of this will be explained later.
## Synchronization
We need all notes available on the phone and synced two-way. QOwnNotes has built-in support for OwnCloud which has an Android client. Also note files can be synced using [BitTorrent Sync](https://getsync.com/) or [Syncthing](https://www.syncthing.net/).
As we use markdown editor QOwnNotes note file format should be ".md", not ".txt".
I run BitTorrent Sync on my Linux laptop, Android phone and Synology NAS which acts as an "always on" sync target. After proper setting I haven't yet encountered any issue with using this setup for two-way sync as long as I edit notes on one device at a time.
My QOwnNotes note folders on the laptop are all stored in one directory which is synced with the "save directory" of Android markdown editor.
**HELP WANTED: OwnCloud setup**
## Features and integration
After initial sync we have all the notes in markdown editor which provides the basic features:
1. View and edit notes;
2. Render markdown;
3. Same folder structure as in QOwnNotes;
4. Notes and text search.
No tags, no inline images, no links to notes or stored files.
Lets try getting it all.
### Tags
By default QOwnNotes doesn't store note tags in a portable form. Markdown editor have no tags support at all.
With plain text files there are only 3 easy ways to pass the data: file path, file name and file content. Markdown editor's file list shows path and name. Lets try using both for tags.
QOwnNotes sports a powerful in-app scripting support to implement any kind of odd behaviour. And that's just what we need.
Add this script in the scripting section of QOwnNotes options:
```
import QtQml 2.0
import com.qownnotes.noteapi 1.0
/**
* This script is an example how to add the tag names to the file names of notes
* when a note file gets stored in QOwnNotes
*/
QtObject {
/**
* This function is called when a note gets stored to disk if
* "Allow note file name to be different from headline" is enabled
* in the settings
*
* It allows you to modify the name of the note file
* Return an empty string if the file name of the note should
* not be modified
*
* @param {NoteApi} note - the note object of the stored note
* @return {string} the file name of the note
*/
function handleNoteTextFileNameHook(note) {
// the current note name is the fallback
var fileName = note.name;
// get a list of note text lines
var noteLines = note.noteText.split("\n");
// set the first line of the note as base for the file name
if (noteLines.length > 0) {
// you maybe also want to exclude some disallowed characters here
fileName = noteLines[0];
}
// get a list of all assigned tag names
var tagNameList = note.tagNames();
// add the tag names to the filename
if (tagNameList.length > 0) {
fileName += " [" + tagNameList.join("] [") + "]";
}
script.log("note file name: " + fileName);
return fileName;
}
}
```
Using this script we will get the note files names as "Note name [tag1] [tag2]" so file name search can now search for tags.
But search is not handy on the run. That's where the "path" shall be used. We'll make a parallel directory structure based on tags for quick navigation. With tags now appended to note names the shell script below will create a new "Tag" directory with tags and "untagged" as subdirs and symlinks for all notes there. Using symlinks allows correct saving and syncing of changes made to the notes opened from new "Tag" directory.
Files in "Tag" directory can be modified and it will sync to original notes. Creating new notes or deleting notes in "Tag" won't. New notes will be deleted, deleted notes will be back again. So do creation/deletion in original note folders.
The script should be run *from a directory where all notes directories (folders) stored*.
```
#!/bin/bash
# Set dir names here.
tag_dir=Tag
untagged_dir=Untagged
# Clear tag dir.
rm -r $tag_dir
mkdir -p $tag_dir
# Create symlinks in tag dir. Ignores hidden dirs and files like BTSync's ".sync".
IFS=$'\n'
for file in $(find `pwd` -name *.md -not -path '*/\.*')
do
## Tagged to tags dirs.
for tag in $(echo $file|awk -vRS="]" -vFS="[" '{print $2}')
do
test -d "$tag_dir/$tag" || mkdir -p "$tag_dir/$tag" && cp -s $file "$tag_dir/$tag"
done
## Untagged to untagged dir.
for untagged in $(echo $file|grep -v ].md)
do
test -d "$tag_dir/$untagged_dir" || mkdir -p "$tag_dir/$untagged_dir" && cp -s $file "$tag_dir/$untagged_dir"
done
done
```
I used this script on my notes many times, however any damage it can do is your responsibility. Make backup before using some internet guy's scripts on your files.
While Android and BTSync support symlinks my phone for some reason doesn't. After I synced the results of the script above to my phone the symlinks on it vanished.
So if your phone doesn't eat symlinks the script above is the best option. If it does there's is another way which is little longer.
The script below will do the same as the one above but with files instead of symlinks. It uses a "buffer" dir for modifications syncing.
```
#!/bin/bash
# Set dir names here. You can also put full path for buffer dir so it won't be in the current dir.
tag_dir=Tag
buffer_dir=.Tag-buffer
untagged_dir=Untagged
# Copy modified notes from synced tag dir to buffer dir. They will go to the source dirs through hardlinks.
cp -urT $tag_dir $buffer_dir
# Modified notes are in source dirs. Clear buffer and tag dirs.
rm -r $tag_dir $buffer_dir
# Create hardlinks for notes in buffer dir. Ignores hidden dirs and files like BTSync's ".sync".
IFS=$'\n'
mkdir -p $buffer_dir
for file in $(find `pwd` -name *.md -not -path '*/\.*')
do
## Tagged to tags dirs.
for tag in $(echo $file|awk -vRS="]" -vFS="[" '{print $2}')
do
test -d "$buffer_dir/$tag" || mkdir -p "$buffer_dir/$tag" && ln $file "$buffer_dir/$tag"
done
## Untagged to untagged dir.
for untagged in $(echo $file|grep -v ].md)
do
test -d "$buffer_dir/$untagged_dir" || mkdir -p "$buffer_dir/$untagged_dir" && ln $file "$buffer_dir/$untagged_dir"
done
done
# Copy buffer dir to tag dir. If we sync hardlinks instead of their copies BTSync will replace them with newer files from phone and we won't get modifications back to source dir.
cp -rT $buffer_dir $tag_dir
```
**TODO: Script activation**
### Render inline images
When we insert an image to QOwnNotes it copies it to media subdirectory and puts a relative link to the note text. While this is a perfectly portable solution none of Android markdown editors I've tried support such links. They want the absolute one. So lets give them what they they want in a way that will suit both desktop and phone.
For Linux the easiest way is having the notes stored on the desktop in the very exact path they are stored on the phone which is */storage/emulated/0/Android/data/com.ryeeeeee.markdownx/files/notes/* for MardownX and */storage/emulated/0/writeily* (or whatever you choose) for Writeily Pro.
While QOwnNotes insert only relative link there's a script that will change them to absolute right after insert.
[One of the example scripts](https://github.com/pbek/QOwnNotes/blob/develop/doc/scripting/absolute-media-links.qml) is just about that - putting absolute links instead of default relative ones. So load it in the scripting section in QOwnNotes options and give it a try.
**TODO: Make sure the script actually works**
To "convert" already written notes you can run the following bash script *from a directory where all notes directories (folders) stored*:
```
#!/bin/bash
IFS=$'\n'
for d in *
do ( cd $d && sed -i -e "s|file://media|file://$PWD/media|g" *.md )
done
```
The previous declaimer about shell scripts is valid for this one too.
### Links to other notes and files
Wanted to pull some smart workarounds from my sleeve but I don't have any. Markdown editor don't want to open such links no matter what syntax I use. Avoid depending on them for notes you want to use on phone.
## Using todo.txt format todo lists
[Todo.txt format](http://todotxt.com/) is like markdown for todo lists. It's functional yet it's plain text. Simple and portable.
Best todo.txt app for Android I found is [Simpletask](https://play.google.com/store/apps/details?id=nl.mpcjanssen.simpletask&hl=en) - which provides the great UI for the todo.txt syntax files, even for those having .md extension. I use it's cloudless edition since the only cloud the app supports is Dropbox.
There are some apps for the desktop too, but I prefer plain text in QOwnNotes. To make it structured I put in section headlines followed by "h:1" in-text tag which tells Simpletask not to parse the line as task. Empty lines are also safe to use.
## Fast inbox
Markdown editor provides additional features but it's too bloated when it comes to putting in notes on the run. We need something lightning fast. The fastest Andorid notepad I've found is [Fast Notepad](https://play.google.com/store/apps/details?id=com.taxaly.noteme.v2&hl=en). Change it's "save path" to the note directory you use for inbox and all new notes will appear in QOwnNotes.
For having even faster use I've put the shortcut for it's EditorActivity to the main screen. Such app activity shortcuts can be put by Samsung TouchWiz, using [Tasker](https://play.google.com/store/apps/details?id=net.dinglisch.android.taskerm&hl=en) with [Secure Setting plug-in](https://play.google.com/store/apps/details?id=com.intangibleobject.securesettings.plugin&hl=en) or [many other apps](https://play.google.com/store/search?q=activity%20shortcut&c=apps&hl=en).
As such "fast notes" will have .txt extension they won't be shown in markdown editor, only in Fast Notepad. They will appear in QOwnNotes though where you can move their content to some other notes.
I tend to use really short "fast notes" and I prefer to have them all in one note on my desktop.
I've made a script called [Bash-note](https://github.com/Maboroshy/Bash-note) to be used with QOwnNotes. It has a function to merge all "fast notes" to a single note in an elegant way. Give it a try.
**TODO: Workout conversion of images put to inbox into notes.**