All files / app/features/markdown MarkdownPage.tsx

0% Statements 0/36
0% Branches 0/38
0% Functions 0/11
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import {
  Breadcrumbs,
  Container,
  Link,
  styled,
  Typography,
  TypographyProps,
} from "@mui/material";
import HtmlMeta from "components/HtmlMeta";
import { Trans, useTranslation } from "i18n";
import markdown from "markdown-it";
import Head from "next/head";
import { theme } from "theme";
 
const mkd = new markdown();
 
interface MarkdownPageFrontmatter {
  title: string;
  hide_title?: boolean;
  subtitle?: string;
  bustitle?: string;
  crumb?: string;
  description?: string;
  date?: string;
  author?: string;
  author_username?: string;
  is_blog_post?: boolean;
  has_custom_cta?: boolean;
  share_image?: string;
}
 
export interface MarkdownPageProps {
  slug: Array<string>;
  frontmatter: MarkdownPageFrontmatter;
  content: string;
}
 
const StyledBreadcrumbs = styled(Breadcrumbs)(({ theme }) => ({
  marginTop: theme.spacing(3),
  marginBottom: theme.spacing(3),
 
  "& a": {
    color: theme.palette.primary.main,
  },
}));
 
const StyledBusTitle = styled(Typography)<TypographyProps>(({ theme }) => ({
  marginTop: theme.spacing(3),
  "& > *": {
    fontSize: "1.5rem",
    fontWeight: "bold",
  },
  "& a": {
    color: theme.palette.primary.main,
  },
}));
 
const StyledMarkdown = styled("div")(({ theme }) => ({
  fontSize: theme.typography.fontSize,
  fontFamily: theme.typography.fontFamily,
  "& h1, & h2, & h3, & h4, & h5, & h6, & p": {
    borderBottom: "none",
    paddingBottom: 0,
    marginBottom: 0,
    marginTop: theme.spacing(2),
    overflowWrap: "break-word",
    whiteSpace: "pre-wrap",
  },
  "& h1": { ...theme.typography.h1, fontSize: "2.5rem", lineHeight: "1.125" },
  "& h2": { ...theme.typography.h2, fontSize: "1.75em !important" },
  "& h3": { ...theme.typography.h3, fontSize: "1.5em !important" },
  "& h4": theme.typography.h4,
  "& h5": theme.typography.h5,
  "& h6": theme.typography.h6,
  "& p": theme.typography.body1,
  "& ol": theme.typography.body1,
  "& ul": theme.typography.body1,
  "& blockquote": theme.typography.body1,
  "& a": {
    color: theme.palette.primary.main,
  },
  "& img": {
    width: "100%",
    maxWidth: "400px",
    height: "auto",
  },
  "& .tag": {
    color: "#fff",
    borderRadius: "4px",
    display: "inline-block",
    fontSize: "0.75rem",
    padding: "0.3rem 0.75rem",
  },
  "& .tag-large": {
    fontSize: "1.25rem",
  },
  "& .tag-governance": {
    backgroundColor: "#82bb42",
  },
  "& .tag-design": {
    backgroundColor: "#3da4ab",
  },
  "& .tag-tech": {
    backgroundColor: "#f46d50",
  },
 
  // Roadmap: fix peach backgrounds for dark mode readability
  // Row labels and cells with solid peach background
  '[data-mui-color-scheme="dark"] & td[style*="background-color: #FDE9D4"]': {
    backgroundColor: "#3d4347 !important",
  },
  // Header row with peach background - target the row and its children
  '[data-mui-color-scheme="dark"] & tr[style*="FDE9D4"]': {
    backgroundColor: "#3d4347 !important",
  },
  '[data-mui-color-scheme="dark"] & tr[style*="FDE9D4"] th': {
    backgroundColor: "inherit !important",
  },
  // Pending items with peach gradient
  '[data-mui-color-scheme="dark"] & td[style*="linear-gradient(to right, #FDE9D4"]':
    {
      background:
        "linear-gradient(to right, rgba(254, 152, 42, 0.3), transparent) !important",
    },
  // Completed items with green gradient - fade to dark background instead of white
  '[data-mui-color-scheme="dark"] & td[style*="linear-gradient(to right, #20686C"]':
    {
      background:
        "linear-gradient(to right, #20686C, #00A398, #313539) !important",
    },
  // Partially completed checkmark - make it white in dark mode
  '[data-mui-color-scheme="dark"] & .partial-check': {
    filter: "grayscale(1) brightness(10)",
  },
 
  // Blog index entry styling
  "& .blog-entry": {
    marginTop: theme.spacing(4),
  },
  "& .blog-entry h2": {
    marginBottom: 0,
  },
  "& .blog-entry-date": {
    marginTop: theme.spacing(0.5),
    fontStyle: "italic",
  },
}));
 
const StyledTitle = styled(Typography)(({ theme }) => ({
  fontSize: "2.5rem",
  lineHeight: "1.125",
}));
 
function AuthorList({
  author,
  authorUsername,
}: {
  author: string;
  authorUsername?: string;
}) {
  const authors = author.split(",").map((a) => a.trim());
  const usernames = authorUsername
    ? authorUsername.split(",").map((u) => u.trim())
    : [];
  return (
    <>
      {authors.map((name, i) => (
        <span key={name}>
          {i > 0 && i === authors.length - 1 ? " and " : i > 0 ? ", " : ""}
          {usernames[i] ? (
            <Link href={`/user/${usernames[i]}`}>{name}</Link>
          ) : (
            name
          )}
        </span>
      ))}
    </>
  );
}
 
