Skip to content

Instantly share code, notes, and snippets.

@titangene
Last active September 3, 2022 08:56
Show Gist options
  • Select an option

  • Save titangene/7490c024a92f6553ca191f1c30f09ad9 to your computer and use it in GitHub Desktop.

Select an option

Save titangene/7490c024a92f6553ca191f1c30f09ad9 to your computer and use it in GitHub Desktop.
Google translate 文字不換行
(() => {
const inlineCodePattern = /(\w+\.)*\w+\(\)/g;
function formatParagraph(paragraph) {
const lines = paragraph.split('\n');
return formatLines(lines);
}
function formatLines(lines) {
const result = lines
.map(line => formatLine(line.trim()))
.filter(line => line.trim().length > 0)
.join(' ');
return formatInlineCode(result);
}
function formatInlineCode(string) {
/**
* ex:
* - "foo()" --> "`foo()`"
* - "foo.bar()" --> "`foo.bar()`"
*/
return string.replaceAll(inlineCodePattern, match => '`' + match + '`');
}
function formatLine(line) {
if (line.endsWith('-')) {
return line.slice(0, -1);
}
else if (line.startsWith('//')) {
return line.slice(2).trim();
}
else if (line === '*') {
return '';
}
else if (line.startsWith('* ')) {
return line.slice(2).trim();
}
else {
return line;
}
}
const textarea = document.querySelector('c-wiz textarea');
const paragraphs = textarea.value.split('\n\n');
const formattedParagraphs = paragraphs.map(formatParagraph).join('\n\n');
textarea.value = formattedParagraphs;
const event = new CustomEvent('input', { bubbles: true });
textarea.dispatchEvent(event);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment