Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pages/contributions/connector-contributor-guide.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default function PostPage({ frontmatter, content, id }) {
</Col>
<Col xs={1} className="gitIcon">
<a
href={`${process.env.gitHubPath}hacktoberfest/${id}.md`}
href={`${process.env.gitHubPath}contributions/${id}.md`}
target="_blank"
rel="noreferrer"
title="Edit in GitHub"
Expand Down
218 changes: 154 additions & 64 deletions pages/spec/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,38 @@
* under the License.
*/

import fs from 'fs';
import matter from 'gray-matter';
import React from 'react';
import ReactMarkdown from 'react-markdown';
import { Container, Col, Row } from 'react-bootstrap';
import remarkGfm from 'remark-gfm';
import Image from 'next-image-export-optimizer';
import rehypeRaw from 'rehype-raw';
import Head from 'next/head';
import Link from 'next/link';
import Layout from '../../layouts/LayoutSpec';
import { prefix } from '../../utils/prefix';
import fs from "fs";
import matter from "gray-matter";
import React from "react";
import ReactMarkdown from "react-markdown";
import { Container, Col, Row } from "react-bootstrap";
import remarkGfm from "remark-gfm";
import Image from "next-image-export-optimizer";
import rehypeRaw from "rehype-raw";
import Head from "next/head";
import Link from "next/link";
import Layout from "../../layouts/LayoutSpec";
import { prefix } from "../../utils/prefix";
import { highlight } from "../../utils/highlighter";
import { data } from "jquery";

String.prototype.hashCode = function () {
var hash = 0,
i, chr;
i,
chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash = (hash << 5) - hash + chr;
hash |= 0;
}
return hash;
}
};

