Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/app/lib/regex.const.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const regexes = {
AMP_REGEX: /\.amp$/,
APP_REGEX: /\.app$/,
LITE_REGEX: /\.lite$/,
MARKDOWN_REGEX: /\.md$/,
TLD_REGEX: /(\.com|\.co\.uk)/g,
};

Expand Down
25 changes: 25 additions & 0 deletions src/app/utilities/blocksToMarkdown/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default function blocksToMarkdown(blocks: Block[]): string {

Check failure on line 1 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Cannot find name 'Block'.

Check failure on line 1 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Cannot find name 'Block'.

Check failure on line 1 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Cannot find name 'Block'.

Check failure on line 1 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Cannot find name 'Block'.
return blocks.map(block => renderBlock(block)).join('\n\n');
}

function renderBlock(block: Block): string {

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Function lacks ending return statement and return type does not include 'undefined'.

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Cannot find name 'Block'.

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Function lacks ending return statement and return type does not include 'undefined'.

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

Cannot find name 'Block'.

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Function lacks ending return statement and return type does not include 'undefined'.

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Cannot find name 'Block'.

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Function lacks ending return statement and return type does not include 'undefined'.

Check failure on line 5 in src/app/utilities/blocksToMarkdown/index.ts

View workflow job for this annotation

GitHub Actions / cypress-run (22.x)

Cannot find name 'Block'.
switch (block.type) {
case 'paragraph':
return block.model?.text ?? '';
case 'headline':
return `# ${block.model?.blocks[0].model.blocks[0].model.text ?? ''}`;
case 'subheadline':
return `## ${block.model?.blocks[0].model.blocks[0].model.text ?? ''}`;
case 'text':
const thisBlock = block.model.blocks;
const returnText = thisBlock.reduce((acc, block) => {
return acc + `${block.model?.text}\r\r`;
}, '');
return `${returnText ?? ''}`;
case 'image':
const imgSrc = block.model.blocks[1].type === 'rawImage' ? block.model.blocks[1].model.locator : block.model.blocks[2].model.locator;
return `![${block.model.blocks[0].model.blocks[0].model.blocks[0].model.text}](https://ichef.bbci.co.uk/ace/ws/800/cpsprodpb/${imgSrc}.webp)`;
default:
// console.log(block.type, block);
}
}
3 changes: 2 additions & 1 deletion src/app/utilities/getPathExtension/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Url from 'url-parse';
import { APP_REGEX, AMP_REGEX, LITE_REGEX } from '#app/lib/regex.const';
import { APP_REGEX, AMP_REGEX, LITE_REGEX, MARKDOWN_REGEX } from '#app/lib/regex.const';

export default (url: string) => {
const { pathname } = new Url(url, true);
Expand All @@ -8,5 +8,6 @@ export default (url: string) => {
isAmp: AMP_REGEX.test(pathname),
isApp: APP_REGEX.test(pathname),
isLite: LITE_REGEX.test(pathname),
isMarkdown: MARKDOWN_REGEX.test(pathname),
};
};
10 changes: 9 additions & 1 deletion ws-nextjs-app/pages/[service]/articles/handleArticleRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import handleError from '#app/routes/utils/handleError';
import { PageTypes } from '#app/models/types/global';

import { ArticleMetadata } from '#app/models/types/optimo';
import blocksToMarkdown from '#app/utilities/blocksToMarkdown';
import augmentWithDisclaimer from './augmentWithDisclaimer';
import shouldRender from '../../../utilities/shouldRender';
import getPageData from '../../../utilities/pageRequests/getPageData';
Expand Down Expand Up @@ -40,7 +41,7 @@ export default async (context: GetServerSidePropsContext) => {

const resolvedUrlWithoutQuery = resolvedUrl.split('?')?.[0];

const { isAmp } = getPathExtension(resolvedUrlWithoutQuery);
const { isAmp, isMarkdown } = getPathExtension(resolvedUrlWithoutQuery);
const { variant } = parseRoute(resolvedUrl);

const { data } = await getPageData({
Expand Down Expand Up @@ -95,6 +96,13 @@ export default async (context: GetServerSidePropsContext) => {
'Cache-Control',
`public, stale-if-error=90, stale-while-revalidate=30, max-age=${maxAge}`,
);
if (isMarkdown) {
const markdown = blocksToMarkdown(data?.pageData?.article.content.model.blocks);
context.res.setHeader('Content-Type', 'text/markdown; charset=utf-8');
context.res.setHeader('x-markdown-tokens', Math.floor(markdown.length / 3));
context.res.end(markdown);
return { props: {} };
}

const {
topStories = null,
Expand Down
Loading