API.md

May 6, 2022 ยท View on GitHub

NOTE: You can ommit function's parentheses if the argument is only a string or table

local clip_img = require('clipboard-image')
clip_img.setup({img_dir = 'assets'})

-- Is the same as
local clip_img = require 'clipboard-image'
clip_img.setup {img_dir = 'assets'}

How to call these function:

require 'clipboard-image.<module>'.<function>()

Example:

-- Paste image
require 'clipboard-image.paste'.paste_img()

Modules

Init

For this module, it's fine if you don't specify the module's name.

-- These will produce the same result
require 'clipboard-image'.setup()
require 'clipboard-image.init'.setup()
FunctionParameterReturnDescription
setupconfig: configSet your configuration up

Config

FunctionParameterReturnDescription
get_configconfig: configGet raw config
merge_configold_opts: table
new_opts: table
config: table
get_usable_configusable_config: img_optsOriginal config isn't ready to use because it contains config for various filetypes.
Default config for all filetype and config for current filetype need to be merged to be usable before pasting image.
load_configconfig_toload: img_optsloaded_config: tableConfig field which value is function needs to be loaded first.
{ img_name = function () return os.date('%Y-%m-%d-%H-%M-%S') end }
to { img_name = "2021-08-21-16-14-17" }

Config Structure

FieldTypeDescription
defaultimg_optsThe default value of img_opts for all filetype
<file-type>img_optsValue of img_opts for specific filetype.
Run :set filetype? to get filetype name.

Image Options

FieldTypeDefault ValueDescription
img_namestring, functionfunction () return os.date('%Y-%m-%d-%H-%M-%S') end
Example result: "2021-08-21-16-14-17"
Image's name
img_dirstring, function"img"Directory to save the image
img_dir_txtstring, function"img"Directory on the text/buffer.
Example: Your actual dir is src/assets/img but your dir on text or buffer is /assets/img
img_handlerfunctionfunction(img) endFunction that will handle image after pasted.
img is a table that contain image's name and path
affixstring, functiondefault: "%s"
markdown: "![](%s)"
asciidoc: "image::%s[]"
String that sandwiched the image's path

Paste

FunctionParameterReturnDescription
paste_imgopts?: img_optsPaste name

Utils

FunctionParameterReturnDescription
get_osos_name: string
Windows, Linux, Darwin, etc
Reference https://vi.stackexchange.com/a/2577/33116
get_clip_commandcmd_check: string
cmd_paste: string
Get command to check and paste clipboard content
get_clip_contentcommand: string
command to check clip_content
usable_config: tableWill be used in utils.is_clipboard_img to check if image data exist
is_clipboard_imgcontent: string
clipboard-content
loaded_config: tableCheck if clipboard contain image data
See also: Data URI scheme
resolve_dirdir: string | table
path_separator: string
full_path: stringResolve any complicated pathing
create_dirdir: string dir pathCreate directory
get_img_pathdir: string
img_name: string
is_txt?: "txt"
Wether img_path will be used as inserted text or to copy image because Windows has different path separator
img_path: stringGet image's path
NOTE: Probably will replace is_text with path_separator
insert_txt
insert_text
affix: string
path_txt: string
Insert image's path with affix