var traverseFolder = function (dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function (file) {

var filex = dir + "/" + file;
var stat = fs.statSync(filex);

Expand All @@ -57,12 +58,11 @@ var traverseFolder = function (dir) {
}
} else {
/* Is a file */
if (filex !== 'spec/spec.md') {
if (filex !== "spec/spec.md") {
filex = filex.replace(/spec\//g, "");
results.push(filex);
}
}

});
return results;
};
Expand All @@ -72,7 +72,7 @@ export async function getStaticPaths() {
const files = traverseFolder("spec");
const paths = files.map((fileName) => ({
params: {
slug: fileName.replace("\/spec.md", ""),
slug: fileName.replace("/spec.md", ""),
},
}));

Expand All @@ -83,7 +83,6 @@ export async function getStaticPaths() {
}

export async function getStaticProps({ params: { slug } }) {

// slug = slug.join("/");
const fileName = fs.readFileSync(`spec/${slug}/spec.md`, "utf-8");
const { data: frontmatter, content } = matter(fileName);
Expand All @@ -93,106 +92,197 @@ export async function getStaticProps({ params: { slug } }) {
props: {
frontmatter,
content,
codeSnippets
codeSnippets,
},
};
}

// Define a constant for code block font size
const CODE_BLOCK_FONT_SIZE = "12.25px";

export default function PostPage({ frontmatter, content, codeSnippets }) {
const codes = codeSnippets ? new Map(JSON.parse(codeSnippets)) : new Map();

// Add id attributes to headings
const extractText = (value) => {
if (typeof value === 'string') {
return value
if (typeof value === "string") {
return value;
} else {
return value.props.children
return value.props.children;
}
}
};

const genrateId = (children) => {
const newArray = children.map(extractText);
let newId = newArray.join('').replace(/[&\/\\#,+()!$~%.'":*?<>{}]/g, '').toLowerCase();
newId = newId.replace(/ /g, '-');
let newId = newArray
.join("")
.replace(/[&\/\\#,+()!$~%.'":*?<>{}]/g, "")
.toLowerCase();
newId = newId.replace(/ /g, "-");

return newId;
}
};

const extractLibraryNameFromContent = () => {
return content.split("\n")[0].substring(2);
}
};

return (
<>
<Head>
<meta name="description" content={frontmatter.title || extractLibraryNameFromContent()} />
<meta
name="description"
content={frontmatter.title || extractLibraryNameFromContent()}
/>
<meta name="keywords" content={frontmatter.keywords} />

<title>{`${frontmatter.title || extractLibraryNameFromContent()} - The Ballerina programming language`}</title>
<title>{`${
frontmatter.title || extractLibraryNameFromContent()
} - The Ballerina programming language`}</title>

{/* <!--FB--> */}
<meta property="og:type" content="article" />
<meta property="og:title" content={`${frontmatter.title || extractLibraryNameFromContent()} - The Ballerina programming language`} />
<meta property="og:description" content={frontmatter.title || extractLibraryNameFromContent()}></meta>
<meta
property="og:title"
content={`${
frontmatter.title || extractLibraryNameFromContent()
} - The Ballerina programming language`}
/>
<meta
property="og:description"
content={frontmatter.title || extractLibraryNameFromContent()}
></meta>

{/* <!--LINKED IN --> */}
<meta property="og:description" content={frontmatter.title || extractLibraryNameFromContent()} />
<meta
property="og:description"
content={frontmatter.title || extractLibraryNameFromContent()}
/>

{/* <!--TWITTER--> */}
<meta name="twitter:title" content={`${frontmatter.title || extractLibraryNameFromContent()} - The Ballerina programming language`} />
<meta property="twitter:description" content={frontmatter.title || extractLibraryNameFromContent()} />
<meta property="twitter:text:description" content={frontmatter.title || extractLibraryNameFromContent()} />



<meta
name="twitter:title"
content={`${
frontmatter.title || extractLibraryNameFromContent()
} - The Ballerina programming language`}
/>
<meta
property="twitter:description"
content={frontmatter.title || extractLibraryNameFromContent()}
/>
<meta
property="twitter:text:description"
content={frontmatter.title || extractLibraryNameFromContent()}
/>
<style>
{`pre div pre {
font-size: ${CODE_BLOCK_FONT_SIZE};
}
pre pre div {
font-size: ${CODE_BLOCK_FONT_SIZE};
}
`}
</style>
</Head>
<Layout>
<Col xs={12} className='mdContent'>
<Col xs={12} className="mdContent">
<Container>
<Row className='topRowSpec'>
<Row className="topRowSpec">
<Col xs={12}>
<Link href='/' passHref className='specIcon'><Image src={`${prefix}/images/ballerina-logo.svg`} height={37} width={199} alt="Ballerina-logo" className='specIcon' /></Link>
<Link href="/" passHref className="specIcon">
<Image
src={`${prefix}/images/ballerina-logo.svg`}
height={37}
width={199}
alt="Ballerina-logo"
className="specIcon"
/>
</Link>
</Col>
</Row>

<Row className='specContent'>
<ReactMarkdown className='stdLib'
<Row className="specContent">
<ReactMarkdown
className="stdLib"
components={{
h2: ({ node, children, ...props }) => <h2 id={genrateId(children)} {...props}>{children}</h2>,
h3: ({ node, children, ...props }) => <h3 id={genrateId(children)} {...props}>{children}</h3>,
h4: ({ node, children, ...props }) => <h4 id={genrateId(children)} {...props}>{children}</h4>,
h5: ({ node, children, ...props }) => <h5 id={genrateId(children)} {...props}>{children}</h5>,
h6: ({ node, children, ...props }) => <h6 id={genrateId(children)} {...props}>{children}</h6>,
h2: ({ node, children, ...props }) => (
<h2 id={genrateId(children)} {...props}>
{children}
</h2>
),
h3: ({ node, children, ...props }) => (
<h3 id={genrateId(children)} {...props}>
{children}
</h3>
),
h4: ({ node, children, ...props }) => (
<h4 id={genrateId(children)} {...props}>
{children}
</h4>
),
h5: ({ node, children, ...props }) => (
<h5 id={genrateId(children)} {...props}>
{children}
</h5>
),
h6: ({ node, children, ...props }) => (
<h6 id={genrateId(children)} {...props}>
{children}
</h6>
),
ul: ({ node, children, ...props }) => (
<ul
style={{ listStyleType: "disc" }}
id={genrateId(children)}
{...props}
>
{children}
</ul>
),
code({ node, inline, className, children, ...props }) {
const key = (children[0]).trim().split(/\r?\n/).map(row => row.trim()).join('\n');
const key = children[0]
.trim()
.split(/\r?\n/)
.map((row) => row.trim())
.join("\n");
const highlightedCode = codes.get(key.hashCode());
if (highlightedCode) {
return <div dangerouslySetInnerHTML={{ __html: highlightedCode }} />
return (
<div
dangerouslySetInnerHTML={{ __html: highlightedCode }}
/>
);
}
const match = /language-(\w+)/.exec(className || '')
return inline ?
const match = /language-(\w+)/.exec(className || "");
return inline ? (
<code className={className} {...props}>
{children}
</code>
: match ?
<div dangerouslySetInnerHTML={{ __html: String(children).replace(/\n$/, '') }} />
: <pre className='default'>
<code className={className} {...props}>
{children}
</code>
</pre>
}
) : match ? (
<pre className="default">
<div
dangerouslySetInnerHTML={{
__html: String(children).replace(/\n$/, ""),
}}
/>
</pre>
) : (
<pre className="default">
<code className={className} {...props}>
{children}
</code>
</pre>
);
},
}}
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw]}
>
{content}
</ReactMarkdown>

</Row>
</Container>
</Col>

</Layout>
</>
);
Expand Down