Skip to content

Commit

Permalink
default keymaps, update readme, check for input
Browse files Browse the repository at this point in the history
  • Loading branch information
twilwa committed Jul 9, 2024
1 parent d3be84f commit 36d0146
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
61 changes: 32 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):

```lua
{
"yourusername/crawler.nvim",
'yourusername/crawler.nvim',
dependencies = { 'nvim-lua/plenary.nvim' },
config = function()
require("crawler").setup({
-- Add any configuration options here
render_markdown = true,
render_json = false,
search_engine = true,
-- Add any configuration options here (optional)
})
end,
dependencies = {
"nvim-lua/plenary.nvim",
},
cmd = { "CrawlMarkdown", "CrawlJson", "CrawlSearch" },
}
```

Expand All @@ -38,9 +32,9 @@ You can configure the plugin by passing options to the `setup` function:

```lua
require("crawler").setup({
render_markdown = true, -- Enable markdown rendering
render_json = false, -- Enable JSON rendering
search_engine = true, -- Enable search engine functionality
render_markdown = true, -- Enable markdown rendering (default: true)
render_json = false, -- Enable JSON rendering (default: false)
search_engine = true, -- Enable search engine functionality (default: true)
})
```

Expand All @@ -52,41 +46,50 @@ The plugin provides three main commands:
- `:CrawlJson`: Crawl a URL and render it to JSON
- `:CrawlSearch`: Perform a search query

You can use these commands in normal mode or visual mode (to use the selected text as input).
These commands can be used in normal mode (prompting for input) or visual mode (using the selected text as input).

### Key Mappings
### Default Key Mappings

Add these to your Neovim configuration to set up key mappings:
The plugin comes with the following default key mappings:

```lua
vim.api.nvim_set_keymap('n', '<leader>cm', ':CrawlMarkdown<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>cj', ':CrawlJson<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>cs', ':CrawlSearch<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('v', '<leader>cm', ':CrawlMarkdown<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('v', '<leader>cj', ':CrawlJson<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('v', '<leader>cs', ':CrawlSearch<CR>', { noremap = true, silent = true })
```
- Normal mode:
- `<leader>lm`: Crawl and render to Markdown
- `<leader>lj`: Crawl and render to JSON
- `<leader>ls`: Perform a search query

- Visual mode:
- `<leader>lm`: Crawl selection and render to Markdown
- `<leader>lj`: Crawl selection and render to JSON
- `<leader>ls`: Search using selection

You can override these mappings in your Neovim configuration if desired.

### Examples:

1. Process a single URL and render to Markdown:
```
<leader>cm
https://example.com
:CrawlMarkdown
> Enter URL or search query: https://example.com
```

2. Process multiple URLs and render to JSON:
```
<leader>cj
https://example.com, https://another-example.com
:CrawlJson
> Enter URL or search query: https://example.com, https://another-example.com
```

3. Perform a search:
```
<leader>cs
neovim lua plugins
:CrawlSearch
> Enter search query: neovim lua plugins
```

4. Using visual mode:
- Select a URL or text in visual mode
- Press `<leader>lm` to crawl and render to Markdown
- Press `<leader>lj` to crawl and render to JSON
- Press `<leader>ls` to search using the selected text

## Integration with Other Tools

crawler.nvim is particularly useful when used in conjunction with other plugins and tools that leverage Neovim buffers for various purposes:
Expand Down
23 changes: 21 additions & 2 deletions lua/crawler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,21 @@ local function process_search(query)
insert_into_buffer(response.body)
end

local function get_input(prompt)
local input = vim.fn.mode() == 'v' and get_visual_selection() or ''
if input == '' then
input = vim.fn.input(prompt)
end
return input
end

local function crawl_with_type(render_type)
local input = get_visual_selection()
local prompt = render_type == 'search' and "Enter search query: " or "Enter URL or search query: "
local input = get_input(prompt)

if input == '' then
input = vim.fn.input("Enter URL, multiple URLs (comma-separated), or search query: ")
print("No input provided")
return
end

if input:find(',') then
Expand Down Expand Up @@ -132,4 +143,12 @@ vim.api.nvim_create_user_command('CrawlMarkdown', M.crawl_markdown, {})
vim.api.nvim_create_user_command('CrawlJson', M.crawl_json, {})
vim.api.nvim_create_user_command('CrawlSearch', M.crawl_search, {})

-- Set up default keymaps
vim.api.nvim_set_keymap('n', '<leader>lm', ':CrawlMarkdown<CR>', { noremap = true, silent = true, desc = 'Crawl and render to Markdown' })
vim.api.nvim_set_keymap('n', '<leader>lj', ':CrawlJson<CR>', { noremap = true, silent = true, desc = 'Crawl and render to JSON' })
vim.api.nvim_set_keymap('n', '<leader>ls', ':CrawlSearch<CR>', { noremap = true, silent = true, desc = 'Perform a search query' })
vim.api.nvim_set_keymap('v', '<leader>lm', ':CrawlMarkdown<CR>', { noremap = true, silent = true, desc = 'Crawl selection and render to Markdown' })
vim.api.nvim_set_keymap('v', '<leader>lj', ':CrawlJson<CR>', { noremap = true, silent = true, desc = 'Crawl selection and render to JSON' })
vim.api.nvim_set_keymap('v', '<leader>ls', ':CrawlSearch<CR>', { noremap = true, silent = true, desc = 'Search using selection' })

return M

0 comments on commit 36d0146

Please sign in to comment.