Skip to content

Commit

Permalink
[DEV-863]: rewrite URLs if they point to https://docs.pagopa.it (#560)
Browse files Browse the repository at this point in the history
* feat(DEV-863): rewrite URLs if they point to https://docs.pagopa.it

* feat(DEV-863): update guides mapping

* feat(DEV-863): update switch cases

* feat(DEV-863): add anchor and test

* feat(DEV-863): fix sanitazion error

* feat(DEV-863): fix sanitazion error

* feat(DEV-863): fix sanitazion error

* feat(DEV-863): update rewrite logic

* feat(DEV-863): simplify logic

* chore: fix eslint error

* test: update test

* feat(DEV-863): update urls map

* feat(DEV-863): simplify logic

* chore: update changeset

* chore: remove wrong changeset

* feat(DEV-863): fix typo

Co-authored-by: Marco Comi <[email protected]>

---------

Co-authored-by: Marco Comi <[email protected]>
Co-authored-by: marcobottaro <[email protected]>
  • Loading branch information
3 people authored Feb 2, 2024
1 parent f67d231 commit 558d23c
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/hip-pigs-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gitbook-docs": patch
"nextjs-website": patch
---

[DEV-863]: Replace URLs host if specified in config
43 changes: 43 additions & 0 deletions apps/nextjs-website/src/_contents/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,47 @@ export const spaceToPrefixMap = pipe(
RA.uniq(Eq.struct({ spaceId: s.Eq, pathPrefix: s.Eq }))
);

/**
* Contains the mapping between the docs.pagopa.it url and the developer portal url.
*/
export const urlReplacesMap: { readonly [url: string]: string } = {
// App IO
'https://docs.pagopa.it/manuale-operativo-dei-servizi':
'/app-io/guides/manuale-servizi',
'https://docs.pagopa.it/kb-enti': '/app-io/guides/supporto-agli-enti',
'https://docs.pagopa.it/kb-enti-adesione':
'/app-io/guides/supporto-agli-enti',
'https://docs.pagopa.it/kb-enti-servizi': '/app-io/guides/supporto-agli-enti',
'https://docs.pagopa.it/kb-enti-messaggi':
'/app-io/guides/supporto-agli-enti',
'https://docs.pagopa.it/kb-enti-pagamenti':
'/app-io/guides/supporto-agli-enti',
'https://docs.pagopa.it/kb-enti-accordi': '/app-io/guides/supporto-agli-enti',
'https://docs.pagopa.it/kb-enti-assistenza':
'/app-io/guides/supporto-agli-enti',
'https://docs.pagopa.it/kit-di-comunicazione-per-gli-enti':
'/app-io/guides/kit-comunicazione',
'https://docs.pagopa.it/io-come-aderire': '/app-io/guides/accordi-adesione',

// SEND
'https://docs.pagopa.it/f.a.q.-per-integratori/knowledge-base-di-piattaforma-notifiche':
'/send/guides/knowledge-base',
'https://docs.pagopa.it/modello-di-integrazione-di-piattaforma-notifiche':
'/send/guides/modello-di-integrazione',

// PagoPA
'https://docs.pagopa.it/manuale-back-office-pagopa':
'/pago-pa/guides/manuale-bo-ec',
'https://docs.pagopa.it/manuale-back-office-pagopa/manuale-operativo-back-office-pagopa-ente-creditore/':
'/pago-pa/guides/manuale-bo-ec',
'https://docs.pagopa.it/manuale-back-office-pagopa/v/manuale-bo-pagopa-psp/':
'/pago-pa/guides/manuale-bo-psp',
'https://docs.pagopa.it/gestionedeglierrori': '/pago-pa/guides/errori',
'https://docs.pagopa.it/dizionario-dei-metadata': '/pago-pa/guides/metadata',

// Firma con IO
'https://docs.pagopa.it/manuale-operativo-di-firma-con-io':
'/firma-con-io/guides/manuale-operativo',
};

