Skip to content

JimLiu/baoyu-skills / skills/baoyu-danger-x-to-markdown

baoyu-danger-x-to-markdown

Verified

Converts X (Twitter) tweets and articles to markdown with YAML front matter. Uses reverse-engineered API requiring user consent. Use when user mentions "X to markdown", "tweet to markdown", "save tweet", or provides x.com/twitter.com URLs for conversion.

scripts/tweet-article.ts

View on GitHub
import { fetchXArticle } from "./graphql.js";
import type { ArticleEntity } from "./types.js";

function coerceArticleEntity(value: unknown): ArticleEntity | null {
  if (!value || typeof value !== "object") return null;
  const candidate = value as ArticleEntity;
  if (
    typeof candidate.title === "string" ||
    typeof candidate.plain_text === "string" ||
    typeof candidate.preview_text === "string" ||
    candidate.content_state
  ) {
    return candidate;
  }
  return null;
}

function hasArticleContent(article: ArticleEntity): boolean {
  const blocks = article.content_state?.blocks;
  if (Array.isArray(blocks) && blocks.length > 0) {
    return true;
  }
  if (typeof article.plain_text === "string" && article.plain_text.trim()) {
    return true;
  }
  if (typeof article.preview_text === "string" && article.preview_text.trim()) {
    return true;
  }
  return false;
}

function parseArticleIdFromUrl(raw: string | undefined): string | null {
  if (!raw) return null;
  try {
    const parsed = new URL(raw);
    const match = parsed.pathname.match(/\/(?:i\/)?article\/(\d+)/);
    if (match?.[1]) return match[1];
  } catch {
    return null;
  }
  return null;
}

function extractArticleIdFromUrls(urls: any[] | undefined): string | null {
  if (!Array.isArray(urls)) return null;
  for (const url of urls) {
    const candidate =
      url?.expanded_url ?? url?.url ?? (url?.display_url ? `https://${url.display_url}` : undefined);
    const id = parseArticleIdFromUrl(candidate);
    if (id) return id;
  }
  return null;
}

export function extractArticleEntityFromTweet(tweet: any): unknown | null {
  return (
    tweet?.article?.article_results?.result ??
    tweet?.article?.result ??
    tweet?.legacy?.article?.article_results?.result ??
    tweet?.legacy?.article?.result ??
    tweet?.article_results?.result ??
    null
  );
}

export function extractArticleIdFromTweet(tweet: any): string | null {
  const embedded = extractArticleEntityFromTweet(tweet);
  const embeddedArticle = embedded as { rest_id?: string } | null;
  if (embeddedArticle?.rest_id) {
    return embeddedArticle.rest_id;
  }

  const noteUrls = tweet?.note_tweet?.note_tweet_results?.result?.entity_set?.urls;
  const legacyUrls = tweet?.legacy?.entities?.urls;
  return extractArticleIdFromUrls(noteUrls) ?? extractArticleIdFromUrls(legacyUrls);
}

export async function resolveArticleEntityFromTweet(
  tweet: any,
  cookieMap: Record<string, string>
): Promise<unknown | null> {
  if (!tweet) return null;
  const embedded = extractArticleEntityFromTweet(tweet);
  const embeddedArticle = coerceArticleEntity(embedded);
  if (embeddedArticle && hasArticleContent(embeddedArticle)) {
    return embedded;
  }

  const articleId = extractArticleIdFromTweet(tweet);
  if (!articleId) {
    return embedded ?? null;
  }

  const fetched = await fetchXArticle(articleId, cookieMap, false);
  return fetched ?? embedded ?? null;
}
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 323kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 323kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 323kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 323kUpdated Claude CodeCodexVerifiedInstall