Skip to content

Commit

Permalink
add ${selectedText} predefine
Browse files Browse the repository at this point in the history
  • Loading branch information
telesoho committed Feb 28, 2023
1 parent 6e81c00 commit 825469c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Smartly paste for Markdown.
- If you paste an image, the extension will create an new file for the image and insert link code to Markdown.
- If you paste a text, it will test the text with customize regex, and replace matched content by regex.
- If you paste a text contain HTML tag, it will try to convert the HTML content to Markdown.
- If you paste a rich text, it will try to convert the rich text to Markdown.(Linux only)
- If you paste a rich text, it will try to convert the rich text to Markdown.(Linux & Windows only)
![](./res/images/markdown-paste-rich-text-html-table.gif)

- Download file
Expand Down Expand Up @@ -71,6 +71,7 @@ Smartly paste for Markdown.
- `${fileExtname}` - the current opened file's extension
- `${fileDirname}` - the current opened file's directory name
- `${datetime}` - the current date & time formatted by `"yyyyMMDDHHmmss"`, You can customize the format by format string. exp: `${datetime|yyyy-MM-DD_HH-mm-ss}`
- `${selectedText}` - the current selected text. If selected text contain illegal characters `\/:*?""<>|\r\n` it will return "".

- `MarkdownPaste.path`

Expand Down
28 changes: 2 additions & 26 deletions src/paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ class Paster {
editor.insertSnippet(rubyTag);
}

private static isHTML(content) {
return /<[a-z][\s\S]*>/i.test(content);
}

private static writeToEditor(content): Thenable<boolean> {
let startLine = vscode.window.activeTextEditor.selection.start.line;
const selection = vscode.window.activeTextEditor.selection;
Expand Down Expand Up @@ -508,8 +504,6 @@ class Paster {
let ret = Paster.parse_rules(content);
if (typeof ret === "string") {
return ret;
} else {
return content;
}

try {
Expand All @@ -534,10 +528,6 @@ class Paster {
// Logger.log(error);
}

if (Paster.isHTML(content)) {
return toMarkdown(content);
}

return content;
}

Expand Down Expand Up @@ -678,16 +668,6 @@ class Paster {
}

let filePath = fileUri.fsPath;
// get selection as image file name, need check
const selection = editor.selection;
const selectText = editor.document.getText(selection);

if (selectText && !/^[^\\/:\*\?""<>|]{1,120}$/.test(selectText)) {
vscode.window.showInformationMessage(
"Your selection is not a valid file name!"
);
return;
}

// get image destination path
let folderPathFromConfig = this.getConfig().path;
Expand All @@ -709,12 +689,8 @@ class Paster {
let namePrefix = this.getConfig().namePrefix;
let nameBase = this.getConfig().nameBase;
let nameSuffix = this.getConfig().nameSuffix;
if (!selectText) {
imageFileName = namePrefix + nameBase + nameSuffix + extension;
imageFileName = this.replacePredefinedVars(imageFileName);
} else {
imageFileName = selectText + extension;
}
imageFileName = namePrefix + nameBase + nameSuffix + extension;
imageFileName = this.replacePredefinedVars(imageFileName);

// image output path
let folderPath = path.dirname(filePath);
Expand Down
20 changes: 20 additions & 0 deletions src/predefine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ class Predefine {
public file(): string {
return this.filePath();
}

/**
* Get current selected text.
* @returns
* string: selected text
* "" : if selected text contain illegal characters
*/
public selectedText(): string {
const selection = vscode.window.activeTextEditor.selection;
const selectText =
vscode.window.activeTextEditor.document.getText(selection);

if (selectText && !/^[^\\/:\*\?""<>|\r\n]*$/.test(selectText)) {
vscode.window.showInformationMessage(
"The selected text contains illegal characters that cannot be used as a file name!"
);
return "";
}
return selectText;
}
}

export { Predefine };

0 comments on commit 825469c

Please sign in to comment.