function createBreadcrumbs({
  slug,
  frontmatter,
}: {
  slug: Array<string>;
  frontmatter: MarkdownPageFrontmatter;
}) {
  const crumbs = [{ key: "root", value: "Couchers.org", path: "/" }];
  if (slug.length > 2 && slug[0] == "blog") {
    // this is fragile, but basically hides the date from the blog crumbs
    crumbs.push({
      key: "blog",
      value: "Blog",
      path: "/blog/",
    });
    crumbs.push({
      key: slug[-1],
      value: frontmatter.title,
      path: "/" + slug.join("/") + "/",
    });
  } else {
    for (let i = 0; i < slug.length; i++) {
      const item = slug[i];
      crumbs.push({
        key: item,
        value:
          i == slug.length - 1
            ? frontmatter.crumb
              ? frontmatter.crumb
              : frontmatter.title
            : item.substring(0, 1).toUpperCase() +
              item.substring(1, item.length),
        path: (i == 0 ? "/" : crumbs[i - 1].path) + item + "/",
      });
    }
  }
  return crumbs;
}
 
export default function MarkdownPage({
  slug,
  frontmatter,
  content,
}: MarkdownPageProps) {
  const { t } = useTranslation();
  const subtitle = !!frontmatter.subtitle
    ? mkd.renderInline(frontmatter.subtitle)
    : null;
  const bustitle = !!frontmatter.bustitle
    ? mkd.renderInline(frontmatter.bustitle)
    : null;
 
  const crumbs = createBreadcrumbs({ slug, frontmatter });
 
  return (
    <>
      <HtmlMeta
        title={frontmatter.title}
        description={frontmatter.description}
        shareImage={frontmatter.share_image}
      />
      {slug.length === 1 && slug[0] === "blog" && (
        <Head>
          <link
            rel="alternate"
            type="application/rss+xml"
            title="Couchers.org Blog"
            href="/blog/rss.xml"
          />
        </Head>
      )}
      <Container
        disableGutters
        maxWidth="md"
        sx={{ marginTop: theme.spacing(3) }}
      >
        <StyledBreadcrumbs aria-label="breadcrumb">
          {crumbs.map((crumb, index) => {
            const isLast = index === crumbs.length - 1;
            return isLast ? (
              <Typography key={`${crumb.key}-${index}`} color="textPrimary">
                {crumb.value}
              </Typography>
            ) : (
              <Link
                key={crumb.key}
                underline="hover"
                color="inherit"
                href={crumb.path}
              >
                {crumb.value}
              </Link>
            );
          })}
        </StyledBreadcrumbs>
        {!frontmatter.hide_title && (
          <StyledTitle>{frontmatter.title}</StyledTitle>
        )}
        {subtitle && (
          <Typography component="h2">
            <div dangerouslySetInnerHTML={{ __html: subtitle }}></div>
          </Typography>
        )}
        <StyledMarkdown
          dangerouslySetInnerHTML={{ __html: content }}
        ></StyledMarkdown>
        {frontmatter.is_blog_post && frontmatter.date && (
          <Typography
            variant="body1"
            sx={{ fontStyle: "italic", marginTop: theme.spacing(2) }}
          >
            {frontmatter.author ? (
              <Trans
                i18nKey="blog.byline_with_author"
                values={{ date: frontmatter.date }}
              >
                {"Written by "}
                <AuthorList
                  author={frontmatter.author}
                  authorUsername={frontmatter.author_username}
                />
                {". Published on {{date}}."}
              </Trans>
            ) : (
              t("blog.byline_without_author", { date: frontmatter.date })
            )}
          </Typography>
        )}
        {frontmatter.is_blog_post && !frontmatter.has_custom_cta && (
          <Typography
            variant="body1"
            sx={{ fontWeight: "bold", marginTop: theme.spacing(3) }}
          >
            <Trans i18nKey="blog.cta_message">
              {"Want to help write our blog or volunteer? "}
              <Link href="/volunteer">Sign up</Link>
              {" and let us know. Volunteers and "}
              <Link href="/donate">donations</Link>
              {" are what make Couchers.org possible!"}
            </Trans>
          </Typography>
        )}
        {frontmatter.is_blog_post && (
          <Typography
            variant="body1"
            sx={{ marginTop: theme.spacing(3), display: "flex", gap: 1 }}
          >
            <Link
              href="https://www.instagram.com/couchersorg/"
              target="_blank"
              rel="noopener"
            >
              <img
                src="/img/blog/instagram_logo.svg"
                alt="Instagram"
                style={{ width: 26 }}
              />
            </Link>
            <Link
              href="https://bsky.app/profile/couchers.bsky.social"
              target="_blank"
              rel="noopener"
            >
              <img
                src="/img/blog/bluesky_logo.svg"
                alt="Bluesky"
                style={{ width: 26 }}
              />
            </Link>
          </Typography>
        )}
        {bustitle && (
          <StyledBusTitle component="h2">
            <div dangerouslySetInnerHTML={{ __html: bustitle }}></div>
          </StyledBusTitle>
        )}
      </Container>
    </>
  );
}