Skip to content

Commit

Permalink
option to expose local files through the web
Browse files Browse the repository at this point in the history
  • Loading branch information
wandersoncferreira committed Aug 6, 2020
1 parent b908aab commit 4521405
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ feature to work, org-roam protocol should be configured in the system.

Also make sure the emacs server is started; `M-x server-start RET`


## Expose Local files

If you want to expose your local files to be accessible through the Preview modal
page you can check perform the following actions:

```elisp
(setq org-roam-server-enable-access-to-local-files t
org-roam-server-webserver-prefix "/home/wand"
org-roam-server-webserver-address "127.0.0.1:8887/"
org-roam-server-webserver-supported-extensions '("pdf" "mp4" "ogv"))
```

There are security reasons your browse disable this feature to be used, however if you are aware of
them and want to expose only in your local network your files, you can use a Web Server like
[Web Server Chrome](https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb)
and expose a specific *folder*.

This *folder* needs to be setup in the `org-roam-server-webserver-prefix` and the address of the webserver
configured at `org-roam-server-webserver-address` variable.

Remember to keep your Web Server active in the background while navigating in your Roam Graph.

Be safe, but also be happy.

## License
org-roam-server is licensed under the MIT License.

Expand Down
51 changes: 51 additions & 0 deletions org-roam-server.el
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ or { \"physics\": { \"enabled\": false } }"
:group 'org-roam-server
:type 'boolean)

(defcustom org-roam-server-enable-access-to-local-files nil
"Enable access to your local files through a Web Server."
:group 'org-roam-server
:type 'boolean)

(defcustom org-roam-server-webserver-address "127.0.0.1:8887/"
"Address to access your local web server."
:group 'org-roam-server
:type 'string)

(defcustom org-roam-server-webserver-prefix nil
"Prefix of the folder being hosted by your webserver e.g. /home/."
:group 'org-roam-server
:type 'string)

(defcustom org-roam-server-webserver-supported-extensions '("pdf" "mp4" "ogv")
"Supported file extensions to be opened by your browser."
:group 'org-roam-server
:type 'list)

(defcustom org-roam-server-network-poll t
"Poll the network changes if it is set to `t`.
If you have a large network and experience lag
Expand Down Expand Up @@ -180,6 +200,13 @@ In the first case options are applied to all edges."
(if (< x 10) (+ x ?0) (+ x (- ?a 10))))))
(buffer-string)))

(defun org-roam-server-concat-or-regexp-tokens (tokens)
"Concatenate TOKENS into OR regexp."
(let ((result (car tokens)))
(dolist (token (cdr tokens))
(setq result (format "%s\\|%s" result token)))
result))

(defun org-roam-server-html-servlet (file)
"Export the FILE to HTML and create a servlet for it."
`(defservlet* ,(intern (concat (org-roam--path-to-slug file) ".html")) text/html (token)
Expand All @@ -191,6 +218,30 @@ In the first case options are applied to all edges."
(setq-local org-export-with-sub-superscripts nil)
(setq-local org-html-style-default org-roam-server-export-style)
(insert-file-contents ,file)

;; Handle opening media in your computer
(if org-roam-server-enable-access-to-local-files
(let* ((file-string (buffer-string))
(matches (s-match-strings-all (format "\\[\\[\\(file:\\)\\(.*\\.\\(%s\\)\\)\\]\\(\\[.*\\]\\)?\\]"
(org-roam-server-concat-or-regexp-tokens
org-roam-server-webserver-supported-extensions))
file-string)))
(dolist (match matches)
(let ((path (elt match 2))
(link (elt match 0)))
(unless (file-name-absolute-p path)
(setq path (concat (file-name-directory ,file) path)))
(setq path (file-truename path))
(if (file-exists-p path)
(setq file-string
(s-replace link (format "[[http://%s%s]%s]" org-roam-server-webserver-address
(string-remove-prefix org-roam-server-webserver-prefix path)
(elt match 4))
file-string)))))
(erase-buffer)
(insert "#+HTML_HEAD: <base target=\"_blank\">\n")
(insert file-string)))

;; Handle images
(if org-roam-server-export-inline-images
(let* ((file-string (buffer-string))
Expand Down

0 comments on commit 4521405

Please sign in to comment.