Skip to content

Commit

Permalink
Hacky footnote support
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Feb 19, 2025
1 parent 47be08c commit a1a55e5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
18 changes: 11 additions & 7 deletions posts/2024-11-17-expectations-disappointment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ Expectations and Disappointment

It’s your responsibility to deal with your frustration when you wanted someone to want something, and they don’t.

Being close with someone means you build a mental model of their behavior.
Getting to know someone means you build a mental model of their behavior.
You spend enough time around them that you come to trust this model to predict how they are going to react to something.
And then they don’t.
And you were actually looking forward to the other response they could have given, the one they didn’t give.
This feeling is called “frustration”.

Now there are a few ways people tend to go from here.
Now there are a few ways people tend to go from here. E.g.

I don’t deserve to feel frustrated, that’s not fair.
This person I’m close with is not very nice to do that to me.
This person is not very nice to do that to me.

We could say, they expect the other person to be able to read minds.
To react in a way that caters to the unspoken expectations of someone else.
We could say, they expect the other person to be able to read minds
to react in a way that caters to the unspoken expectations of someone else.

or

I really believed that person I’m close with to be the kind of person that would have done the other thing.
Maybe they just did this thing I didn’t like that once, that would be naughty of them.
I really believed that person to be the kind of person that would have done the other thing.
Maybe they just did this thing I didn’t like that one time, that would be naughty of them.
Or it’s a character flaw of them to be the kind of person that doesn’t do that.

In other words, they are filling in the blanks in their mental model with themselves;
Expand All @@ -31,6 +31,8 @@ or
The other way to react
----------------------

I often have to remind myself of the rationalist mantra. [1]_

.. epigraph::

As a rationalist, my strength is to be more confused by fiction than reality.
Expand All @@ -47,3 +49,5 @@ don’t make the situation toxic by expecting the worst, but think “I notice t
PS: People tend to forget rules that rarely come into play.
Especially if they aren’t aware that how important they are to you.
So make sure that close others know what’s important to you.

.. [1] The problems in the rationalist community notwithstanding, they do have nice ideals.
59 changes: 51 additions & 8 deletions src/build-tools/rst.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,33 @@ function convertNode(node: RSTNode, level: number): m.Node[] {
switch (node.type) {
case 'document':
return convertChildren(node, level)
case 'comment':
case 'comment': {
const comment = (node.children as [RSTInlineNode])[0].value

const [name, text] = parseCommentAsFootnote(comment)
if (name !== null && text !== null) {
return [
// TODO: anchor
<m.EnumList pos={pos(node)} start={Number(name)}>
<m.ListItem pos={pos(node)}>{text}</m.ListItem>
</m.EnumList>,
]
}
return []
case 'reference': {
const name = (node.children[0] as RSTInlineNode).value
}
case 'reference':
case 'footnote_reference': {
const text = (node.children[0] as RSTInlineNode).value
const [name, label] =
node.type === 'footnote_reference'
? [
`footnote-${text}`,
<m.Superscript pos={pos(node)}>{text}</m.Superscript>,
]
: [text, text]
return [
<m.Link ref={{ name }} pos={pos(node)}>
{name}
{label}
</m.Link>,
]
}
Expand Down Expand Up @@ -289,16 +309,39 @@ function* extractTargetsInner(
yield [name, `#${anchor}`]
} else if (child.type === 'comment') {
const comment = (child.children as [RSTInlineNode])[0].value
const [, name = null, href = null] =
/^_([^:]+):\s+(.+)$/.exec(comment) ?? []
// TODO: “_`name with backticks`: ...”
if (name !== null && href !== null) yield [name.toLocaleLowerCase(), href]

{
// normal reference
const [, name = null, href = null] =
/^_([^:]+):\s+(.+)$/.exec(comment) ?? []
// TODO: “_`name with backticks`: ...”
if (name !== null && href !== null) {
yield [name.toLocaleLowerCase(), href]
}
}

{
// footnote reference
const [name, text] = parseCommentAsFootnote(comment)
if (name !== null && text !== null) {
const ref = `footnote-${name.toLocaleLowerCase()}`
yield [ref, `#${ref}`]
}
}
} else if ('children' in child) {
yield* extractTargetsInner(child)
}
}
}

function parseCommentAsFootnote(
comment: string,
): [string | null, string | null] {
const [, name = null, text = null] =
/^\[([^\]]+)\]\s+(.+)$/.exec(comment) ?? []
return [name, text]
}

const URL_SCHEMA = /^https?:.*$/
const ANCHOR_SCHEMA = /^#.*$/

Expand Down
6 changes: 5 additions & 1 deletion src/components/markup/MarkupNodeComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const MarkupNodeComponentInner: FC<MarkupElementProps> = ({ node, level }) => {
}
case Type.EnumList:
return (
<ol style={{ listStyleType: node.enumeration }}>
<ol style={{ listStyleType: node.enumeration }} start={node.start}>
{convertChildren(node, level)}
</ol>
)
Expand Down Expand Up @@ -168,6 +168,10 @@ const MarkupNodeComponentInner: FC<MarkupElementProps> = ({ node, level }) => {
return <em>{convertChildren(node, level)}</em>
case Type.Strong:
return <strong>{convertChildren(node, level)}</strong>
case Type.Subscript:
return <sub>{convertChildren(node, level)}</sub>
case Type.Superscript:
return <sup>{convertChildren(node, level)}</sup>
case Type.Link: {
if ('name' in node.ref)
throw new ASTError(`Unresolved reference ${node.ref.name}`, node)
Expand Down
15 changes: 15 additions & 0 deletions src/markup/MarkupDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export enum Type {
LineBreak,
Emph,
Strong,
Subscript,
Superscript,
Link,
Code,
InlineMath,
Expand Down Expand Up @@ -101,6 +103,8 @@ export type Elem =
| LineBreak
| Emph
| Strong
| Subscript
| Superscript
| Link
| Code
| InlineMath
Expand Down Expand Up @@ -192,6 +196,7 @@ export const BulletList = mkFun<BulletList>(Type.BulletList)
export interface EnumList extends Element {
type: Type.EnumList
enumeration?: Enumeration
start?: number | undefined
}
export const EnumList = mkFun<EnumList>(Type.EnumList)
export interface ListItem extends Element {
Expand Down Expand Up @@ -263,6 +268,16 @@ export interface Strong extends Element {
}
export const Strong = mkFun<Strong>(Type.Strong)

export interface Subscript extends Element {
type: Type.Subscript
}
export const Subscript = mkFun<Subscript>(Type.Subscript)

export interface Superscript extends Element {
type: Type.Superscript
}
export const Superscript = mkFun<Superscript>(Type.Superscript)

export interface Link extends Element {
type: Type.Link
ref: { name: string } | { href: string }
Expand Down
9 changes: 8 additions & 1 deletion typings/restructured/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
declare module 'restructured' {
export type InlineNodeType = 'text' | 'literal' | 'emphasis' | 'strong'
export type InlineNodeType =
| 'text'
| 'literal'
| 'emphasis'
| 'strong'
| 'subscript'
| 'superscript'
export type BlockNodeType =
| 'document'
| 'section'
| 'comment'
| 'reference'
| 'footnote_reference'
| 'title'
| 'paragraph'
| 'block_quote'
Expand Down

0 comments on commit a1a55e5

Please sign in to comment.