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

Fix page link and render line break #40

Closed
wants to merge 3 commits into from
Closed

Fix page link and render line break #40

wants to merge 3 commits into from

Conversation

tdloi
Copy link

@tdloi tdloi commented Nov 19, 2020

Notion Page: https://www.notion.so/tdloi/Page-1-8ac0c155447d44d699fd8e8007d3ca69
Preview: https://csb-x51vy.netlify.app/
CodeSandbox source: https://codesandbox.io/s/laughing-frog-x51vy?file=/src/App.js (preview will not work because fail to install directly from Github repo, but deploy to Netlify work)

Comment on lines +26 to +33
<React.Fragment key={i}>
{text.split(/(\n)/).map((t, index) => (
<React.Fragment key={index}>
{t !== "\n" ? t : <br />}
</React.Fragment>
))}
</React.Fragment>
);
Copy link
Author

Choose a reason for hiding this comment

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

This looks a bit hacky, should we wrap it inside span and use innerHTML?

Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines +36 to +37
if (text === "‣" && decorations[0][0] === "p") {
text = blocks[decorations[0][1]]?.value?.properties?.title ?? "";
Copy link
Author

Choose a reason for hiding this comment

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

I add blocks param so we could map page title here, and since we only use blockMap, it's not possible to map user mention to username

@@ -52,6 +64,12 @@ export const createRenderChildText = (
{element}
</a>
);
case "p":
return (
Copy link
Author

@tdloi tdloi Nov 19, 2020

Choose a reason for hiding this comment

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

I'm thinking of adding some hooks here to allow changing page url, what do you think @timolins?
For example:

const pageUrl = hooks?.setPageUrl?.(decorator[1]) ?? decorator[1];
return (
    <a className="notion-link" href={pageUrl} key={i}>
    {element}
    </a>
);

This is useful for those want to map pageId to slug (me 😄 ):

      hooks={{
        setPageUrl: (pageId) => props.postsIndex?.[pageId] ?? pageId,
      }}

Some hooks I could think of:

  • setLinkUrl: So you could extract block Id to add in page link
  • setPageUrl: Change page url in case of using slug
  • setBlockAttributes (not implemented): Add id attribute for a block so in page link could work, this also allow us to set id for block which has a link in page pointed to only, avoiding adding a bunch of id which will increase HTML file size by a lot. Could also use for foot notes.

@tanishqkancharla
Copy link

I was worried my links would lead outside the block map I looked at, so I did this instead:

const PageTitle: React.FC<{ id: string }> = ({ id }) => {
  const [title, setTitle] = React.useState<null | string>(null)
  getPage(id).then((blockmap) => {
    setTitle(blockmap[id].value.properties?.title)
  })
  if (title) {
    return <a href={`/${id}`}>{title}</a>
  } else {
    return <a style={{ fontSize: '85%' }}>Loading...</a>
  }
}

@janniks
Copy link
Contributor

janniks commented Apr 15, 2021

I'll chime in, regarding the missing line-breaks: In vue-notion, we solved this via CSS white-space: pre-line (which collapses similarly to normal/default, but preserves line-breaks). I think it's a more simple fix than adding an \n parsing functionality with the complexity of adding more fragments.

src/style.css

.notion-callout-text {
  margin-left: 8px;
+  white-space: pre-line;
}

This comment is only regarding the line-break issue, NOT the page link issue

@tdloi
Copy link
Author

tdloi commented Mar 2, 2022

Sorry for late reply, I will close this PR as t's been a long time and I don't have time working on this
Actually I didn't blog much 😄

@tdloi tdloi closed this Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants