Skip to content

Commit

Permalink
Merge pull request #122 from brianhuster/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
brianhuster authored Oct 14, 2024
2 parents 1277a38 + 3e7ffb3 commit b005508
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 123 deletions.
91 changes: 65 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Support Katex for rendering math equations in markdown and AsciiDoc files

Supports mermaid for rendering diagrams in markdown files

### [Updates](RELEASE.md)
### Updates

See [RELEASE.md](RELEASE.md)

**⚠️ Important Notice:** You must clear the cache of the browser after updating to ensure the plugin works correctly.

Expand All @@ -25,51 +27,82 @@ https://github.com/user-attachments/assets/865112c1-8514-4920-a531-b2204194f749

## Installation

You can install the plugin using your favorite plugin manager. Here are some examples:
You can install this plugin using a plugin manager. Most plugin managers are supported. Below are some examples

### Using lazy.nvim (recommended)

### Using lazy.nvim
```lua
require("lazy").setup({
{
'brianhuster/live-preview.nvim',
dependencies = {'brianhuster/autosave.nvim'}, -- Not required, but recomended for autosaving
}
opts = {},
}
})
```

### mini.deps
```lua
add({
source = 'brianhuster/live-preview.nvim',
depends = { 'brianhuster/autosave.nvim' }, -- Not required, but recomended for autosaving
})
require('livepreview').setup()
```

### Using vim-plug
### vim-plug
```vim
Plug 'brianhuster/live-preview.nvim'
Plug 'brianhuster/autosave.nvim' " Not required, but recomended for autosaving
let g:livepreview_config = {} " Optional configuration.
lua require('livepreview').setup(vim.g.livepreview_config) " Required to enable the plugin
```

## Setup
### Native package (without a plugin manager)

Add the following to your `init.lua`:
- **Linux, MacOS, Unix-based**

```lua
require('live-preview').setup()
```sh
git clone --depth 1 https://github.com/brianhuster/live-preview.nvim ~/.config/nvim/pack/brianhuster/start/live-preview.nvim
```

For `init.vim`:
- **Windows (Powershell)**

```vim
lua require('live-preview').setup()
```powershell
git clone --depth 1 https://github.com/brianhuster/live-preview.nvim "$HOME/AppData/Local/nvim/pack/brianhuster/start/live-preview.nvim"
```

You can also customize the plugin by passing a table to the `setup` function. Here is an example of how to customize the plugin:bubbles
You must add the line `require('livepreview').setup()` (Lua) or `lua require('livepreview').setup()` (Vimscript) to your Neovim configuration file to enable the plugin.

- Using Lua:
## Setup

You can customize the plugin by passing a table to the `opts` variable (if you use lazy.nvim) or the function `require('livepreview').setup()`. Here is the default configuration:

### In Lua

```lua
require('live-preview').setup({
{
commands = {
start = 'LivePreview', -- Command to start the live preview server and open the default browser. Default is 'LivePreview'
stop = 'StopPreview', -- Command to stop the live preview. Default is 'StopPreview'
start = 'LivePreview', -- Command to start the live preview server and open the default browser.
stop = 'StopPreview', -- Command to stop the live preview.
},
port = 5500, -- Port to run the live preview server on. Default is 5500
browser = "default", -- Browser to open the live preview in. Default is 'default', meaning the default browser of your system will be used
})
port = 5500, -- Port to run the live preview server on.
browser = 'default', -- Terminal command to open the browser for live-previewing (eg. 'firefox', 'flatpak run com.vivaldi.Vivaldi'). By default, it will use the default browser.
}
```

### In Vimscript

```vim
let g:livepreview_config = {
\ 'commands': {
\ 'start': 'LivePreview', " Command to start the live preview server and open the default browser.
\ 'stop': 'StopPreview', " Command to stop the live preview.
\ },
\ 'port': 5500, " Port to run the live preview server on.
\ 'browser': 'default', " Terminal command to open the browser for live-previewing (eg. 'firefox', 'flatpak run com.vivaldi.Vivaldi'). By default, it will use the default browser.
\ }
```

