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

feat(rich-text-links): improve getRichTextEntityLinks() #738

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ each published separately to npm.

### Local Development

Run `npm install` to install all necessary dependencies.
Run `yarn install` to install all necessary dependencies.

As a post-install step, all Lerna dependencies are [hoisted](https://github.com/lerna/lerna/blob/master/doc/hoist.md),
`yarn build` ensures all Lerna dependencies are [hoisted](https://github.com/lerna/lerna/blob/master/doc/hoist.md),
and hence internally reliant packages (e.g., `rich-text-html-renderer`, which
depends upon `rich-text-types`) will resolve their modules via symlink. In other
words, `npm install` will _both_ install external dependencies for each project,
_and_ ensure packages that pull in other packages in this repository as
dependencies are linked to the local version (rather than whatever the state
words, `yarn build` ensure packages that pull in other packages in this repository
as dependencies are linked to the local version (rather than whatever the state
of those packages is on npm).

Each package is written in [TypeScript](https://www.typescriptlang.org/) and
Expand Down
22 changes: 21 additions & 1 deletion packages/rich-text-links/src/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,19 +491,39 @@ describe('getRichTextEntityLinks', () => {
},
content: [],
},
{
nodeType: INLINES.EMBEDDED_ENTRY,
data: {
target: {
sys: {
linkType: 'Entry',
type: 'Link',
id: 'baz',
},
},
},
content: [],
},
],
},
],
};

it('ignores all links of different types', () => {
expect(getRichTextEntityLinks(document, BLOCKS.EMBEDDED_ENTRY)).toEqual({
expect(
getRichTextEntityLinks(document, BLOCKS.EMBEDDED_ENTRY, INLINES.EMBEDDED_ENTRY),
).toEqual({
Entry: [
{
linkType: 'Entry',
type: 'Link',
id: 'foo',
},
{
linkType: 'Entry',
type: 'Link',
id: 'baz',
},
],
Asset: [],
});
Expand Down
6 changes: 4 additions & 2 deletions packages/rich-text-links/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ export function getRichTextEntityLinks(
/**
* Node type. Only the entity links with given node type will be extracted.
*/
type?: string,
...type: (BLOCKS | INLINES | string)[]
): EntityLinks {
const types = new Set(type);

const entityLinks: EntityLinkMaps = {
Entry: new Map(),
Asset: new Map(),
};

// const content = (document && document.content) || ([] as Node[]);
const addLink = ({ data, nodeType }: Node) => {
const hasRequestedNodeType = !type || nodeType === type;
const hasRequestedNodeType = !types.size || types.has(nodeType);

if (hasRequestedNodeType && isLinkObject(data)) {
entityLinks[data.target.sys.linkType].set(data.target.sys.id, data.target.sys);
Expand Down