export const products = [appIo, ioSign, send, pagoPa];
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import GuideMenu from '@/components/atoms/GuideMenu/GuideMenu';
import GitBookContent from '@/components/organisms/GitBookContent/GitBookContent';
import GuideInPageMenu from '@/components/organisms/GuideInPageMenu/GuideInPageMenu';
import { FragmentProvider } from '@/components/organisms/FragmentProvider/FragmentProvider';
import { gitBookPagesWithTitle, spaceToPrefixMap } from '@/_contents/products';
import {
gitBookPagesWithTitle,
spaceToPrefixMap,
urlReplacesMap,
} from '@/_contents/products';
import { translations } from '@/_contents/translations';
import { ParseContentConfig } from 'gitbook-docs/parseContent';
import { Metadata } from 'next';
Expand Down Expand Up @@ -82,6 +86,7 @@ const Page = async ({ params }: { params: Params }) => {
assetsPrefix: source.assetsPrefix,
gitBookPagesWithTitle,
spaceToPrefix: spaceToPrefixMap,
urlReplaces: urlReplacesMap,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { getTutorial, getTutorialPaths } from '@/lib/api';
import { Product } from '@/lib/types/product';
import GitBookContent from '@/components/organisms/GitBookContent/GitBookContent';
import { Box } from '@mui/material';
import { gitBookPagesWithTitle, spaceToPrefixMap } from '@/_contents/products';
import {
gitBookPagesWithTitle,
spaceToPrefixMap,
urlReplacesMap,
} from '@/_contents/products';
import { ParseContentConfig } from 'gitbook-docs/parseContent';
import { Metadata } from 'next';
import { makeMetadata } from '@/helpers/metadata.helpers';
Expand Down Expand Up @@ -63,6 +67,7 @@ const Page = async ({ params }: { params: Params }) => {
assetsPrefix: source.assetsPrefix,
gitBookPagesWithTitle,
spaceToPrefix: spaceToPrefixMap,
urlReplaces: urlReplacesMap,
},
};

Expand Down
42 changes: 42 additions & 0 deletions packages/gitbook-docs/src/__tests__/parseContent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const config = {
title: 'S1 Home',
},
],
urlReplaces: {
'https://docs.pagopa.it/modello-di-integrazione-di-piattaforma-notifiche':
'/send/guides/modello-di-integrazione',
},
};

describe('parseContent', () => {
Expand Down Expand Up @@ -284,6 +288,44 @@ describe('parseContent', () => {
]);
});

it('should apply a rewritted url coming from the config', () => {
expect(
parseContent(
'[Modello di Integrazione](https://docs.pagopa.it/modello-di-integrazione-di-piattaforma-notifiche/path/#fragment)',
config
)
).toStrictEqual([
new Markdoc.Tag('Paragraph', {}, [
new Markdoc.Tag(
'Link',
{
href: '/send/guides/modello-di-integrazione/path/#fragment',
},
['Modello di Integrazione']
),
]),
]);
});

it('should not apply a rewritted url coming from the config if not present', () => {
expect(
parseContent(
'[Modello di Integrazione](https://docs.pagopa.it/dont-exists/)',
config
)
).toStrictEqual([
new Markdoc.Tag('Paragraph', {}, [
new Markdoc.Tag(
'Link',
{
href: 'https://docs.pagopa.it/dont-exists/',
},
['Modello di Integrazione']
),
]),
]);
});

it('should parse unordered list', () => {
expect(parseContent('* Item', config)).toStrictEqual([
new Markdoc.Tag('List', { ordered: false }, [
Expand Down
12 changes: 11 additions & 1 deletion packages/gitbook-docs/src/markdoc/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export class LinkAttr {
'g'
);

const { urlReplaces } = parseContentConfig;
// Find the first key that match the url
const rewriteKey = Object.keys(urlReplaces).find((key) =>
value?.startsWith(key)
);

if (value && !value.startsWith('http') && !value.startsWith('mailto:')) {
const isIndex = variables?.isPageIndex === true;
const pagePath = isIndex
Expand All @@ -53,7 +59,11 @@ export class LinkAttr {
spacePrefix.pathPrefix
)
: value;
} else return value;
} else if (value && rewriteKey) {
return value.replace(rewriteKey, urlReplaces[rewriteKey]);
} else {
return value;
}
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/gitbook-docs/src/parseContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type ParseContentConfig = {
readonly spaceId: string;
readonly pathPrefix: string;
}>;
readonly urlReplaces: { readonly [url: string]: string };
};

const pairedHtmlTag = (tag: string) => ({
Expand Down

0 comments on commit 558d23c

Please sign in to comment.