## Usage
Expand All @@ -88,7 +121,7 @@ To stop the live preview server, use the command:

These commands can be changed based on your customization in the `setup` function in your Neovim configuration file.

Use the command `:help live-preview` to see the help documentation.
Use the command `:help livepreview` to see the help documentation.

## Contributing

Expand All @@ -110,8 +143,14 @@ Since this is a young project, there should be a lot of rooms for improvements.
* [mermaid-js/mermaid](https://github.com/mermaid-js/mermaid) for rendering diagrams

### Buy me a coffee
#### Paypal
[https://www.paypal.com/paypalme/brianphambinhan](https://www.paypal.com/paypalme/brianphambinhan)

#### Momo (Vietnam)
[https://me.momo.vn/brianphambinhan](https://me.momo.vn/brianphambinhan)
Maintaining this project takes time and effort, especially as I am a student now. If you find this project helpful, please consider supporting me :>

<a href="https://paypal.me/brianphambinhan">
<img src="https://www.paypalobjects.com/webstatic/mktg/logo/pp_cc_mark_111x69.jpg" alt="Paypal" style="height: 69px;">
</a>
<a href="https://img.vietqr.io/image/mb-9704229209586831984-print.png?addInfo=Donate%20for%20livepreview%20plugin%20nvim&accountName=PHAM%20BINH%20AN">
<img src="https://github.com/user-attachments/assets/f28049dc-ce7c-4975-a85e-be36612fd061" alt="VietQR" style="height: 85px;">
</a>
<a href="https://me.momo.vn/brianphambinhan">
<img src="https://github.com/user-attachments/assets/3907d317-b62f-43f5-a231-3ec7eb4eaa1b" alt="Momo (Vietnam)" style="height: 85px;">
</a>
81 changes: 59 additions & 22 deletions README.vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Hỗ trợ Katex để hiển thị các phương trình toán học trong tệp

Hỗ trợ mermaid để hiển thị các biểu đồ trong tệp Markdown

### [Cập nhật](RELEASE.md)
### Cập nhật

**⚠️ Quan trọng:** Bạn cần xóa bộ nhớ đệm của trình duyệt sau khi cập nhật để plugin hoạt động đúng.
Xem [RELEASE.md](RELEASE.md)

**⚠️ Chú ý:** Bạn cần xóa bộ nhớ đệm của trình duyệt sau khi cập nhật để plugin hoạt động đúng.

## Video demo

Expand All @@ -32,43 +34,74 @@ require("lazy").setup({
{
'brianhuster/live-preview.nvim',
dependencies = {'brianhuster/autosave.nvim'}, -- Không bắt buộc, nhưng nên có để tự động lưu tệp khi bạn chỉnh sửa file
opts = {},
}
})
```

### Với vim-plug
### mini.deps
```lua
add({
source = 'brianhuster/live-preview.nvim',
depends = { 'brianhuster/autosave.nvim' }, -- Not required, but recomended for autosaving
})
require('livepreview').setup()
```

### vim-plug
```vim
Plug 'brianhuster/live-preview.nvim'
Plug 'brianhuster/autosave.nvim' " Không bắt buộc, nhưng nên có để tự động lưu tệp khi bạn chỉnh sửa file
```
Plug 'brianhuster/autosave.nvim' " Not required, but recomended for autosaving
## Thiết lập
let g:livepreview_config = {} " Cấu hình tùy chọn
lua require('livepreview').setup(vim.g.livepreview_config) " Bắt buộc để kích hoạt plugin
```
### Cài đặt thủ công (không dùng trình quản lý plugin)

Thêm đoạn code sau vào `init.lua`:
- **Linux, MacOS, dựa trên Unix**

```lua
require('live-preview').setup()
```sh
git clone --depth 1 https://github.com/brianhuster/live-preview.nvim ~/.config/nvim/pack/brianhuster/start/live-preview.nvim
```

Nếu bạn dùng `init.vim`:
- **Windows (Powershell)**

```vim
lua require('live-preview').setup()
```powershell
git clone --depth 1 https://github.com/brianhuster/live-preview.nvim "$HOME/AppData/Local/nvim/pack/brianhuster/start/live-preview.nvim"
```

Bạn cũng có thể tùy chỉnh plugin. Dưới đây là cấu hình mặc định
## Tùy chỉnh

Bạn có thể tùy chỉnh plugin bằng cách đưa 1 bảng vào biến `opts` (với lazy.nvim) hoặc hàm `require('livepreview`).setup()`. Dưới đây là cấu hình mặc định

### Trong Lua

```lua
require('live-preview').setup({
{
commands = {
start = 'LivePreview', -- Lệnh khởi động máy chủ live-preview.
stop = 'StopPreview', -- Lệnh để dừng máy chủ live-preview.
},
port = 5500, -- Cổng để chạy máy chủ live-preview
browser = "default", -- Trình duyệt để xem kết quả live-preview. Mặc định "default" sẽ mở trình duyệt mặc định của hệ điều hành
})
browser = 'default', -- Lệnh để mở trình duyệt (ví dụ 'firefox', 'flatpak run com.vivaldi.Vivaldi'. Giá trị 'default' là trình duyệt mặc định của hệ điều hành.
}
```

### Trong Vimscript

```vim
let g:livepreview_config = {
\ 'commands': {
\ 'start': 'LivePreview', " Lệnh khởi động máy chủ live-preview.
\ 'stop': 'StopPreview', " Lệnh để dừng máy chủ live-preview.
\ },
\ 'port': 5500, " Cổng để chạy máy chủ live-preview
\ 'browser': 'default', " Lệnh để mở trình duyệt (ví dụ 'firefox', 'flatpak run com.vivaldi.Vivaldi'. Giá trị 'default' là trình duyệt mặc định của hệ điều hành.
\ }
```

**⚠️ Chú ý:** Đảm bảo rằng bạn cấu hình `g:livepreview_config` trước khi gọi `lua require('livepreview').setup()`.

## Cách dùng

> Hướng dẫn dưới đây sử dụng cấu hình mặc định
Expand All @@ -83,7 +116,7 @@ Lệnh này sẽ mở tệp Markdown hoặc HTML hiện tại trong trình duy

`:StopPreview`

Gõ lệnh `:help live-preview` để xem bằng tiếng Anh.
Gõ lệnh `:help livepreview` để xem bằng tiếng Anh.

## Đóng góp

Expand All @@ -107,9 +140,13 @@ Vì đây là một dự án khá mới, hẳn sẽ có nhiều điều cần c


### Ủng hộ
#### Momo (Việt Nam)
[https://me.momo.vn/brianphambinhan](https://me.momo.vn/brianphambinhan)

#### Paypal
[https://www.paypal.com/paypalme/brianphambinhan](https://www.paypal.com/paypalme/brianphambinhan)

<a href="https://me.momo.vn/brianphambinhan">
<img src="https://github.com/user-attachments/assets/3907d317-b62f-43f5-a231-3ec7eb4eaa1b" alt="Momo (Vietnam)" style="height: 85px;">
</a>
<a href="https://img.vietqr.io/image/mb-9704229209586831984-print.png?addInfo=Donate%20for%20livepreview%20plugin%20nvim&accountName=PHAM%20BINH%20AN">
<img src="https://github.com/user-attachments/assets/f28049dc-ce7c-4975-a85e-be36612fd061" alt="VietQR" style="height: 85px;">
</a>
<a href="https://paypal.me/brianphambinhan">
<img src="https://www.paypalobjects.com/webstatic/mktg/logo/pp_cc_mark_111x69.jpg" alt="Paypal" style="height: 69px;">
</a>
5 changes: 5 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### Breaking changes
`require('live-preview')` is deprecated (but still usable). Use `require('livepreview')` instead.

For help, use `:help livepreview` instead of `:help live-preview`.

### New features

Improve performance
Expand Down
31 changes: 22 additions & 9 deletions doc/livepreview.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

==============================================================================
Lua module : require("livepreview") *livepreview*

Expand Down Expand Up @@ -47,8 +48,9 @@ Server class for live-preview.nvim To call this class, do >lua
• {watch_dir} (`fun(self: Server, func: function)`) Watch a directory
for changes and send a message "reload" to a WebSocket
client
{start} (`fun(self: Server, ip: string, port: number)`) Start the
server
{start} (`fun(self: Server, ip: string, port: number, func: function, client: uv_tcp_t)`)
Start the server

{stop} (`fun(self: Server)`) Stop the server


Expand All @@ -67,12 +69,16 @@ Server:routes({path}) *livepreview.server.Server:routes()*
Return: ~
(`string`) path to the file

Server:start({ip}, {port}) *livepreview.server.Server:start()*
*livepreview.server.Server:start()*
Server:start({ip}, {port}, {func}, {client})
Start the server

Parameters: ~
{ip} (`string`) IP address to bind to
{port} (`number`) port to bind to
{ip} (`string`) IP address to bind to
{port} (`number`) port to bind to
{func} (`function`)
{client} (`uv_tcp_t`) uv_tcp client


Server:stop() *livepreview.server.Server:stop()*
Stop the server
Expand Down Expand Up @@ -125,8 +131,9 @@ send_http_response({client}, {status}, {content_type}, {body}, {headers})
{client} (`uv_tcp_t`) client connection
{status} (`string`) for example "200 OK", "404 Not Found", etc.
• {content_type} (`string`) MIME type of the response
{body} (`string`) body of the response
{headers} (`table`) (optional) additional headers to include in
{body} (`string`) body of the respons
{headers} (`table?`) (optional) additional headers to include in

the response

*livepreview.server.handler.serve_file()*
Expand Down Expand Up @@ -264,9 +271,14 @@ kill_port({port}) *livepreview.utils.kill_port()*
open_browser({path}, {browser}) *livepreview.utils.open_browser()*
Open URL in the browser

Example: ```lua open_browser("https://neovim.io/", "firefox")
open_browser("https://neovim.io/", "flatpak run com.microsoft.Edge") >
<

Parameters: ~
{path} (`string`)
{browser} (`string`)
{browser} (`string?`)


sha1({val}) *livepreview.utils.sha1()*
Compute the SHA1 hash of a string.
Expand Down Expand Up @@ -296,7 +308,8 @@ term_cmd({cmd}, {callback}) *livepreview.utils.term_cmd()*
Parameters: ~
{cmd} (`string`) terminal command to execute. Term_cmd will use
sh or pwsh depending on the OS
{callback} (`function`) function to call when the command finishes.
{callback} (`function?`) function to call when the command finishes.

• code: the exit code of the command
• signal: the signal that killed the process
• stdout: the standard output of the command
Expand Down
15 changes: 10 additions & 5 deletions lua/livepreview/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ function M.is_compatible(ver, range)
return requirement:has(ver)
end

local function checkhealth_command(cmd)
if vim.fn.executable(cmd) then
vim.health.ok(cmd)
else
vim.health.warn(cmd .. " not available")
end
end


--- Run health check for Live Preview. This can also be run using `:checkhealth livepreview`
--- @see https://neovim.io/doc/user/health.html
function M.check()
Expand All @@ -38,11 +47,7 @@ function M.check()
"For Live Preview to open default browser, at least one of these commands must be executable. If you have specified a custom browser in your configuration, you can ignore this message.")
local open_cmds = { "xdg-open", "open", "start", "rundll32", "wslview" }
for _, cmd in ipairs(open_cmds) do
if vim.fn.executable(cmd) then
vim.health.ok(cmd)
else
vim.health.warn(cmd .. " not available")
end
checkhealth_command(cmd)
end
end

Expand Down
Loading

0 comments on commit b005508

Please sign in to comment.