Remove generated anchor links from feed content

This commit is contained in:
Râu Cao 2024-10-23 14:54:01 +02:00
parent ba80792cc4
commit 062ded9e6d
Signed by: raucao
GPG Key ID: 37036C356E56CC51
3 changed files with 191 additions and 1 deletions

View File

@ -18,7 +18,7 @@ export function profileAtomFeed(profile: Profile, articles: Article[]) {
<updated>${isoDate(article.updatedAt)}</updated>
<summary>${article.summary}</summary>
<content type="html"><![CDATA[
${article.html}
${cleanContentHtml(article.html)}
]]></content>
</entry>
`;
@ -38,3 +38,11 @@ export function profileAtomFeed(profile: Profile, articles: Article[]) {
</feed>
`.trim();
}
export function cleanContentHtml(html: string) {
const cleanHtml = html.replace(
/<a class="anchor" aria-hidden="true"[^>]*>.*?<\/a>/gs,
"",
);
return cleanHtml;
}

17
tests/feeds_test.ts Normal file
View File

@ -0,0 +1,17 @@
import { describe, it } from "@std/testing/bdd";
import { expect } from "@std/expect";
import { cleanContentHtml } from "../feeds.ts";
describe("Feeds", () => {
describe("#cleanContentHtml", () => {
let articleHtml = Deno.readTextFileSync(
"tests/fixtures/gfm-content-1.html",
);
it("removes the anchor links for headlines", () => {
const cleanHtml = cleanContentHtml(articleHtml);
expect(cleanHtml).not.toMatch(/<a class="anchor" aria-hidden="true"/);
expect(cleanHtml).not.toMatch(/<svg class="octicon octicon-link"/);
});
});
});

165
tests/fixtures/gfm-content-1.html vendored Normal file
View File

@ -0,0 +1,165 @@
<p>
This week, it finally happened: I still had a Lightning channel open with a
node that hadn't been online for the better part of a year now, so I decided
to close the channel unilaterally. But force-closing a channel means you have
to broadcast the latest commitment transaction, the pre-set fee of which was
only ~1 sat/vB for this one.
</p>
<p>
With LND, if the channel is created as an <a
href="https://lightning.engineering/posts/2021-01-28-lnd-v0.12/"
rel="noopener noreferrer"
>anchor channel</a> (by default only since version 0.12), then the commitment
transaction contains small extra outputs (currently 330 sats), which let
either channel partner spend one of them into a child transaction that can be
created with higher fees to pay for the parent transaction (CPFP). LND even
has a built-in command for that: <code>lncli wallet bumpclosefee</code>
</p>
<p>
However, this channel was created in the old-school way, and was thus stuck
with its low fee. In fact, even the local bitcoin node refused to accept the
transaction into its own mempool, so the bitcoin p2p network didn't even know
it existed. So how do we get out of this pickle?
</p>
<h2 id="the-solution">
<a class="anchor" aria-hidden="true" tabindex="-1" href="#the-solution"><svg
class="octicon octicon-link"
viewBox="0 0 16 16"
width="16"
height="16"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"
>
</path>
</svg></a>The solution
</h2>
<p>
Enter the <a
href="https://mempool.space/accelerator"
rel="noopener noreferrer"
>mempool.space Accelerator</a>. It is essentially an automated way to create
agreements with various mining pools to mine your low-fee transaction in
exchange for an out-of-band payment. Mempool.space coordinates these
agreements and out-of-band payments with miners and gets a share from the
overall fee for that.
</p>
<p>
Now, if you're in the same situation as I was, you might search for the ID of
your closing transaction and find that mempool.space cannot find it. Remember
how the local bitcoin node (with mostly default settings) didn't accept it in
the first place?
</p>
<h3 id="1-get-the-transaction-to-be-broadcast">
<a
class="anchor"
aria-hidden="true"
tabindex="-1"
href="#1-get-the-transaction-to-be-broadcast"
><svg
class="octicon octicon-link"
viewBox="0 0 16 16"
width="16"
height="16"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"
>
</path>
</svg></a>1. Get the transaction to be broadcast
</h3>
<p>In your <code>bitcoin.conf</code>, add the following line:</p>
<pre><code>minrelaytxfee=0</code></pre><p>
This sets the minimum fee to 0, meaning it will accept and broadcast your
transactions, no matter how low the fee is. Restart <code>bitcoind</code> and
wait a little bit. LND will retry broadcasting the closing transaction every
minute or so until it succeeds. At some point you should be able to find it on
mempool.space.
</p>
<h3 id="2-use-the-accelerator-to-confirm-it">
<a
class="anchor"
aria-hidden="true"
tabindex="-1"
href="#2-use-the-accelerator-to-confirm-it"
><svg
class="octicon octicon-link"
viewBox="0 0 16 16"
width="16"
height="16"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"
>
</path>
</svg></a>2. Use the Accelerator to confirm it
</h3>
<p>
Once you can see the transaction on <a
href="https://mempool.space"
rel="noopener noreferrer"
>mempool.space</a>, you can just click the "Accelerate" button next to the
ETA. This will bring you to a page that shows you the estimated share of
miners that will include your transaction in their blocks, as well as some
acceleration fee options for various transaction fee levels, which you can pay
for via the Lightning Network, of course.
</p>
<p>
If you haven't looked into this service before (which I had), then the fees
might be a bit of a surprise to you. This thing is <strong>not</strong> cheap!
Bumping my fee from 1 sat/vB to <del>9 sats/vB cost a whopping 51,500 sats
(</del>31 USD that day). Bumping it higher only seemed to add the difference
in the transaction fee itself, so the service seems to have cost a flat 50K
sats at the time.
</p>
<p>
Unfortunately, this channel wasn't particularly large, so the acceleration fee
amounted to ~9% of my remaining channel balance. But 91% of something is
better than 100% of nothing, so I actually felt pretty good about it.
</p>
<p>Next, you will see something like this:</p>
<p>
<img
src="https://image.nostr.build/76151cc2ae06a93a8fcd97102bf4fa63541f8f3bd19800b96ff1070c9450945c.png"
alt="Screenshot of an accelerated transaction on mempool.space"
/>
</p>
<p>
Time to lean back and let the miners work for you. In my case, the ETA was
eerily precise. It told me that it would take ~56 minutes to confirm the
transaction, and almost exactly an hour later it was mined.
</p>
<h3 id="3-wait">
<a class="anchor" aria-hidden="true" tabindex="-1" href="#3-wait"><svg
class="octicon octicon-link"
viewBox="0 0 16 16"
width="16"
height="16"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"
>
</path>
</svg></a>3. Wait
</h3>
<p>
Now that our transaction is confirmed, our channel is not closed immediately,
of course. The <a
href="https://docs.lightning.engineering/the-lightning-network/multihop-payments/hash-time-lock-contract-htlc"
rel="noopener noreferrer"
>time lock of the HTLC</a> protects our channel partner from us broadcasting
an old channel state in which our balance might be higher than in the latest
state.
</p>
<p>
In my case, it was set to 144 blocks, i.e. ~24 hours. So I checked back the
next day, et voilá: channel closed and balance restored. 🥳
</p>