Skip to content

Commit

Permalink
feat: extract github urls from posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewnt219 committed Oct 17, 2021
1 parent 3e1603b commit 5bb609b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"rss-parser": "3.12.0",
"sanitize-html": "2.3.2",
"set-interval-async": "2.0.3",
"stoppable": "1.1.0"
"stoppable": "1.1.0",
"tslib": "2.3.1"
},
"devDependencies": {
"@babel/core": "7.14.3",
Expand Down
17 changes: 17 additions & 0 deletions src/web/src/components/Posts/Post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ const extractBlogClassName = (url: string) => {
return 'is-generic';
};

const extractGitHubUrlsFromPost = (htmlString: string): string[] => {
const parser = new DOMParser();
const postDoc = parser.parseFromString(htmlString, 'text/html');

const allGithubLinks = Array.from(
// all links that have href starts with 'https://github.com'
postDoc.querySelectorAll("a[href^='https://github.com']"),
(element) => (element as HTMLAnchorElement).href
);

// unique links only
return allGithubLinks.reduce(
(acc: string[], element) => (acc.includes(element) ? acc : [...acc, element]),
[]
);
};

const PostComponent = ({ postUrl }: Props) => {
const classes = useStyles();
const theme = useTheme();
Expand Down

0 comments on commit 5bb609b

Please sign in to comment.