使用书签分别对每个部分进行排序

Sort each section separately using bookmarklet

我在文本区域使用维基百科语法。我的文字看起来像这样...

=main-heading=
test
again
practice
==sub-heading==
america
usa
india

我使用在这里找到的小书签...

https://gist.github.com/aquilax/ac94ea6003f2ddb1e1fd6195c00ac3d9

当我排序时,我得到了这个。如何分别对每个部分进行排序?

==sub-heading==
=main-heading=
again
america
india
practice
test
usa

预计:

=main-heading=
again
practice
test
==sub-heading==
america
india
usa

这个 awk 命令有效:

awk '/=/ {c++} {print c+1, [=13=]}' t1.txt | sort -n | cut -d' ' -f2- | sed '/^$/d'

但是awk不能在浏览器中使用。是否可以在 JS 书签中包含转换?

https://en.wikipedia.org/wiki/Schwartzian_transform

尝试这样的事情:

function sortSections() {
  const textarea = document.querySelector('textarea');

  textarea.value = textarea.value
    .trim()
    .split(/\n(?==.+=$)/m)
    .map(section => {
      const [heading, body] = section.split(/(?<==)\n/);
      return `${heading}\n${[...new Set(body.split('\n'))].sort().join('\n')}`;
    })
    .join('\n');
}
<textarea cos="40" rows="10">=main-heading=
test
again
practice
==sub-heading==
india
america
usa
india</textarea>

<button onclick="sortSections();">Sort</button>

书签:

javascript: {
  const textarea = document.querySelector('textarea');

  void (textarea.value = textarea.value
    .trim()
    .split(/\n(?==.+=$)/m)
    .map(section => {
      const [heading, body] = section.split(/(?<==)\n/);
      return `${heading}\n${[...new Set(body.split('\n'))].sort().join('\n')}`;
    })
    .join('\n'));
}

<a href="javascript: {  const textarea = document.querySelector('textarea');  void (textarea.value = textarea.value    .trim()    .split(/\n(?==.+=$)/m)    .map(section => {      const [heading, body] = section.split(/(?<==)\n/);      return `${heading}\n${[...new Set(body.split('\n'))].sort().join('\n')}`;    })    .join('\n'));}">Bookmarklet</a>