Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add "toggle block quote" #811

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"title": "%command.editing.toggleCodeSpan.title%",
"category": "Markdown All in One"
},
{
"command": "markdown.extension.editing.toggleQuote",
"title": "%command.editing.toggleQuote.title%",
"category": "Markdown All in One"
},
{
"command": "markdown.extension.editing.toggleMath",
"title": "%command.editing.toggleMath.title%",
Expand Down Expand Up @@ -93,6 +98,12 @@
"mac": "cmd+b",
"when": "editorTextFocus && !editorReadonly && editorLangId == markdown"
},
{
"command": "markdown.extension.editing.toggleQuote",
"key": "ctrl+.",
"mac": "cmd+.",
"when": "editorTextFocus && !editorReadonly && editorLangId == markdown"
},
Sean10 marked this conversation as resolved.
Show resolved Hide resolved
{
"command": "markdown.extension.editing.toggleItalic",
"key": "ctrl+i",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"command.editing.toggleMath.title": "触发数学环境",
"command.editing.toggleMathReverse.title": "触发数学环境(反向)",
"command.editing.toggleList.title": "触发列表",
"command.editing.toggleQuote.title": "触发引用",
"config.title": "Markdown All in One",
"config.toc.levels.description": "目录级别的范围. 例如 `2..5` 表示在目录中只包含 2 到 5 级标题",
"config.toc.orderedList.description": "使用有序列表 (1. ..., 2. ...)",
Expand Down
69 changes: 69 additions & 0 deletions src/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function activate(context: ExtensionContext) {
commands.registerCommand('markdown.extension.editing.toggleHeadingDown', toggleHeadingDown),
commands.registerCommand('markdown.extension.editing.toggleList', toggleList),
commands.registerCommand('markdown.extension.editing.toggleCodeBlock', toggleCodeBlock),
commands.registerCommand('markdown.extension.editing.toggleQuote', toggleQuote),
commands.registerCommand('markdown.extension.editing.paste', paste),
commands.registerCommand('markdown.extension.editing._wrapBy', args => styleByWrapping(args['before'], args['after']))
);
Expand Down Expand Up @@ -45,6 +46,74 @@ function toggleCodeBlock() {
return editor.insertSnippet(new SnippetString('```$0\n$TM_SELECTED_TEXT\n```'));
}

enum QuoteState {
// State 1: part of lines has been quoted, need to be quoted
PARTIALQUOTED,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is more natural to quote all the lines (not only the unquoted lines) in this case (thinking about doing multi-line comment).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In consideration of the selected text not always accurate, especially like i use three finder drag, i may select more lines which have been quoted. In this situation, what i need is just to quote the unquoted lines. There is no need to quote which have been quoted which may cause the text format disturbed. So could you share the example which is preferred to quote all selected lines?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In consideration of the selected text not always accurate, especially like i use three finder drag, i may select more lines which have been quoted. In this situation, what i need is just to quote the unquoted lines.

I agree this is probably a good design (for some people, or maybe the majority). But as a first step, I would like to make it consistent with other similar commands, e.g.

multi-line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a switch to decide wthether need to only quote unquoted lines so that default quote behavior is the same as others?

Copy link

@huyz huyz Mar 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you implement a separate unquoting command, @Sean10 can't the user just solve your case by hitting the unquoting command then hitting the quoting command? Same result.

I agree with @yzhang-gh there are many more instances when it's preferable to nest blockquotes.

// State 2: all of lines has been quoted, need to be unquoted
FULLQUOTED,
}

function getQuoteState(lineArray: Array<string>): QuoteState {
let cntQuoted = 0;
console.log("array:", lineArray)
lineArray.forEach(function(line) {
if (line.startsWith(">")) {
cntQuoted += 1;
}
})
Sean10 marked this conversation as resolved.
Show resolved Hide resolved

return cntQuoted != lineArray.length ? QuoteState.PARTIALQUOTED : QuoteState.FULLQUOTED;
}

function quoteLine(line: string): string {
if(!line.startsWith("> ") && line.startsWith(">")) {
return line.replace(/>/g, "> ");
} else if(!line.startsWith("> ")){
return "> " + line;
} else {
return line;
}
}

function unquoteLine(line: string): string {
if (line.startsWith("> ")) {
return line.slice(2);
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line.startsWith('>')?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I thought adding a space is more beautiful and standard. But actually no such setting in github markdown.I will modify this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think we can definitely advocate adding a space after > (actually I like it). I meant we still need to handle the case if there isn't a space.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok~I will modify the space back and skip the line which it hasn't appended space.

return line;
}
}

function setQuote(lineArray: Array<string>, state: QuoteState): string {
let resultArray;
switch(state) {
case QuoteState.PARTIALQUOTED:
resultArray = lineArray.map(quoteLine);
break;
case QuoteState.FULLQUOTED:
resultArray = lineArray.map(unquoteLine);
break;
default:
resultArray = lineArray;
}
return resultArray.join('\n');
Sean10 marked this conversation as resolved.
Show resolved Hide resolved
}

async function toggleQuote() {
let editor = window.activeTextEditor;
let start = editor.selection.start;
let end = editor.selection.end;
start = editor.document.lineAt(start.line).range.start;
let linesText = editor.document.getText(new Range(start, end));;

let line_array = linesText.split('\n');
let state = getQuoteState(line_array);
let resultText = setQuote(line_array, state);

return await editor.edit(editBuilder => {
editBuilder.replace(new Range(start, end), resultText);
});
Sean10 marked this conversation as resolved.
Show resolved Hide resolved
}

function toggleStrikethrough() {
return styleByWrapping('~~');
}
Expand Down